QCAD集成QXlsx读写Excel

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中。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片