1. 程式人生 > >Qt 讀取excel

Qt 讀取excel

let variant tlist dal ast for close pre int

void CMainWindow::openExcel(QString fileName)
{
    QAxObject excel("Excel.Application");
    excel.setProperty("Visible", false);
    QAxObject *work_books = excel.querySubObject("WorkBooks");
    work_books->dynamicCall("Open(const QString&)", fileName);

    QAxObject *work_book = excel.querySubObject("
ActiveWorkBook"); QAxObject *work_sheets = work_book->querySubObject("Sheets"); //Sheets也可換用WorkSheets int sheet_count = work_sheets->property("Count").toInt(); //獲取工作表數目 if (sheet_count > 0) { QAxObject *work_sheet = work_book->querySubObject("Sheets(int)", 1); ui.label
->setText("文件數據讀取中..."); QVariant var = readAll(work_sheet); castVariant2ListListVariant(var); } work_book->dynamicCall("Close(Boolean)", false); //關閉文件 excel.dynamicCall("Quit(void)"); //退出 } QVariant CMainWindow::readAll(QAxObject *sheet) { QVariant var; if
(sheet != NULL && !sheet->isNull()) { QAxObject *usedRange = sheet->querySubObject("UsedRange"); if (NULL == usedRange || usedRange->isNull()) { return var; } var = usedRange->dynamicCall("Value"); delete usedRange; } return var; } void CMainWindow::castVariant2ListListVariant(const QVariant &var) { QVariantList varRows = var.toList(); if (varRows.isEmpty()) { return; } const int rowCount = varRows.size(); QVariantList rowData; for (int i = 0; i < rowCount; ++i) { rowData = varRows[i].toList(); if (i == 0) { QStringList headers; for each (auto item in rowData) { QString value = item.toString(); headers.append(value); } ui.tableWidget->setColumnCount(headers.size()); //設置列數 ui.tableWidget->setHorizontalHeaderLabels(headers); } else { int row = ui.tableWidget->rowCount(); ui.tableWidget->setRowCount(row + 1); for (int j = 0; j < rowData.size(); j++) { QString value = rowData[j].toString(); QTableWidgetItem *item = new QTableWidgetItem(value); ui.tableWidget->setItem(row, j, item); } } } }

Qt 讀取excel