谁能帮助我,如何将我的QtableView保存为 Excel 文件。我有一个QTableView和 一个 QPushButton(保存按钮)。如果我在我的 QtableView 中输入值并且如果我单击保存按钮 QTableView 项目应保存为 Excel 文件。请帮助我。谢谢..
问问题
14458 次
3 回答
3
看看这个线程My-approach-to-export-QTableView-data-to-a-Microsoft-Excel-file。我通过向 Google 询问获得了此链接:QTableView 保存。对于那里的解决方案,您需要一个启用了 ODBC 的 Qt,这不是默认设置。
您还可以研究包含例如包含 writeToFile 函数的地址簿示例的详细文档。您可以从那里开始搜索例如有关 CSV 格式的信息。
于 2012-09-27T10:42:39.373 回答
3
this link worked for me : http://www.qtcn.org/bbs/simple/?t47265.html
1-you should also use below code in .pro file.
QT += sql
2-you should use #include "exportexcelobject.h"
in your file that wanna export to excel.
于 2016-04-23T05:21:35.877 回答
1
请参阅代码示例:
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());
}
于 2016-03-31T21:14:46.127 回答