参考文献:
在Qt中实现对word的操作有两种方式,即QAxWidget的书签标号法和html语言直接读写法,前一种方法自己试用了,感觉不怎么好用,总会出现这样那样的问题,后一种方法则比较易于理解,而且免去了人工定义书签的方法,同时在实现批量报告的过程中可以节省很大的工作量提高工作效率。
html语言直接读写法:
这里直接上程序,下面例子中会在word中写入五个表,一幅图(图也是写在表中),根据word模板来写表是一个细活,这个word文件花了一天半才写好,还是前期对html语言不熟,但这也是一个不断学习的过程,喜欢这种感觉,可以不断的学习新东西,每天都在进步着。
注意:Qt 向word写入中文汉字时,需要对中文字符串做处理: QString::fromLocal8Bit("汉字");
程序如下:
#include "widget.h"
#include <QApplication>
#include <QAxObject>
#include <QAxWidget>
#include <QDebug>
#include <QAxSelect>
#include <QDir>
#include <QTextCodec>
#include <QString>
#include <QDateTime>
QString GeneratePicWord()
{
QString html;
//文字部分
QDateTime current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
QString s_paper_name = QString::fromLocal8Bit( "报告" );
QString s_paper_title = QString::fromLocal8Bit( "***********" );
QString imagepath = "f:/115.jpeg";
long showHeight = 100;
html += "<h2 align=\"center\">" + s_paper_name + "</h2>";
html += "<h4 align=\"center\">" + s_paper_title + "</h2><br>";
// html += "<img align=\"middle\" src = \"" + imagepath + "\" width=\"100\" height=\""+QString::number(showHeight) + "\"/><br>" ;
html += "<table align=\"center\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" width=\"645\" >";
html += "<tr>";
QString fieldname;
QStringList fields;
fields << QString::fromLocal8Bit( "*****" ) << QString::fromLocal8Bit( "*************" )
<< QString::fromLocal8Bit( "*****" ) << QString::fromLocal8Bit( "*************" );
html +="<td align=\"center\" width=102 height=20 colspan=\"1\">" + fields.at( 0 ) + "</td>";
html +="<td align=\"center\" width=233 height=20 colspan=\"3\">" + fields.at( 1 ) + "</td>";
html +="<td align=\"center\" width=95 height=20 colspan=\"1\">" + fields.at( 2 ) + "</td>";
html +="<td align=\"center\" width=213 height=20 colspan=\"3\">" + fields.at( 3 ) + "</td>";
html += "</tr>";
html += "<tr>";
fields.clear();
qDebug() << fields;
fields << QString::fromLocal8Bit( "*****" ) << QString::fromLocal8Bit( "2016-09-20" )
<<QString::fromLocal8Bit( "***" ) <<QString::fromLocal8Bit( "17:12:35" );
qDebug() << fields;
html +="<td width=102 height=20 colspan=\"1\">" + fields.at( 0 ) + "</td>";
html +="<td width=233 height=20 colspan=\"3\">" + fields.at( 1 ) + "</td>";
html +="<td width=95 height=20 colspan=\"1\">" + fields.at( 2 ) + "</td>";
html +="<td width=213 height=20 colspan=\"3\">" + fields.at( 3 ) + "</td>";
html += "</tr>";
html += "<tr>";
fieldname = QString::fromLocal8Bit( "******" );
html += "<td width=102 height=20 colspan=\"1\">" + fieldname + "</td>";
fieldname = QString::fromLocal8Bit( "******" );
html += "<td width=543 height=20 colspan=\"7\">" + fieldname + "</td>";
html += "</tr>";
html += "<tr>";
fields.clear();
qDebug() << fields;
fields << QString::fromLocal8Bit( "*******" ) << QString::fromLocal8Bit( "2016-09-20" )
<<QString::fromLocal8Bit( "*******" ) <<QString::fromLocal8Bit( "17:12:35" );
qDebug() << fields;
html +="<td width=102 height=20 colspan=\"1\">" + fields.at( 0 ) + "</td>";
html +="<td width=233 height=20 colspan=\"3\">" + fields.at( 1 ) + "</td>";
html +="<td width=95 height=20 colspan=\"1\">" + fields.at( 2 ) + "</td>";
html +="<td width=213 height=20 colspan=\"3\">" + fields.at( 3 ) + "</td>";
html += "</tr>";
html += "<tr>";
fields.clear();
qDebug() << fields;
fields << QString::fromLocal8Bit( "********" ) << QString::fromLocal8Bit( "2016-09-20" )
<<QString::fromLocal8Bit( "********" ) <<QString::fromLocal8Bit( "17:12:35" );
qDebug() << fields;
html +="<td width=102 height=20 colspan=\"1\">" + fields.at( 0 ) + "</td>";
html +="<td width=233 height=20 colspan=\"3\">" + fields.at( 1 ) + "</td>";
html +="<td width=95 height=20 colspan=\"1\">" + fields.at( 2 ) + "</td>";
html +="<td width=213 height=20 colspan=\"3\">" + fields.at( 3 ) + "</td>";
html += "</tr>";
html += "<tr>";
fields.clear();
qDebug() << fields;
fields << QString::fromLocal8Bit( "*******" ) << QString::fromLocal8Bit( "0" )
<<QString::fromLocal8Bit( "*******" ) <<QString::fromLocal8Bit( "17:12:35" )
<<QString::fromLocal8Bit( "*******" )<<QString::fromLocal8Bit( ".C" )
<<QString::fromLocal8Bit( "*******" )<<QString::fromLocal8Bit( "88" );
qDebug() << fields;
html +="<td width=102 height=20 colspan=\"1\">" + fields.at( 0 ) + "</td>";
html +="<td width=57 height=20 colspan=\"1\">" + fields.at( 1 ) + "</td>";
html +="<td width=86 height=20 colspan=\"1\">" + fields.at( 2 ) + "</td>";
html +="<td width=92 height=20 colspan=\"1\">" + fields.at( 3 ) + "</td>";
html +="<td width=95 height=20 colspan=\"1\">" + fields.at( 4 ) + "</td>";
html +="<td width=72 height=20 colspan=\"1\">" + fields.at( 5 ) + "</td>";
html +="<td width=92 height=20 colspan=\"1\">" + fields.at( 6 ) + "</td>";
html +="<td width=51 height=20 colspan=\"1\">" + fields.at( 7 ) + "</td>";
html +="</tr></table><br>";
html += "<table bgcolor=skyblue bordercolor=black color=\"black\" align=\"center\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" width=\"645\">";
html += "<tr>";
fieldname = QString::fromLocal8Bit( "*********" );
html += "<td width=\"102\" height=\"20\" colspan=\"1\">" + fieldname + "</td>";
for( int i = 0; i < 7; i++ )
{
html += "<td width=\"77\" height=\"20\" colspan=\"1\"> </td>";
}
html +="</tr></table><br>";
html += "<table align=\"center\" border=\"1\" bordercolor=\"black\" cellspacing=\"0\" cellpadding=\"0\" width=\"645\" height=\"35\">";
html +="<tr>";
fieldname = QString::fromLocal8Bit( "2、******:" );
html += "<td height=\"35\" align=\"left\">" + fieldname + "</td>";
html +="</tr></table><br>";
html += "<table align=\"center\" border=\"1\" bordercolor=\"black\" cellspacing=\"0\" cellpadding=\"0\" width=\"645\" height=\"116\">";
html +="<tr>";
fieldname = QString::fromLocal8Bit( "3、结论及处理意见:" );
html += "<td height=\"116\" align=\"left\" valign=\"top\">" + fieldname + "</td>";
html +="</tr></table>";
fields.clear();
fields << QString::fromLocal8Bit( "******:" ) << QString::fromLocal8Bit( "******:" );
qDebug() << fields;
html += "<table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" width=\"645\">";
html +="<tr>";
html += "<td height=\"42\" width=\"290\" align=\"left\">" + fields.at(0) + "</td>";
html += "<td height=\"42\" width=\"355\" align=\"left\">" + fields.at(1) + "</td>";
html +="</tr></table><br>";
return html;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
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格式啦
html += GeneratePicWord();//该函数同生产pdf文档的一样
QFile outFile("f:/18.doc");
outFile.open(QIODevice::WriteOnly | QIODevice::Append );
QTextStream ts(&outFile);
ts<<html<<endl;
}