if (!excel)
{
"Error while creating excel object!",
"No excel object can be instantiated!\n"
"Please, check if you have MS Excel installed.");
log("Excel object cannot be created (is not Excel installed?)!", 1);
return;
}
//QAxObject* application = excel->querySubObject("Application()");
QAxObject* workbooks
= excel
->querySubObject
("Workbooks()");
if (!workbooks)
{
"Error while creating excel object!",
"No excel object can be instantiated!\n"
"Workbooks in Excel document can be found.");
log("Excel object cannot be created (is not Excel installed?)!", 1);
return;
}
QAxObject* workbook
= workbooks
->querySubObject
("Add()");
QAxObject* worksheet
= workbook
->querySubObject
("Worksheets(int)",
1 );
ChartView* active = activeMdiChild();
if (!active)
{
log("There is no activated chart window!", 1);
return;
}
if (!model)
{
log("There is no data in chart window!", 1);
return;
}
for (int x=1; x<model->columnCount()+1; x++)
{
QAxObject *range
= worksheet
->querySubObject
("Range(QString)",
getExcelColumnName(x) + "1" +
":" +
getExcelColumnName(x) + "1"
);
range->setProperty("Value", model->horizontalHeaderItem(x-1)->text());
delete range;
}
for (int y=1; y<model->rowCount()+1; y++)
for (int x=1; x<model->columnCount()+1; x++)
{
QAxObject *range
= worksheet
->querySubObject
("Range(QString)",
getExcelColumnName
(x
) + QString("%1").
arg(y
+1) + ":" +
getExcelColumnName
(x
) + QString("%1").
arg(y
+1) );
range->setProperty("Value", model->item(y-1, x-1)->data(Qt::EditRole));
delete range;
}
QAxObject* excel = new QAxObject("Excel.Application", 0);
if (!excel)
{
QMessageBox::critical(this,
"Error while creating excel object!",
"No excel object can be instantiated!\n"
"Please, check if you have MS Excel installed.");
log("Excel object cannot be created (is not Excel installed?)!", 1);
return;
}
//QAxObject* application = excel->querySubObject("Application()");
QAxObject* workbooks = excel->querySubObject("Workbooks()");
if (!workbooks)
{
QMessageBox::critical(this,
"Error while creating excel object!",
"No excel object can be instantiated!\n"
"Workbooks in Excel document can be found.");
log("Excel object cannot be created (is not Excel installed?)!", 1);
return;
}
QAxObject* workbook = workbooks->querySubObject("Add()");
QAxObject* worksheet = workbook->querySubObject("Worksheets(int)", 1 );
ChartView* active = activeMdiChild();
if (!active)
{
log("There is no activated chart window!", 1);
return;
}
QStandardItemModel *model = active->getModel();
if (!model)
{
log("There is no data in chart window!", 1);
return;
}
for (int x=1; x<model->columnCount()+1; x++)
{
QAxObject *range = worksheet->querySubObject("Range(QString)",
getExcelColumnName(x) + "1" +
":" +
getExcelColumnName(x) + "1"
);
range->setProperty("Value", model->horizontalHeaderItem(x-1)->text());
delete range;
}
for (int y=1; y<model->rowCount()+1; y++)
for (int x=1; x<model->columnCount()+1; x++)
{
QAxObject *range = worksheet->querySubObject("Range(QString)",
getExcelColumnName(x) + QString("%1").arg(y+1) +
":" +
getExcelColumnName(x) + QString("%1").arg(y+1)
);
range->setProperty("Value", model->item(y-1, x-1)->data(Qt::EditRole));
delete range;
}
To copy to clipboard, switch view to plain text mode
workbook->dynamicCall("SaveAs (const QString&)", fileName);
workbook->dynamicCall("Close()");
excel->dynamicCall("Quit()");
workbook->dynamicCall("SaveAs (const QString&)", fileName);
workbook->dynamicCall("Close()");
excel->dynamicCall("Quit()");
To copy to clipboard, switch view to plain text mode
Bookmarks