注:http://qt-project.org/doc/qt-4.8/richtext-html-subset.html#table-cell-attributes
QTextDocument *textdocument = newQTextDocument();
QStringqstrHTMLAbsolutePath = "***.html";
//创建一个包含HTML的字符串
QString qstrHTML =".............";
//将字符串写入文件
textdocument->setHtml(qstrHTML);
//输出HTML格式
ExportHtml(qstrHTMLAbsolutePath,textdocument);
//输出HTML格式
boolQHTMLClass::ExportHtml(constQString &filePath,QTextDocument *document)
{
if(filePath.endsWith(".htm") ||filePath.endsWith(".html"))
{
// 通过实例化一个QFile打开文件,然后通过QTextStream对象存取它
QFilefile(filePath);
if(file.open(QIODevice::WriteOnly|QIODevice::Text))
{
// QTextStream用来读写文本数据
QTextStreamout(&file);
// 对于HTML文件,推荐使用"utf-8"编码
out.setCodec("utf-8");
out<<document->toHtml("utf-8");
file.close();
returntrue;
}
returnfalse;
}
return false;
}