void onPrintBtn<span style="font-family:Arial, Helvetica, sans-serif;">()</span>
{
//1. open SaveDialog and get a file name
if (last_path_.isEmpty())
{
QString path = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).at(0);
QDir::root().mkpath(path);
last_path_ = path;
}
QString default_dir_name = last_path_ + "/" + ui_->leIssueCode->text();
QString fileName = QFileDialog::getSaveFileName(this, "Save File",
default_dir_name,
"Word (*.doc)");
//save the current dir for next time
last_path_ = fileName.left(fileName.lastIndexOf('/'));
if(fileName.isEmpty()) {
qDebug() <<"There isn't a file name input "<<endl;
return;
}
bool isWordext = fileName.endsWith(".doc", Qt::CaseInsensitive);
if(!isWordext) {
qDebug() << "The format of the file name is not .doc"<<endl;
fileName.append(".doc");
qDebug() << "Current file name: " << fileName << endl;
}
//2. Prepare a temp Html file
QTemporaryFile tmpfile;
if (!tmpfile.open()) {
qDebug() << "Opening temp file failed " << endl;
return;
}
QString inputHtml = tmpfile.fileName();
QTextStream outHtml(&tmpfile);
outHtml.setCodec("UTF-8");
QString strhtml;
generateHtml(strhtml);
outHtml << strhtml<< endl;
tmpfile.flush();
//3. create a word App
QAxObject word("Word.Application", 0);
word.setProperty("Visible", false);
QAxObject *documents = word.querySubObject("Documents");
if(!documents) {
qDebug() << "Quering Documents failed " << endl;
return;
}
//create a new document using the html file as a template
documents->dynamicCall("Add(QString)",inputHtml);
QAxObject *document=word.querySubObject("ActiveDocument");
if(!document) {
qDebug() << "Quering ActiveDocument failed " << endl;
return;
}
//save a doc using the input file name
document->dynamicCall("SaveAs (const QString&)", fileName);
//4. exit the word App
document->dynamicCall("Close (boolean)", false);
documents->dynamicCall("Close()");
word.dynamicCall("Quit()");
qDebug() << "The content is saved successfully" << endl;
tmpfile.close();
}
void PrintDialog::onSaveBtn()
{
//1. open SaveDialog and get a file name
if (last_path_.isEmpty())
{
QString path = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).at(0);
QDir::root().mkpath(path);
last_path_ = path;
}
QString default_dir_name = last_path_ + "/" + ui_->leIssueCode->text();
QString fileName = QFileDialog::getSaveFileName(this, "Save File",
default_dir_name,
"Word (*.doc)");
//save the current dir for next time
last_path_ = fileName.left(fileName.lastIndexOf('/'));
if(fileName.isEmpty()) {
qDebug() <<"There isn't a file name input "<<endl;
return;
}
bool isWordext = fileName.endsWith(".doc", Qt::CaseInsensitive);
if(!isWordext) {
qDebug() << "The format of the file name is not .doc"<<endl;
fileName.append(".doc");
qDebug() << "Current file name: " << fileName << endl;
}
//2. Prepare a temp Html file
QTemporaryFile tmpfile;
if (!tmpfile.open()) {
qDebug() << "Opening temp file failed " << endl;
return;
}
QString inputHtml = tmpfile.fileName();
QTextStream outHtml(&tmpfile);
outHtml.setCodec("UTF-8");
QString strhtml;
generateHtml(strhtml);
outHtml << strhtml<< endl;
tmpfile.flush();
//3. create a word App
QAxObject word("Word.Application", 0);
word.setProperty("Visible", false);
QAxObject *documents = word.querySubObject("Documents");
if(!documents) {
qDebug() << "Quering Documents failed " << endl;
return;
}
//create a new document using the html file as a template
documents->dynamicCall("Add(QString)",inputHtml);
QAxObject *document=word.querySubObject("ActiveDocument");
if(!document) {
qDebug() << "Quering ActiveDocument failed " << endl;
return;
}
//save a doc using the input file name
document->dynamicCall("SaveAs (const QString&)", fileName);
//4. exit the word App
document->dynamicCall("Close (boolean)", false);
documents->dynamicCall("Close()");
word.dynamicCall("Quit()");
qDebug() << "The content is saved successfully" << endl;
tmpfile.close();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PS:generateHtml函数得到字符串sample如下:
<html><head> <style>
.div-line{position:absolute; top:160px;} .titlefont{font-size:18;}
.phigh{padding-bottom:10;} .div-title {position:static; top:8px;font-size: 22px;margin-right:180px;} td{padding-left:10px;padding-top:2px;padding-bottom:2px;} br{line-height:10px;}
p{padding-bottom:10;margin:2;} body{font-family:宋体;font-size:16px;}
table{border-style:solid;font-size:16px;border-collapse:collapse;} td textarea{border-style:solid; border-color:#000000;} div#sequence {background-color:#ffff99;height:30px;width:150px;float:left;} div#no
{background-color:#ffff99;height:30px;width:150px;float:right;} </style>
</head> <body>
<table border="0" bordercolor="#FFFFFF" bgcolor="#FFFFFF" bordertype="1" style="margin-bottom:4px;margin-top:12px" align=center width=100%> <tr>
<td width=80% class="phigh">当日序列号:2</td> <td width=20% class="phigh">单号:</td>
</tr> </table> <br />
<div class="div-title"> <b><u><p style="text-align:center; margin-left:180">CNY BOND TRADING (信用买卖)</p></u></b></div> <br/>
<table border="1" cellspacing="0" bordercolor="#000000" bgcolor="#FFFFFF" style="margin-bottom:4px;" width="100%"> <tr>
<td width="50%"> <div>
<p>TRADE DATE</p> <p>成交日期2015-10-26</p>
</div> </td>
<td width="50%"> <div>
<p>TIME</p> <p>时间17:11:00</p>
</div> </td>
</tr> </table>
<br/> <table border="0" bordercolor="#FFFFFF" bgcolor="#FFFFFF" width="100%"> <tbody>
<tr> <td width="50%">
<b>BUYER(买入方)</b> </td>
<td width="50%"> <b>SELLER(卖出方)</b>
</td> </tr>
</tbody></table> <table border="1" cellspacing="0" bordercolor="#000000" bgcolor="#FFFFFF" width="100%"> <tbody><tr>
<td width="50%"> <div>
<p class="phigh">NAME 名称:鹏扬投资北京(焦翠)</p> <p class="phigh">CITY 分行/城市:</p><br/>
</div> </td> <td width="50%">
<div> <p class="phigh">NAME 名称:</p>
<p>CITY 分行/城市:</p> </div>
</td> </tr>
<tr> <td width="50%">
<div> <p>Brokerage 佣金:0.00</p>
<p>Broker 经纪人:傅尚杰sjfu</p> <p>使用以下方式确认交易</p>
<table border="0" bordercolor="#FFFFFF"width="100%" style="padding-left:0px;"> <tr>
<td width="18%" style="padding-left:0px;">直线(</td> <td width="25%" style="padding-left:0px;"></td> <td width="25%" style="padding-left:0px;">) 电话(</td> <td
width="25%" style="padding-left:0px;"></td> <td width="7%" style="padding-left:0px;">)</td> </tr>
<tr> <td width="18%" style="padding-left:0px;">RM(</td> <td width="25%" style="padding-left:0px;"></td> <td width="25%" style="padding-left:0px;">)
QQ(</td> <td width="25%" style="padding-left:0px;"></td> <td width="7%" style="padding-left:0px;">)</td> </tr>
</table> </div>
</td> <td width="50%">
<div> <p>Brokerage 佣金:0.00 </p>
<p>Broker 经纪人:</p> <p>使用以下方式确认交易</p>
<table border="0" bordercolor="#FFFFFF" width="100%" style="padding-left:0px;"> <tr>
<td width="18%" style="padding-left:0px;">直线(</td> <td width="25%" style="padding-left:0px;"></td> <td width="25%" style="padding-left:0px;">) 电话(</td> <td
width="25%" style="padding-left:0px;"></td> <td width="7%" style="padding-left:0px;">)</td> </tr>
<tr> <td width="18%" style="padding-left:0px;">RM(</td> <td width="25%" style="padding-left:0px;"></td> <td width="25%" style="padding-left:0px;">)
QQ(</td> <td width="25%" style="padding-left:0px;"></td> <td width="7%" style="padding-left:0px;">)</td> </tr>
</table> </div>
</td> </tr>
</tbody>
</table><br/> <table border="0" bordercolor="#FFFFFF" bgcolor="#FFFFFF" bordertype="1" style="margin-bottom:4px;margin-top:12px" align=center width=100%> <tr>
<td width=50% class="phigh">CURRENCY 货币</td> <td width=50% class="phigh"><b><u></u></b></td> </tr>
<tr> <td width=50% class="phigh">FACE AMOUNT 券面总额</td> <td width=50% class="phigh"></td>
</tr> <tr>
<td width=50% class="phigh">DIRTY PRICE 全价</td> <td width=50% class="phigh"></td>
</tr> <tr>
<td width=50% class="phigh">CLEAN PRICE 净价</td> <td width=50% class="phigh"></td>
</tr> <tr>
<td width=50% class="phigh">YIELD 收益率</td> <td width=50% class="phigh"></td>
</tr> <tr>
<td width=50% class="phigh">DELIVERY DATE 交割日</td> <td width=50% class="phigh">2015-10-27</td> </tr>
<tr> <td width=50% class="phigh">SETTLEMENT TYPE 结算方式</td> <td width=50% class="phigh"></td>
</tr> <tr>
<td width=50% class="phigh">ISSUE CODE 债券代码</td> <td width=50% class="phigh">119175.SZ</td>
</tr> <tr>
<td width=50% class="phigh">ISSUE 债券名称</td>
<td width=50% class="phigh">科慧04</td>
</tr> <tr>
<td width=50% class="phigh">MATURITY DATE 债券到期日</td> <td width=50% class="phigh">2019-04-23</td> </tr>
<tr> <td width=50% class="phigh">SPECIAL DETAILS 特别细节</td> <td width=50% class="phigh"></td>
</tr> </table>
</body></html>