1.讀取Excel表中信息,在for循環中寫入word文檔中
QAxWidget *word=new QAxWidget("Word.Application", 0, Qt::MSWindowsOwnDC);
word->setProperty("Visible", true);// 獲取所有的工作文檔
QAxObject * documents = word->querySubObject("Documents");
QAxObject excel("Excel.Application");
excel.setProperty("Visible",false);
QAxObject *workbooks = excel.querySubObject("WorkBooks");
workbooks->dynamicCall("Open (const QString&)",QString("G:/Vs_product/ReadExcel/Excel/11.xlsx"));
QAxObject *workbook = excel.querySubObject("ActiveWorkBook"); //獲取活動工作簿
QAxObject *worksheets = workbook->querySubObject("WorkSheets"); //獲取所有的工作表
int intCount = worksheets->property("Count").toInt(); //表的數目
qDebug()<<"表的數目:"<<intCount;
QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)",1); //獲取第一個工作表
QAxObject *used_range = worksheet->querySubObject("UsedRange"); //獲得利用的範圍
QAxObject *rows = used_range->querySubObject("Rows");
QAxObject *columns = used_range->querySubObject("Columns");
int row_start = used_range->property("Row").toInt(); //獲得開始行
int column_start = used_range->property("Column").toInt(); //獲得開始列
int row_count = rows->property("Count").toInt(); //獲得行數
int column_count = columns->property("Count").toInt(); //獲得列數
qDebug()<<"第一張表如下:";
for(int i = row_start;i<=row_count;i++){
// 以文件template.doc爲模版新建一個文檔
documents->dynamicCall("Add(QString)",QString::fromLocal8Bit("G:/Vs_product/ReadExcel/word/Id2.doc"));
// 獲取當前激活的文檔
QAxObject *document=word->querySubObject("ActiveDocument");
QAxObject *cell1 = worksheet->querySubObject("Cells(int,int)",i,3);
if (cell1->dynamicCall("Value2()").toString() ==NULL)
{
break;
}
for(int j = column_start;j<=column_count;j++){
QAxObject *cell = worksheet->querySubObject("Cells(int,int)",i,j);
if (j==3)
{
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", cell->dynamicCall("Value2()").toString());
}
else
{
break;
}
}
if (j==4)
{
QAxObject *bookmark_ndvi = document->querySubObject("Bookmarks(QVariant)", "ndvi");
if ( !bookmark_ndvi->isNull())
{
bookmark_ndvi->dynamicCall("Select(void)");
bookmark_ndvi->querySubObject("Range")->setProperty("Text", cell->dynamicCall("Value2()").toString());
}
else
{
break;
}
}
if (j==5)
{
QAxObject *bookmark_pic = document->querySubObject("Bookmarks(QVariant)", "pic");
if ( !bookmark_pic->isNull())
{
bookmark_pic->dynamicCall("Select(void)");
bookmark_pic->querySubObject("Range")->setProperty("Text", cell->dynamicCall("Value2()").toString());
}
else
{
break;
}
}
if (j==6)
{
QAxObject *bookmark_pic2 = document->querySubObject("Bookmarks(QVariant)", "pic2");
if ( !bookmark_pic2->isNull())
{
bookmark_pic2->dynamicCall("Select(void)");
bookmark_pic2->querySubObject("Range")->setProperty("Text", cell->dynamicCall("Value2()").toString());
}
else
{
break;
}
}
if (j==3 ||j==4 || j==5 || j==6)
{
qDebug()<<i<<j<<cell->dynamicCall("Value2()").toString();
}
}
QString str=QString("G:/Vs_product/ReadExcel/word/企業往來詢證函(積極式)")+QString::number(i)+QString(".doc");
document->dynamicCall("SaveAs (const QString&)", str);
document->dynamicCall("Close(boolean)", false);
qDebug()<<endl;
}
word->dynamicCall("Quit()");
excel.dynamicCall("Quit(void)"); //實現對文件的釋放