[英]How to drive excel window from Qt application
在我的Qt應用程序中,我有一個帶有表的窗口,該表使用來自后端服務器的數據進行動態更新。 我需要我的窗口能夠打開excel實例,將表中的所有數據插入excel窗口,並在表中的數據更新時更新excel單元格。
這是可以實現的嗎? 如果是這樣,我該如何完成呢? 我不介意只能在Windows上運行的依賴於平台的解決方案。
這是我最后使用的代碼。
打開Excel:
QAxObject* excel = new QAxObject("Excel.Application", 0);
excel->dynamicCall("SetVisible(bool)", true);
讀取單元格值:
QAxObject* workbooks = excel->querySubObject("Workbooks");
QAxObject* workbook = workbooks->querySubObject("Open(const QString&)", "D:\\a.xlsx");
QAxObject* worksheet = workbook->querySubObject("Worksheets(int)", 1);
QAxObject* usedrange = worksheet->querySubObject("UsedRange");
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();
QAxObject* cell;
for (int i = intRowStart; i < intRowStart + intRows; i++)
{
for (int j = intColStart; j < intColStart + intCols; j++)
{
cell = excel->querySubObject("Cells(Int, Int)", i, j);
QVariant cellValue = cell->dynamicCall("value");
qDebug() << cellValue.toString();
}
}
寫單元格值
cell->setProperty("Value", "somevalue");
注意:在執行任何上述操作之前,請記住先創建QApplication。 我花了大量時間通過不這樣做來找出問題所在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.