Qt excel文件转换csv文件

程序猿的世界,功能,功能,还是功能!!!

接下来我们共同探讨开发一下《利用Qt实现Excel文件转csv文件》
如果Excel文件内容超过1048576行,那么这种方法就不行了。
也可以直接将Excel文件用记事本或notepad++打开查看里面的数据信息
还是话不多说,上干货!!!
1、最主要的一点是添加 axcontainer 模块
QT       += core gui  axcontainer

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11  axcontainer

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

2、添加头文件 QAxObject
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <ActiveQt/QAxObject>
#include <QFile>
#include <QDir>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    bool ExcelToCsvFile(const QString &excelFileName, const QString &csvFileName);

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
3、功能函数代码
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
/* Excel转CSV */
bool MainWindow::ExcelToCsvFile(const QString &excelFileName, const QString &csvFileName)
{
    if(!QFile::exists(excelFileName))
        return false;
    QScopedPointer<QAxObject> excel(new QAxObject());
    /* 连接Excel控件 */
    bool excelOk = excel->setControl("Excel.Application");
    if(!excelOk)
        return false;
    /* 窗体显示设置 true:显示;false:关闭 */
    excel->dynamicCall("SetVisible(bool)",false);
    /* 警告信息设置 true:显示;false:关闭 */
    excel->setProperty("DisplayAlerts",false);
    /* 获取工作簿集合 */
    QAxObject *workBooks = excel->querySubObject("WorkBooks");
    if(workBooks == nullptr)
        return false;
    /* 获取当前工作簿 */
    QAxObject *workBook = workBooks->querySubObject("Open(const QString &)",excelFileName);
    if(workBook == nullptr)
        return false;
    /* 获取工作表集合中的工作表 */
    QAxObject *workSheets = workBook->querySubObject("WorkSheets");
    if(workSheets == nullptr)
        return false;
    /* 获取工作表集合中的工作表1,sheet1 */
    QAxObject *workSheet = workSheets->querySubObject("Item(int)",1);
    if(workSheet == nullptr)
        return false;
    /* 另存为文件,3:txt文件(空格分隔);6:csv文件(逗号分隔) */
    workSheet->dynamicCall("SaveAs(const QString&,int)",QDir::toNativeSeparators(csvFileName),6);
    /* 关闭工作簿 */
    workBook->dynamicCall("Close()");
    excel->dynamicCall("Quit()");
    return true;
}

void MainWindow::on_pushButton_clicked()
{
    QString excelName = ui->lineEdit->text();
    bool saveSuccess = ExcelToCsvFile(excelName,"D:\\123.csv");
    if(saveSuccess)
        ui->label->setText("转换成功");
}

4、UI布局
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>529</width>
    <height>148</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>390</x>
      <y>20</y>
      <width>100</width>
      <height>50</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>16</pointsize>
     </font>
    </property>
    <property name="text">
     <string>转换</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>20</y>
      <width>300</width>
      <height>50</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>16</pointsize>
     </font>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>90</y>
      <width>181</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

热衷于奉献全部代码的小狼人,只为服务于你我他!!! 共同进步!!!
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淘气の小狼人¹º²⁴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值