38.QT-QAxObject快速寫入EXCEL示例
阿新 • • 發佈:2019-04-21
lis column http %20 exce caption 單元 copy bob
參考鏈接: https://blog.csdn.net/czyt1988/article/details/52121360
1. QAxObject介紹
在QT中,有個自帶的QAxObject類,可以直接操作EXCEL
除此之外,當我們操作某個文件夾下的EXCEL的時候,都會在該文件夾下出現一個隱藏文件~$XX.xlsx,如下圖所示:
此時,如果我們再用QAxObject去操作這個文件時,該文件是只讀的,無法保存成功的,所以我們使用QAxObject時,需要提前判斷一次.
2.示例
首先在項目下創建一個模板templatePath.xlsx文件
3.效果如下
4.寫代碼
#include <QApplication> #include "widget.h" #include <QtGui> #include <QAxObject> #include <QStandardPaths> #include <QFileDialog> #include <QFileDialog> /*excel操作*/ enum EXcel_ColumnType{ ColumnA = 1, ColumnB = 2, ColumnC = 3, ColumnD= 4, ColumnE = 5, ColumnF = 6, ColumnG = 7, ColumnH = 8, ColumnI = 9 }; QString saveas() { QString file; QString filter; //如果版本低於QT5,則需要將: // QStandardPaths::writableLocation(QStandardPaths::DesktopLocation), //改為:QDesktopServices::storageLocation(QDesktopServices::DesktopLocation),file = QFileDialog::getSaveFileName ( NULL, //父組件 "另存為", //標題 QStandardPaths::writableLocation(QStandardPaths::DesktopLocation), //設置路徑, .表示當前路徑,./表示更目錄 "Excel(*.xlsx)", //過濾器 &filter ); return file; } //設置EXCEL中單個內容的數據 void Excel_SetCell(QAxObject *worksheet,EXcel_ColumnType column,int row,QColor color,QString text) { QAxObject *cell = worksheet->querySubObject("Cells(int,int)", row, column); cell->setProperty("Value", text); QAxObject *font = cell->querySubObject("Font"); font->setProperty("Color", color); } //把QVariant轉為QList<QList<QVariant> >,用於快速讀出的 void castVariant2ListListVariant(const QVariant &var, QList<QList<QVariant> > &res) { QVariantList varRows = var.toList(); if(varRows.isEmpty()) { return; } const int rowCount = varRows.size(); QVariantList rowData; for(int i=0;i<rowCount;++i) { rowData = varRows[i].toList(); res.push_back(rowData); } } //把QList<QList<QVariant> > 轉為QVariant,用於快速寫入的 void castListListVariant2Variant(const QList<QList<QVariant> > &cells, QVariant &res) { QVariantList vars; const int rows = cells.size(); for(int i=0;i<rows;++i) { vars.append(QVariant(cells[i])); } res = QVariant(vars); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QString templatePath = "./template.xlsx"; QFileInfo info(templatePath); if(!info.exists()) { qDebug()<<"template.xlsx is NULL"; return 0; } templatePath = info.absoluteFilePath(); //獲取模板的絕地路徑 templatePath = QDir::toNativeSeparators(templatePath); //轉換一下路徑,讓windows能夠識別 QString ExcelFile = QDir::toNativeSeparators(saveas()); //打開文件保存對話框,找到要保存的位置 if(ExcelFile=="") return 0; QFile::copy(templatePath, ExcelFile); //將模板文件復制到要保存的位置去 info.setFile(ExcelFile); info.setFile(info.dir().path()+"/~$"+info.fileName()); if(info.exists()) //判斷一下,有沒有"~$XXX.xlsx"文件存在,是不是為只讀 { qDebug()<<"報表屬性為只讀,請檢查文件是否已打開!"; return 0; } QAxObject *excel = new QAxObject();//建立excel操作對象 excel->setControl("Excel.Application");//連接Excel控件 excel->setProperty("Visible", true);//顯示窗體看效果 excel->setProperty("DisplayAlerts", false);//顯示警告看效果 QAxObject *workbooks = excel->querySubObject("WorkBooks"); QAxObject* workbook = workbooks->querySubObject("Open(const QString&)",QDir::toNativeSeparators(ExcelFile) ); //打開 excel->setProperty("Caption", "Qt Excel"); //標題為Qt Excel QAxObject *work_book = excel->querySubObject("ActiveWorkBook"); QAxObject *worksheet = work_book->querySubObject("Sheets(int)",1); //獲取表單1 Excel_SetCell(worksheet,ColumnB,2,QColor(74,51,255),"12345"); //設置B2單元格內容為12345 Excel_SetCell(worksheet,ColumnB,3,QColor(255,255,0),"B3"); //設置B3單元格內容 Excel_SetCell(worksheet,ColumnB,4,QColor(255,0,0),"B4"); //設置B4單元格內容 /*批量一次性設置A6~I106所在內容*/ QAxObject *user_range = worksheet->querySubObject("Range(const QString&)","A6:I106"); QList<QList<QVariant>> datas; for(int i=1;i<101;i++) { QList<QVariant> rows; for(int j=1;j<10;j++) { rows.append(i*j); } datas.append(rows); } QVariant var; castListListVariant2Variant(datas,var); user_range->setProperty("Value", var); workbook->dynamicCall("Save()" );
// workbook->dynamicCall("Close()"); //關閉文件 // excel->dynamicCall("Quit()");//關閉excel return 0; }
未完待續,下章學習Qtxlsx庫操作EXCEL: https://www.cnblogs.com/lifexy/p/10743352.html
38.QT-QAxObject快速寫入EXCEL示例