程序猿的世界,功能,功能,还是功能!!!
接下来我们共同探讨开发一下《利用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
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;
}
bool MainWindow :: ExcelToCsvFile ( const QString & excelFileName, const QString & csvFileName)
{
if ( ! QFile :: exists ( excelFileName) )
return false ;
QScopedPointer< QAxObject> excel ( new QAxObject ( ) ) ;
bool excelOk = excel-> setControl ( "Excel.Application" ) ;
if ( ! excelOk)
return false ;
excel-> dynamicCall ( "SetVisible(bool)" , 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 ;
QAxObject * workSheet = workSheets-> querySubObject ( "Item(int)" , 1 ) ;
if ( workSheet == nullptr )
return false ;
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>
热衷于奉献全部代码的小狼人,只为服务于你我他!!! 共同进步!!!