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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值