QDir dir("C:/Users/Chark Zhou/Desktop/Log"); // 指定文件夹路径 foreach (QString file, dir.entryList(QStringList() << "*.xls", QDir::Files)) { // 遍历所有.xlsx文件 QAxObject *excel = new QAxObject("Excel.Application"); QAxObject *workbooks = excel->querySubObject("Workbooks"); QAxObject *workbook = workbooks->querySubObject("Open(const QString&)", dir.absoluteFilePath(file)); QAxObject *sheets = workbook->querySubObject("Worksheets"); int sheetCount = sheets->dynamicCall("Count()").toInt(); for (int i = 1; i <= sheetCount; i++) { QAxObject *sheet = sheets->querySubObject("Item(int)", i); QAxObject *usedRange = sheet->querySubObject("UsedRange"); QAxObject *rows = usedRange->querySubObject("Rows"); QAxObject *columns = usedRange->querySubObject("Columns"); int rowCount = rows->dynamicCall("Count()").toInt(); int columnCount = columns->dynamicCall("Count()").toInt(); for (int row = 1; row <= rowCount; row++) { for (int column = 1; column <= columnCount; column++) { QAxObject *cell = sheet->querySubObject("Cells(int,int)", row, column); QString value = cell->dynamicCall("Value()").toString(); if (value.contains(" ANDY")) { ui->textBrowser->append(QString::fromLocal8Bit("文件 %1, 表格 %2, 位置 %3").arg(file).arg(i).arg(cell->dynamicCall("Address()").toString())); } cell->clear(); delete cell; } } delete sheet; } workbook->dynamicCall("Close()"); delete workbook; delete workbooks; excel->dynamicCall("Quit()"); delete excel; }为什么这一段代码会导致数据少的.xls文件能打开,数据大的无法打开呢
时间: 2024-03-28 21:38:32 浏览: 85
baseOpe.rar_xml/soap/webservice_QT_
这段代码可能会导致数据量较大的.xls文件无法打开的原因可能是因为程序没有足够的内存来处理大量数据,或者是由于Excel应用程序的实例未正确关闭而导致的内存泄漏。另外,这段代码中没有对异常情况进行处理,例如打开文件失败,遍历工作表失败等,这也可能会导致程序崩溃。建议对代码进行优化和改进,加入异常处理机制,释放资源等操作,以提高程序的稳定性和可靠性。
阅读全文