需求:將軟件處理的結果保存爲一個報告文檔,文檔中包含表格、圖片、文字,格式爲word的.doc和.pdf。生成word是爲了便於用戶編輯。html
開發環境:qt4.8.4+vs2010框架
在qt的官網上對於pdf的操做介紹以下:http://qt-project.org/wiki/Handling_PDF 。即經過QPrinter類來建立pdf;還有經過第三方庫PoDoFo、Hummus。本文主要介紹的是用QPrinter類來建立pdf。函數
在qt的官網上對於word的操做介紹以下:http://qt-project.org/wiki/Handling_Microsoft_Word_file_format 。即經過Word自己COM組件的形式,Qt的ActiveX框架來實現;還有經過xml來實現。本文主要介紹Qt ActiveX和html格式生成word文檔。ui
下面舉例詳細說明pdf和word文檔的生成:this
1.pdf的生成spa
pdf文檔裏要求有表格、圖片、文字,參考博客:http://blog.sina.com.cn/s/blog_a6fb6cc90101gvnx.html。裏面有介紹文字、圖片、表格分別是怎樣經過QPrinter類來實現的。可是遇到一個麻煩就是怎樣把這三種格式的東西放在一塊兒並且排版好呢,用上述博客裏的方法嘗試以後,最終肯定使用第三種生成表格(html格式)的方式來獲得:.net
- QPrinter printer_text;
- printer_text.setOutputFormat(QPrinter::PdfFormat);
- printer_text.setOutputFileName(pdfname);
-
- QTextDocument text_document;
- QString html = GeneratePicWord();
-
- text_document.setHtml(html);
- text_document.print(&printer_text);
- QTextBlock it = text_document.end();
GeneratePicWord()函數的內容大體以下:code
- QString html;
- QDateTime current_date_time = QDateTime::currentDateTime();
- QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
- html += "<h2 align=\"center\">育種管理模塊</h2>";
- html += "<h4 align=\"center\">" + current_date + "</h2><br>";
- html += "<img align=\"middle\" src = \"" + imagepath + "\" width=\"600\" height=\""+QString::number(showHeight) + "\"/><br>" ;
- html += "<table align=\"center\" border=\"0.2\" cellspacing=\"0\" cellpadding=\"0\" style=\"width: 100%; height: 100%;\">";
- html +="<tr>";
- QString fieldname;
- for ( int i = 0; i < fieldCount; ++i)
- {
- fieldname = fields[i].name();
- html +="<td bgcolor=\"Silver\">" + fieldname + "</td>";
- }
- html +="</tr></table>";
最後結果獲得的pdf如圖:orm
2.word的生成xml
word的生成能夠用QActiveX來實現。博客:http://www.360doc.com/content/14/0227/16/7918060_356177077.shtml裏說的比較詳細了。首先新建一個模板文件Id.dot,在模板文件中事先「插入」-》「書籤」,以下圖所示:
書籤主要在表格的第一行前兩列,分別是code,ndvi。表格下面有兩個書籤,分別是pic,pic2。用代碼插入須要的文字和圖片:
- QAxWidget *word = new QAxWidget("Word.Application",this, Qt::MSWindowsOwnDC);
- word->setProperty("Visible", true);
- QAxObject *documents = word->querySubObject("Documents");
- documents->dynamicCall("Add(QString)", QString::fromLocal8Bit("F:/Id.dot"));
- QAxObject *document = word->querySubObject("ActiveDocument");
-
- QString code = "code";
- QAxObject *bookmark_code = document->querySubObject("Bookmarks(QVariant)", "code");
- if ( !bookmark_code->isNull())
- {
- bookmark_code->dynamicCall("Select(void)");
- bookmark_code->querySubObject("Range")->setProperty("Text", "textg");
- }
- QAxObject *bookmark_ndvi = document->querySubObject("Bookmarks(QVariant)", "ndvi");
- if ( !bookmark_ndvi->isNull())
- {
- bookmark_ndvi->dynamicCall("Select(void)");
- bookmark_ndvi->querySubObject("Range")->setProperty("Text", "ndvi");
- }
-
- QAxObject *bookmark_ndvi2 = document->querySubObject("Bookmarks(QVariant)", "ndvi");
- if ( !bookmark_ndvi2->isNull())
- {
- bookmark_ndvi2->dynamicCall("Select(void)");
- bookmark_ndvi2->querySubObject("Range")->setProperty("Text", "ndvi2");
- }
-
- QAxObject *bookmark_pic = document->querySubObject("Bookmarks(QVariant)", "pic");
- if ( !bookmark_pic->isNull())
- {
- bookmark_pic->dynamicCall("Select(void)");
- QAxObject *shapes = document->querySubObject("InlineShapes");
- shapes->dynamicCall("AddPicture(Const QString&)", "F:\\CND.jpg");
-
- }
- document->dynamicCall("SaveAs (const QString&)", QString("F:/testword.doc"));
- document->dynamicCall("Close(boolean)", false);
- word->dynamicCall("Quit()");
獲得的結果就不貼了。存在有兩個問題:1.在生成的word裏圖片的大小很是小,須要人爲把它拉大才看獲得;2.表格數據只會插入到第一行已經預設好書籤的位置。對於有多條數據,還不知道怎樣動態建立書籤來插入數據。在網上找了些資料,未能很好的經過模板書籤的方式來解決這兩個問題。
那麼想經過上面生成pdf時的html能不能直接保存爲.doc文件呢?答案是能!並且節約了不少時間,只需一次生成固定格式的html,就能夠保存爲pdf和doc文件,多好的事情啊,爲何要用Qt ActiveX呢。
固然不能直接把生成pdf的文件名改爲doc就好了,還須要作下面一個事情:
- QString html;
- html += "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\" >"; //這句可加可不加。主要是由於我在word裏把doc另存爲html文件後,看到有這麼個頭標籤,由此想到直接將html文檔保存爲doc文件。
- html =GeneratePicWord();
-
- QFile outFile(docname);
- outFile.open(QIODevice::WriteOnly | QIODevice::Append );
- QTextStream ts(&outFile);
- ts<<html<<endl;
主要在後面那4行,將html輸出到doc文件中去。
獲得的結果如圖:
pdf和word的生成就完成了,具體獲得的結果可能格式上還須要調整,html對於調格式仍是很方便的。
注:轉載請註明出處
http://blog.csdn.net/tszhangjunqiao/article/details/22681351