QAxObject * excel = NULL;
QAxObject * workbooks = NULL;
QAxObject * workbook = NULL;
excel = new QAxObject(this);
excel -> setControl("Excel.Application");
if (!excel) {
QMessageBox:: critical(NULL, "错误信息", "EXCEL对象丢失");
return;
}
excel -> dynamicCall("SetVisible(bool)", false);
workbooks = excel -> querySubObject("WorkBooks");
workbook = workbooks -> querySubObject("Open(const QString&)", path);
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 intCols = columns -> property("Count").toInt();
int intRows = rows -> property("Count").toInt();
int intRowStart = usedrange -> property("Row").toInt();
int intColStart = usedrange -> property("Column").toInt();
ui.tableWidget -> setRowCount(intRows);
/*获取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);
QString value = cell -> dynamicCall("Value2()").toString();
setItemValue(i - 1, j - 1, value);
delete cell;
}
}
// 关闭excel
workbook -> dynamicCall("Close(Boolean)", true);
excel -> dynamicCall("Quit(void)");
delete excel;
excel = NULL;
版权归属:
三味线
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区