1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
/one*************************************************************************************************/ void TestReadExcel::readExcel() { QAxObject *excel = NULL; QAxObject *workbooks = NULL; QAxObject *workbook = NULL; excel = new QAxObject( "Excel.Application" ); if (!excel) { QMessageBox::critical( this , "錯誤信息" , "EXCEL對象丟失" ); return ; } excel->dynamicCall( "SetVisible(bool)" , false ); workbooks = excel->querySubObject( "WorkBooks" ); workbook = workbooks->querySubObject( "Open(QString, QVariant)" , QString(tr( "d:\\test.xls" ))); QAxObject * worksheet = workbook->querySubObject( "WorkSheets(int)" , 1); //打開第一個sheet //QAxObject * worksheet = workbook->querySubObject("WorkSheets");//獲取sheets的集合指針 //int intCount = worksheet->property("Count").toInt();//獲取sheets的數量 QAxObject * usedrange = worksheet->querySubObject( "UsedRange" ); //獲取該sheet的使用範圍對象 QAxObject * rows = usedrange->querySubObject( "Rows" ); QAxObject * columns = usedrange->querySubObject( "Columns" ); /*獲取行數和列數*/ int intRowStart = usedrange->property( "Row" ).toInt(); int intColStart = usedrange->property( "Column" ).toInt(); int intCols = columns->property( "Count" ).toInt(); int intRows = rows->property( "Count" ).toInt(); /*獲取excel內容*/ for ( int i = intRowStart; i < intRowStart + intRows; i++) //行 { for ( int j = intColStart; j < intColStart + intCols; j++) //列 { QAxObject * cell = worksheet->querySubObject( "Cells(int,int)" , i, j ); //獲取單元格 // qDebug() << i << j << cell->property("Value"); //*****************************出問題!!!!!! qDebug() << i << j <<cell->dynamicCall( "Value2()" ).toString(); //正確 } } workbook->dynamicCall( "Close (Boolean)" , false ); //同樣,設置值,也用dynamimcCall("SetValue(const QVariant&)", QVariant(QString("Help!")))這樣才成功的。。 //excel->dynamicCall("Quit (void)"); delete excel; //一定要記得刪除,要不線程中會一直打開excel.exe } /****************************************************************************************************/
/two*************************************************************************************************/
int main(int argc, char **argv)
{
QApplication a(argc, argv);
// QAxSelect select;
// select.show();
QAxWidget excel("Excel.Application");
excel.setProperty("Visible", true);//顯示當前窗口
//excel.setProperty("Caption","Invoke Microsoft Excel");//更改標題欄
QAxObject * workbooks = excel.querySubObject("WorkBooks");//獲取excel的集合指針
QAxObject *workbook = workbooks->querySubObject("Open (const QString &)",QString("c:/test.xls"));//打開指定路徑的xls文檔
QAxObject * worksheets = workbook->querySubObject("WorkSheets");//獲取sheets的集合指針
int intCount = worksheets->property("Count").toInt();//獲取sheets的數量
for (int i = 1; i <= intCount; i++)
{
int intVal;
QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", i);//獲取指定位置的sheet頁面
qDebug() << i << worksheet->property("Name").toString();
QAxObject * range = worksheet->querySubObject("Cells(1,1)");//獲取sheet頁面中的C(1,1)數據
intVal = range->property("Value").toInt();//保存該數據
range->setProperty("Value", QVariant(intVal+1));//將數據加1並重新保存回C(1,1)
QAxObject * range2 = worksheet->querySubObject("Range(C1)");//???????????
intVal = range2->property("Value").toInt();
range2->setProperty("Value", QVariant(intVal+1));
}
QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", 1);//打開第一個sheet
QAxObject * usedrange = worksheet->querySubObject("UsedRange");//獲取該sheet的使用範圍對象
QAxObject * rows = usedrange->querySubObject("Rows");
QAxObject * columns = usedrange->querySubObject("Columns");
int intRowStart = usedrange->property("Row").toInt();
qDebug() << intRowStart;
int intColStart = usedrange->property("Column").toInt();
qDebug() << intColStart;
int intCols = columns->property("Count").toInt();
int intRows = rows->property("Count").toInt();
for (int i = intRowStart; i < intRowStart + intRows; i++)
{
for (int j = intColStart; j < intColStart + intCols; j++)
{
QAxObject * range = worksheet->querySubObject("Cells(int,int)", i, j );
qDebug() << i << j << range->property("Value");
}
}
excel.setProperty("DisplayAlerts", 0);
workbook->dynamicCall("SaveAs (const QString&)", QString("c:/xlsbyqt.xls"));
excel.setProperty("DisplayAlerts", 1);
workbook->dynamicCall("Close (Boolean)", false);
excel.dynamicCall("Quit (void)");
return a.exec();
}
|
Qt讀取excel表格
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.