我目前正在进行的项目是将Excel文件导出为PDF。
Excel 文件是一个允许生成图表的“模板”。目标是填充 Excel 文件的一些单元格,以便生成图表,然后以 PDF 格式导出文件。
我在 C++ 中使用 Qt 和 QAxObject 类,所有数据写入过程都运行良好,但 PDF 导出部分则不然。
问题是生成的PDF文件还包含图表的数据,而这些数据不包含在Excel模板的打印区域中。
PDF 导出是通过“ExportAsFixedFormat”函数完成的,该函数作为参数可以忽略位置 5 处的“IgnorePrintAreas”打印区域。即使我决定将此参数设置为“false”,也不要忽略打印区域,因此要考虑打印区域,这并不能解决问题,并且会产生与此参数设置为“true”相同的结果。
我尝试改变其他参数,更改参数中传递的数据类型或不使用任何参数,但它不会对获得的结果进行任何更改,结果始终相同。
这里是导出命令“ExportAsFixedFormat”的“文档”的链接: https://learn.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 类的错误。