Qt – 如何将我的QTableView保存为Excel文件?

任何人都可以帮我,如何将我的QtableView保存为Excel文件。 我有一个QTableView一个QPushButton (保存button)。 如果我在我的QtableViewinput值,如果我点击保存buttonQTableView项目应保存为Excel文件。 Plz帮帮我。 谢谢..

看看这个线程My-approach-to-export-QTableView-data-to-a-Microsoft-Excel-file 。 我得到这个链接要求谷歌:QTableView保存。 对于那里的解决scheme,你需要一个启用了ODBC的Qt,这不是默认的。

您也可以研究包含例如包含writeToFile函数的通讯簿示例的罚款文档。 您可以从那里开始search关于CSV格式的信息。

这个链接为我工作: http : //www.qtcn.org/bbs/simple/?t47265.html

1 – 你也应该在.pro文件中使用下面的代码。 QT += sql

2 – 你应该使用#include "exportexcelobject.h"在你想要导出为ex​​cel的文件中。

看代码示例:

ExcelExportHelper.h

 #ifndef EXCELHELPER_H #define EXCELHELPER_H #include <ActiveQt/qaxobject.h> #include <ActiveQt/qaxbase.h> #include <QString> //Expected in .pro file: QT += axcontainer //Application must be of UI type for ActiveX work. class ExcelExportHelper { public: ExcelExportHelper(const ExcelExportHelper& other) = delete; ExcelExportHelper& operator=(const ExcelExportHelper& other) = delete; ExcelExportHelper(bool closeExcelOnExit = false); void SetCellValue(int lineIndex, int columnIndex, const QString& value); void SaveAs(const QString& fileName); ~ExcelExportHelper(); private: QAxObject* m_excelApplication; QAxObject* m_workbooks; QAxObject* m_workbook; QAxObject* m_sheets; QAxObject* m_sheet; bool m_closeExcelOnExit; }; #endif // EXCELHELPER_H 

ExcelExportHelper.cpp

 #include <ActiveQt/qaxobject.h> #include <ActiveQt/qaxbase.h> #include <QString> #include <QFile> #include <stdexcept> using namespace std; #include "ExcelExportHelper.h" ExcelExportHelper::ExcelExportHelper(bool closeExcelOnExit) { m_closeExcelOnExit = closeExcelOnExit; m_excelApplication = nullptr; m_sheet = nullptr; m_sheets = nullptr; m_workbook = nullptr; m_workbooks = nullptr; m_excelApplication = nullptr; m_excelApplication = new QAxObject( "Excel.Application", 0 );//{00024500-0000-0000-C000-000000000046} if (m_excelApplication == nullptr) throw invalid_argument("Failed to initialize interop with Excel (probably Excel is not installed)"); m_excelApplication->dynamicCall( "SetVisible(bool)", false ); // hide excel m_excelApplication->setProperty( "DisplayAlerts", 0); // disable alerts m_workbooks = m_excelApplication->querySubObject( "Workbooks" ); m_workbook = m_workbooks->querySubObject( "Add" ); m_sheets = m_workbook->querySubObject( "Worksheets" ); m_sheet = m_sheets->querySubObject( "Add" ); } void ExcelExportHelper::SetCellValue(int lineIndex, int columnIndex, const QString& value) { QAxObject *cell = m_sheet->querySubObject("Cells(int,int)", lineIndex, columnIndex); cell->setProperty("Value",value); delete cell; } void ExcelExportHelper::SaveAs(const QString& fileName) { if (fileName == "") throw invalid_argument("'fileName' is empty!"); if (fileName.contains("/")) throw invalid_argument("'/' character in 'fileName' is not supported by excel!"); if (QFile::exists(fileName)) { if (!QFile::remove(fileName)) { throw new exception(QString("Failed to remove file '%1'").arg(fileName).toStdString().c_str()); } } m_workbook->dynamicCall("SaveAs (const QString&)", fileName); } ExcelExportHelper::~ExcelExportHelper() { if (m_excelApplication != nullptr) { if (!m_closeExcelOnExit) { m_excelApplication->setProperty("DisplayAlerts", 1); m_excelApplication->dynamicCall("SetVisible(bool)", true ); } if (m_workbook != nullptr && m_closeExcelOnExit) { m_workbook->dynamicCall("Close (Boolean)", true); m_excelApplication->dynamicCall("Quit (void)"); } } delete m_sheet; delete m_sheets; delete m_workbook; delete m_workbooks; delete m_excelApplication; } 

用法:

 try { const QString fileName = "g:\\temp\\kaka2.xlsx"; ExcelExportHelper helper; helper.SetCellValue(1,1,"Test-11"); helper.SetCellValue(1,2,"Test-12"); helper.SaveAs(fileName); } catch (const exception& e) { QMessageBox::critical(this, "Error - Demo", e.what()); }