Qt中Excel的使用

QtXlsx並不存在在安裝好的Qt裏面,如果要使用,必須要自己下載QtXlsx的源代碼加入自己的工程裏面

準備工作:

    QtXlsx下載地址:https://github.com/QtExcel/QXlsx 或者 https://github.com/dbzhang800/QtXlsxWriter

    git 方式: git clone https://github.com/QtExcel/QXlsx.git

    開發環境 VS2015 & Qt5.8.0

配置環境和示例代碼:

    1. 新建工程並Copy 包含QXlsx源文件的文件到工程目錄;

    2. 添加包含目錄,右擊Project->properties->C/C++->general->Additional Include Directories, 添加以下路徑:

        $(QTDIR)\include
        $(QTDIR)\include\QtCore
        $(QTDIR)\include\QtCore\5.8.0
        $(QTDIR)\include\QtGui
        $(QTDIR)\include\QtGui\5.8.0
        $(QTDIR)\include\QtGui\5.8.0\QtGui
        $(ProjectDir)QXlsx\header

    3. Linker->input 添加Qt庫 Qt5Core.lib,Qt5Gui.lib;

    4. 添加QXlsx源文件,Project>add->Existing Item選擇QXlsx的.h和.cpp文件到工程;

    5. 添加以下代碼到main函數:

#include <QCoreApplication>

#include "xlsxdocument.h"
#include "xlsxchartsheet.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"
using namespace QXlsx;

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	QXlsx::Document xlsx;
	xlsx.write("A1", "Hello A1!"); // write "Hello Qt!" to cell(A,1). it's shared string.
	xlsx.write("A2", "Hello A2!"); // write "Hello Qt!" to cell(A,2). it's shared string.
	xlsx.write("B1", "Hello B1!"); // write "Hello Qt!" to cell(B,1). it's shared string.
	xlsx.write("B2", "Hello B2!"); // write "Hello Qt!" to cell(B,2). it's shared string.
	xlsx.saveAs("Test.xlsx"); // save the document as 'Test.xlsx'

	return 0;
}

    6. 編譯,複製Qt的Qt5Core.dll,Qt5Gui.dll庫到生成的exe文件所在目錄。

    7. 運行測試程序,即可生成excel文件,打開可以查看程序插入裏面的內容,如下圖所示。

 

 

示例工程文件下載:

我的資源->QtExecelTest.rar

 

reference:

1. https://github.com/QtExcel/QXlsx

2.https://qtexcel.github.io/QXlsx/HowToSetProject.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章