Can anyone plz help me, how to save my QtableView as a Excel File. I have a QTableView and a QPushButton (Save Button). If i enter the values in my QtableView and if i click the Save Buttton the QTableView items should be saved as Excel File. Plz Help me. Thanks..
Have a look at this thread My-approach-to-export-QTableView-data-to-a-Microsoft-Excel-file. I got this link by asking Google for: QTableView save. For the solution there you need a Qt with ODBC enabled which is not the default.
You can also study the fine documentation that contains e.g. an Address Book Example that includes a writeToFile function. You can start from there and search e.g. for information about the CSV format.
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.
See the code sample:
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;
}
Usage:
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());
}
© 2022 - 2024 — McMap. All rights reserved.