QCAD
集成QXlsx
读写Excel
QXlsx
源码git clone https://github.com/QtExcel/QXlsx.git
QCAD
三方库集成QXlsx
源码中的QXlsx
文件夹拷贝到/src/3rdparty
下。/src/3rdparty/3rdparty.pro
。SUBDIRS = \
spatialindexnavel \
stemmer \
proj4 \
QXlsx
在\support\examples\exampleplugin\exampleplugin.pro
中加链接库-lQXlsx
,同时创建helloqxlsx
类。
CONFIG += plugin
TARGET = example_test
include(../../../shared.pri)
TEMPLATE = lib
HEADERS = RExamplePlugin.h \
helloqxlsx.h
SOURCES = RExamplePlugin.cpp \
helloqxlsx.cpp
DESTDIR = $$PWD/../../../plugins
LIBS += -lqcadcore -lqcadgui -lqcadecmaapi -lQXlsx
RESOURCES = scripts.qrc
helloqxlsx.h
#ifndef HELLOQXLSX_H
#define HELLOQXLSX_H
class HelloQXlsx
{
public:
HelloQXlsx();
static void CreateExcel();
static void ReadExcel();
};
#endif // HELLOQXLSX_H
helloqxlsx.cpp
#include "helloqxlsx.h"
#include "QXlsx/header/xlsxdocument.h"
#include "QXlsx/header/xlsxchartsheet.h"
#include "QXlsx/header/xlsxcellrange.h"
#include "QXlsx/header/xlsxchart.h"
#include "QXlsx/header/xlsxrichstring.h"
#include "QXlsx/header/xlsxworkbook.h"
#include "RSettings.h"
#include <QDebug>
#include <QApplication>
using namespace QXlsx;
HelloQXlsx::HelloQXlsx()
{
}
void HelloQXlsx::CreateExcel()
{
//write QXlsx
QXlsx::Document xlsx;
QXlsx::Workbook* workBook= xlsx.workbook();
QXlsx::Worksheet* workSheet=static_cast<QXlsx::Worksheet*>(workBook->addSheet("sheet1"));
workSheet->write(1,1,"Hello Sheet");
QXlsx::Cell *cell = workSheet->cellAt(1, 1);
QString value;
if (cell!=NULL){
if (cell->isDateTime())
{
//cell->dateTime().toDate().year();
value = cell->dateTime().toDate().toString("yyyy/MM/dd hh:mm");
}
else
{
value = cell->value().toString();
}
}
int r= workSheet->dimension().rowCount();
int c= workSheet->dimension().columnCount();
//qDebug()<<"Excel value(1,1):"<<value;
// qDebug()<<"Excel row:"<<r<<";column:"<<c;
xlsx.saveAs("Test.xlsx");
//xlsx.write(i + 1, j + 1, value);
}
void HelloQXlsx::ReadExcel()
{
QString path=RSettings::getApplicationPath()+"/Test.xlsx";
qDebug()<<path;
QXlsx::Document xlsx(path);
if(xlsx.load()){
QXlsx::Workbook *workBook = xlsx.workbook();
QXlsx::Worksheet *workSheet = static_cast<QXlsx::Worksheet*>(workBook->sheet(0));
int row= workSheet->dimension().rowCount();
int column=workSheet->dimension().columnCount();
QXlsx::Cell *cell = workSheet->cellAt(1, 1);
QString value=cell->value().toString();
//qDebug()<<"Read-Excel row:"<<row<<";column:"<<column;
//qDebug()<<"Read-Excel value(1,1):"<<value;
}
}
可以将QCAD
中的点,线段等对象导出Excel
文件,亦可以读取Excel
文件到QCAD中。