我目前正在做的项目是将Excel文件导出为PDF。
Excel文件是一个允许生成图形的“模板”。目标是填充Excel文件的某些单元格,以便生成图形,然后以PDF格式导出文件。
我在C++中的QAxObject类中使用了Qt,所有的数据写入过程都运行良好,但PDF导出部分却不是这样。
问题是,生成的PDF文件还包含图形的数据,而这些数据不包括在Excel模板的打印区域中。
PDF导出是使用"ExportAsFixedFormat“函数完成的,该函数有一个参数可以忽略位置5处的"IgnorePrintAreas”打印区域。即使我决定将此参数设置为"false",因此不忽略打印区域并因此考虑打印区域,但这并不能解决问题,它产生的结果与此参数设置为"true“时的结果相同。
我试图改变其他参数,改变传入参数的数据类型,或者不使用任何参数,但这并没有改变所获得的总是相同的结果。
以下是指向导出命令“ExportAsFixedFormat”的“文档”的链接: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()");
}A这一点我真的需要帮助来找到解决这个问题的方法。
我还想知道这是不是QAxObject类的错误。
发布于 2021-07-19 15:48:38
我终于在另一个论坛上找到了解决方案。如果有人需要帮助,我会把答案留给link。
https://stackoverflow.com/questions/68117753
复制相似问题