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)
{
}
|
Qt读取excel表格
最新推荐文章于 2025-05-26 14:45:33 发布