Qt实现读取Excel表,并将读取的信息写进word文档中
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)"); //实现对文件的释放