excelqtpdfexportqaxobject

Export PDF file from Excel template with Qt and QAxObject


The project I am currently working on is to export an Excel file to PDF.

The Excel file is a "Template" that allows the generation of graphs. The goal is to fill some cells of the Excel file so that the graphs are generated and then to export the file in PDF.

I use Qt in C++ with the QAxObject class and all the data writing process works well but it's the PDF export part that doesn't.

The problem is that the generated PDF file also contains the data of the graphs while these data are not included in the print area of the Excel template.

The PDF export is done with the "ExportAsFixedFormat" function which has as a parameter the possibility to ignore the print area that is "IgnorePrintAreas" at position 5. Even if I decide to set this parameter to "false", so not to ignore the print area and therefore to take into account the print area, this does not solve the problem and it produces the same result as if this parameter was set to "true".

I tried to vary the other parameters, to change the type of data passed in parameter or not to use any parameter but it does not change anything to the obtained result which is always the same.

Here is the link to the "documentation" of the export command "ExportAsFixedFormat": https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat

I give you a simplified version of the command suite that is executed in the code:

Rapport::Rapport(QObject *parent) : QObject(parent)
{
        //Create the template from excel file
        QString pathTemplate = "/ReportTemplate_FR.xlsx"
        QString pathReporter = "/Report"
        this->path = QDir(QDir::currentPath() + pathReporter + pathTemplate);
        QString pathAbsolute(this->path.absolutePath().replace("/", "\\\\")); 

        //Create the output pdf file path
        fileName = QString("_" + QDateTime::currentDateTime().toString("yyyyMMdd-HHmmssff") + "_Report");
        QString pathDocument = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation).append("/").replace("/", "\\\\");
        QString exportName(pathDocument + fileName + ".pdf");

        //Create the QAxObjet that is linked to the excel template
        this->excel = new QAxObject("Excel.Application");

        //Create the QAxObject « sheet » who can accepte measure data
        QAxObject* workbooks = this->excel->querySubObject("Workbooks");
        QAxObject* workbook = workbooks->querySubObject("Add(const QString&)", pathAbsolute);
        QAxObject* sheets = workbook->querySubObject("Worksheets");
        QAxObject* sheet = sheets->querySubObject("Item(int)", 3);

        //Get some data measure to a list of Inner class Measurement
        QList<Measurement*> actuMeasure = this->getSomeMeasure() ; //no need to know how it’s work…

        //Create a 2 dimentional QVector to be able to place data on the table where we want (specific index) 
        QVector<QVector<QVariant>> vCells(actuMeasure.size());
        for(int i = 0; i < vCells.size(); i++)
                vCells[i].resize(6);

        //Fill the 2 dimentional QVector with data measure
        int row = 0;
        foreach(Measurement* m, actuMeasure)
        {
                vCells[row][0] = QVariant(m->x);
                vCells[row][1] = QVariant(m->y1);
                vCells[row][2] = QVariant(m->y2);
                vCells[row][3] = QVariant(m->y3);
                vCells[row][4] = QVariant(m->y4);
                vCells[row][5] = QVariant(m->y5);
                row++;
        }

        //Transform the 2 dimentional QVector on a QVariant object
        QVector<QVariant> vvars;
        QVariant var;
        for(int i = 0; i < actuMeasure.size(); i++)
                vvars.append(QVariant(vCells[i].toList()));
        var = QVariant(vvars.toList());

        //Set the QVariant object that is the data measure on the excel file
        sheet->querySubObject("Range(QString)", "M2:AB501")->setProperty("Value", var);

        //Set the fileName on the page setup (not relevant for this example)
        sheet->querySubObject("PageSetup")->setProperty("LeftFooter", QVariant(fileName));

        //Export to PDF file with options – NOT WORKING !!!
        workbook->dynamicCall("ExportAsFixedFormat(const QVariant&, const QVariant&, const QVariant&, const QVariant&, const QVariant&)", QVariant(0), QVariant(exportName), QVariant(0), QVariant(false), QVariant(false));

        //Close
        workbooks->dynamicCall("Close()");
        this->excel->dynamicCall("Quit()");
}

A this point I really need help to find a way to solve this problem.

I also wonder if this is not a bug of the QAxObject class.


Solution

  • I finally found a solution on another forum. If anyone needs help, I'll leave the link to the answer.