通过QT 转换html文件为word文档


void onPrintBtn()
{


	//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 "<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 "< return;
}
bool isWordext = fileName.endsWith(".doc", Qt::CaseInsensitive);
if(!isWordext) {
qDebug() << "The format of the file name is not .doc"< 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如下:


       

        
当日序列号:2单号:
 
 

CNY   BOND   TRADING (信用买卖)

 
         
 
 

TRADE DATE

 

成交日期2015-10-26

 
 
 
 

TIME

 

时间17:11:00

 
 
 
          
 BUYER(买入方)  SELLER(卖出方)  
                
 
 

NAME 名称:鹏扬投资北京(焦翠)

 

CITY 分行/城市:


 
 
 
 

NAME 名称:

 

CITY 分行/城市:

 
 
 
 

Brokerage 佣金:0.00

 

Broker 经纪人:傅尚杰sjfu

 

使用以下方式确认交易

                   
直线() 电话()
RM()  QQ()
 
 
 
 

Brokerage 佣金:0.00

 

Broker 经纪人:

 

使用以下方式确认交易

                   
直线() 电话()
RM()  QQ()
 
 

                                                                   
CURRENCY 货币
FACE AMOUNT 券面总额
DIRTY PRICE 全价
CLEAN PRICE 净价
YIELD 收益率
DELIVERY DATE 交割日2015-10-27
SETTLEMENT TYPE 结算方式
ISSUE CODE 债券代码119175.SZ
ISSUE 债券名称科慧04
MATURITY DATE 债券到期日2019-04-23
SPECIAL DETAILS 特别细节
   

你可能感兴趣的:(C++,QT)