QtXlsxWriter
1.下载
https://github.com/dbzhang800/QtXlsxWriter
2.使用
写xlsx简单例子
1 #include "xlsxdocument.h" 2 #include "xlsxformat.h"
1 QXlsx::Document xlsx; 2 xlsx.addSheet("日志信息"); 3 xlsx.selectSheet("日志信息"); 4 xlsx.setColumnWidth("A1", 7.33); 5 xlsx.setColumnWidth("B1", 17.67); 6 xlsx.setColumnWidth("C1", 14.44); 7 xlsx.setColumnWidth("D1", 15); 8 xlsx.setColumnWidth("E1", 47.44); 9 QXlsx::Format format; 10 format.setHorizontalAlignment(QXlsx::Format::AlignHCenter); 11 format.setVerticalAlignment(QXlsx::Format::AlignVCenter); 12 format.setFontBold(true); 13 format.setFontColor(QColor(Qt::black)); 14 format.setFontSize(11); 15 format.setFontName("等线"); 16 xlsx.write("A1", "序号", format); 17 xlsx.write("B1", "用户名称", format); 18 xlsx.write("C1", "日志类型", format); 19 xlsx.write("D1", "日志时间", format); 20 xlsx.write("E1", "日志内容", format); 21 22 for (int i = 0; i < logInfoAllVec.size(); i++) 23 { 24 xlsx.write(QString("A%1").arg(i + 2), i + 1, format); 25 xlsx.write(QString("B%1").arg(i + 2), logInfoAllVec[i].name, format); 26 int type = logInfoAllVec[i].type; 27 if (TipsLog == type) 28 { 29 xlsx.write(QString("C%1").arg(i + 2), LogTip, format); 30 } 31 else if (OperationLog == type) 32 { 33 xlsx.write(QString("C%1").arg(i + 2), LogOperation, format); 34 } 35 else if (WarningLog == type) 36 { 37 xlsx.write(QString("C%1").arg(i + 2), LogWarning, format); 38 } 39 40 xlsx.write(QString("D%1").arg(i + 2), logInfoAllVec[i].time, format); 41 xlsx.write(QString("E%1").arg(i + 2), logInfoAllVec[i].content, format); 42 } 43 QDir *filePathDir = new QDir; 44 bool exist = filePathDir->exists("./report/"); 45 if (!exist) 46 { 47 filePathDir->mkdir("./report/"); 48 } 49 QString FileName = QDateTime::currentDateTime().toString("yyyyMMddhhmmss") + "日志信息"; 50 QString excelFileName = "./report/" + FileName + ".xlsx"; 51 xlsx.saveAs(excelFileName);
读xlsx简单例子
1 QString path = QFileDialog::getOpenFileName(this, "选择文件", ".", "Images (*.xlsx)"); 2 if (path.length() != 0) 3 { 4 bool bOk = OnCheckImportExcel(path); 5 if (!bOk) 6 { 7 return; 8 } 9 QXlsx::Document xlsx(path); 10 int rowCount = xlsx.dimension().rowCount(); 11 if (rowCount == 0) 12 { 13 QUIMessageBox::Instance()->setMessage("没有内容", 2, 0); 14 QUIMessageBox::Instance()->exec(); 15 return; 16 } 17 QVector<EpidemicPreventionManage> epidemicPreventionInfoAllVec;//全部的防疫 18 for (int i = 1; i < rowCount; i++) 19 { 20 if (m_userInfo.type == USER)//普通用户 21 { 22 QString villageName = xlsx.read(QString("D%1").arg(i + 1)).toString(); 23 if (villageName != m_userInfo.villageZoneName) 24 { 25 continue; 26 } 27 } 28 else if (m_userInfo.type == ADMIN)//管理员用户 29 { 30 QString townshipName = xlsx.read(QString("C%1").arg(i + 1)).toString(); 31 if (townshipName != m_userInfo.townshipName) 32 { 33 continue; 34 } 35 } 36 37 EpidemicPreventionManage epidemicPreventionInfo; 38 epidemicPreventionInfo.CountyZoneName = xlsx.read(QString("B%1").arg(i + 1)).toString(); 39 epidemicPreventionInfo.townshipZoneName = xlsx.read(QString("C%1").arg(i + 1)).toString(); 40 epidemicPreventionInfo.villageZoneName = xlsx.read(QString("D%1").arg(i + 1)).toString(); 41 epidemicPreventionInfo.HerdsmenName = xlsx.read(QString("E%1").arg(i + 1)).toString(); 42 epidemicPreventionInfo.EquipmentName = xlsx.read(QString("F%1").arg(i + 1)).toString(); 43 epidemicPreventionInfo.officer = xlsx.read(QString("G%1").arg(i + 1)).toString(); 44 epidemicPreventionInfo.vaccinumName = xlsx.read(QString("H%1").arg(i + 1)).toString(); 45 epidemicPreventionInfo.batch = xlsx.read(QString("I%1").arg(i + 1)).toString(); 46 epidemicPreventionInfo.date = xlsx.read(QString("J%1").arg(i + 1)).toDate(); 47 epidemicPreventionInfo.time = QDateTime::currentDateTime(); 48 epidemicPreventionInfoAllVec.append(epidemicPreventionInfo); 49 } 50 if (epidemicPreventionInfoAllVec.size() > 0) 51 { 52 QVariant dataInfo; 53 dataInfo.setValue(epidemicPreventionInfoAllVec); 54 emit OnBatchAddEpidemicPreventionInfoRequest(dataInfo); 55 } 56 else 57 { 58 QUIMessageBox::Instance()->setMessage("没有内容", 2, 0); 59 QUIMessageBox::Instance()->exec(); 60 return; 61 } 62 }
天道酬勤 循序渐进 技压群雄