您的位置:首页 > 编程语言 > Qt开发

Qt之导出PDF、HTML和Word(二)

2015-11-25 09:43 155 查看
五、HTML与Word搜索"Qt操作Word",可以找到通过QAxObject和COMObject联合直接读写Word的方法。但是,这个方法用起来不是很方便,在次,我介绍一种另类的方法,就是将“html格式代码保存到QString”,然后将QString导出为“.doc文件”。类似于直接保存“.html文件”,不同的是文件后缀名。如下示例代码:
void SaveReportThread::SaveToWord()
{
QString ReportPath = QDir::toNativeSeparators(QCoreApplication::applicationDirPath()); // 必须把路径中的'/'转换成'\\'
if (m_bSaveAll)
ReportPath += "\\TestReport_All.pdf";
else
ReportPath += "\\TestReport_" + m_pMainwindow->m_strCurrentItemName + ".pdf";
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(ReportPath);
QString strHtml;
//基本信息
...
//word
ReportPath = QDir::toNativeSeparators(QCoreApplication::applicationDirPath()); // 必须把路径中的'/'转换成'\\'
if (m_bSaveAll)
ReportPath += "\\TestReport_All.doc";
else
ReportPath += "\\TestReport_" + m_pMainwindow->m_strCurrentItemName + ".doc";
QFile WordDoc(ReportPath);
WordDoc.open(QIODevice::WriteOnly | QIODevice::Truncate );
QTextStream stream(&WordDoc);
stream << strHtml << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: