使用 Qt 和 QAxObject 从 Excel 模板导出 PDF 文件

Export PDF file from Excel template with Qt and QAxObject

我目前正在进行的项目是将 Excel 文件导出为 PDF。

Excel 文件是一个允许生成图表的“模板”。目标是填充 Excel 文件的一些单元格,以便生成图表,然后以 PDF 格式导出文件。

我在 C++ 中将 Qt 与 QAxObject class 一起使用,所有数据写入过程都运行良好,但 PDF 导出部分却不行。

问题是生成的 PDF 文件还包含图表数据,而这些数据不包含在 Excel 模板的打印区域中。

PDF 导出是使用“ExportAsFixedFormat”函数完成的,该函数作为参数可以忽略位置 5 处的“IgnorePrintAreas”打印区域。即使我决定将此参数设置为“false”,所以不要忽略打印区域,因此要考虑打印区域,这并不能解决问题,它会产生与此参数设置为“true”相同的结果。

我尝试改变其他参数,更改参数中传递的数据类型或不使用任何参数,但它不会对始终相同的结果产生任何影响。

这是导出命令“ExportAsFixedFormat”的“文档”的link: https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat

我给你一个在代码中执行的命令套件的简化版本:

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()");
}

这一点我真的需要帮助找到解决这个问题的方法。

我也想知道这是否是 QAxObject 的错误 class。

我终于在另一个论坛上找到了解决方案。 如果有人需要帮助,我会把 link 留给答案。