前言:環境 win7 64位,QT4.8.5,QT Creatorios
1 //main.cpp 2 #include "mainwindow.h" 3 #include <QApplication> 4 #include <QtGui> 5 #include <QWidget> 6 #include <QAxObject> 7 #include <QAxWidget> 8 #include <QFileDialog> 9 #include <QObject> 10 #include <iostream> 11 using namespace std; 12 13 14 int main(int argc, char *argv[]) 15 { 16 QApplication a(argc, argv); 17 18 QString filepath=QFileDialog::getSaveFileName(NULL,QObject::tr("Save orbit"),"/untitled.xls",QObject::tr("Microsoft Office 2007 (*.xlsx)"));//獲取保存路徑 19 QList<QVariant> allRowsData;//保存全部行數據 20 allRowsData.clear(); 21 // mLstData.append(QVariant(12)); 22 if(!filepath.isEmpty()){ 23 QAxObject *excel = new QAxObject("Excel.Application");//鏈接Excel控件 24 excel->dynamicCall("SetVisible (bool Visible)",false);//不顯示窗體 25 excel->setProperty("DisplayAlerts", true);//不顯示任何警告信息。若是爲true那麼在關閉是會出現相似「文件已修改,是否保存」的提示 26 QAxObject *workbooks = excel->querySubObject("WorkBooks");//獲取工做簿集合 27 workbooks->dynamicCall("Add");//新建一個工做簿 28 QAxObject *workbook = excel->querySubObject("ActiveWorkBook");//獲取當前工做簿 29 QAxObject *worksheets = workbook->querySubObject("Sheets");//獲取工做表集合 30 QAxObject *worksheet = worksheets->querySubObject("Item(int)",1);//獲取工做表集合的工做表1,即sheet1 31 32 for(int row = 1; row <= 1000; row++) 33 { 34 QList<QVariant> aRowData;//保存一行數據 35 for(int column = 1; column <= 2; column++) 36 { 37 aRowData.append(QVariant(row*column)); 38 } 39 allRowsData.append(QVariant(aRowData)); 40 } 41 42 QAxObject *range = worksheet->querySubObject("Range(const QString )", "A1:B1000"); 43 range->dynamicCall("SetValue(const QVariant&)",QVariant(allRowsData));//存儲全部數據到 excel 中,批量操做,速度極快 44 range->querySubObject("Font")->setProperty("Size", 30);//設置字號 45 46 QAxObject *cell = worksheet->querySubObject("Range(QVariant, QVariant)","A1");//獲取單元格 47 cell = worksheet->querySubObject("Cells(int, int)", 1, 1);//等同於上一句 48 cell->dynamicCall("SetValue(const QVariant&)",QVariant(123));//存儲一個 int 數據到 excel 的單元格中 49 cell->dynamicCall("SetValue(const QVariant&)",QVariant("abc"));//存儲一個 string 數據到 excel 的單元格中 50 51 QString str = cell->dynamicCall("Value2()").toString();//讀取單元格中的值 52 cout<<"\nThe value of cell is "<<str.toStdString()<<endl; 53 54 /*QAxObject *font = cell->querySubObject("Font"); 55 font->setProperty("Name", itemFont.family()); //設置單元格字體 56 font->setProperty("Bold", itemFont.bold()); //設置單元格字體加粗 57 font->setProperty("Size", itemFont.pixelSize()); //設置單元格字體大小 58 font->setProperty("Italic",itemFont.italic()); //設置單元格字體斜體 59 font->setProperty("Underline", itemFont.underline()); //設置單元格下劃線 60 font->setProperty("Color", item->foreground().color()); //設置單元格字體顏色*/ 61 worksheet->querySubObject("Range(const QString&)", "1:1")->setProperty("RowHeight", 60);//調整第一行行高 62 63 workbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(filepath));//保存至filepath,注意必定要用QDir::toNativeSeparators將路徑中的"/"轉換爲"\",否則必定保存不了。 64 workbook->dynamicCall("Close()");//關閉工做簿 65 excel->dynamicCall("Quit()");//關閉excel 66 delete excel; 67 excel=NULL; 68 } 69 return a.exec(); 70 }
參考資料:http://blog.csdn.net/li494816491/article/details/50274305app