DataPlayer/ChaosDataPlayer/WordOperate.cpp

540 lines
19 KiB
C++
Raw Normal View History

2022-07-30 11:50:01 +08:00
/********************************************************************
@date: 2017-11-20
@author: gongwei@vishee.com
@edit_by
@brief: word通用操作类
*********************************************************************/
#include "WordOperate.h"
#include "qt_windows.h"
WordOperate::WordOperate(QObject *parent):QObject(parent),
m_bOpened(false),
m_wordDocuments(NULL),
m_wordWidget(NULL),
m_selection(NULL)
{
}
WordOperate::WordOperate(const QString &strFile, QObject *parent):QObject(parent),
m_bOpened(false),
m_wordDocuments(NULL),
m_wordWidget(NULL),
m_selection(NULL),
m_strFilePath(strFile)
{
}
WordOperate::~WordOperate()
{
close();
}
/******************************************************************************
* open
*
* bVisable
* bool
*****************************************************************************/
bool WordOperate::open(bool bVisable)
{
//新建一个word应用程序,并设置为可见
m_wordWidget = new QAxObject();
bool bFlag = m_wordWidget->setControl( "word.Application" );
// bool bFlag = false;//使用wps打开报告
if(!bFlag)
{
// 用wps打开
bFlag = m_wordWidget->setControl( "kwps.Application" );
if(!bFlag)
{
return false;
}
}
m_wordWidget->setProperty("Visible", false);
//获取所有的工作文档
QAxObject *document = m_wordWidget->querySubObject("Documents");
if(!document)
{
return false;
}
//以文件template.dot为模版新建一个文档
document->dynamicCall("Add(QString)", m_strFilePath);
//获取当前激活的文档
m_wordDocuments = m_wordWidget->querySubObject("ActiveDocument");
m_selection = m_wordWidget->querySubObject("Selection");
m_selection->dynamicCall("InsertAfter(QString&)", "\n");
m_bOpened = true;
return m_bOpened;
}
/******************************************************************************
* open
*
* strFilePath bVisable
* bool
*****************************************************************************/
bool WordOperate::open(const QString& strFilePath, bool bVisable)
{
m_strFilePath = strFilePath;
close();
return open(bVisable);
}
/******************************************************************************
* close
*
*
* bool
*****************************************************************************/
bool WordOperate::close()
{
if (m_bOpened)
{
m_wordDocuments->dynamicCall("Close (bool)", false);
m_wordWidget->dynamicCall("Quit()");
m_bOpened = false;
delete m_wordWidget;
m_wordWidget = NULL;
}
return m_bOpened;
}
/******************************************************************************
* isOpen
*
*
* bool
*****************************************************************************/
bool WordOperate::isOpen()
{
return m_bOpened;
}
/******************************************************************************
* save
*
*
* void
*****************************************************************************/
void WordOperate::save()
{
m_wordDocuments->dynamicCall("Save()");
}
/********************************************************************
description:
:
:
********************************************************************/
void WordOperate::Print()
{
m_wordDocuments->dynamicCall("PrintOut()");
}
void WordOperate::Print(int nPageFrom,int nPageTo)
{
QList<QVariant> vars;
vars.push_back(QVariant(true));//background
vars.push_back(QVariant(false));//append
vars.push_back(QVariant("wdPrintRangeOfPages"));//range
vars.push_back(QVariant(""));//OutputFileName
vars.push_back(QVariant(nPageFrom));//From
vars.push_back(QVariant(nPageTo));//To
vars.push_back(QVariant("_wdPrintDocumentContent"));//Item
vars.push_back(QVariant(1));//Copy
vars.push_back(QVariant("1"));//Pages
vars.push_back(QVariant("_wdPrintAllPages"));//PageType
vars.push_back(QVariant(false));//PrintToFile
vars.push_back(QVariant(true));//Collate
vars.push_back(QVariant(""));//FileName
vars.push_back(QVariant(false));//ManualDuplexPrint
vars.push_back(QVariant(0));//PrintZoomCulumn
vars.push_back(QVariant(0));//PrintZoomRow
vars.push_back(QVariant(0));//PrintZoomPaperWidth
vars.push_back(QVariant(0));//PrintZoomPaperHeight
m_wordDocuments->dynamicCall("PrintOut(bool,bool,const QString&,const QString&,int,int,const QString&,int,const QString&,const QString&,bool,bool,const QString&,bool,int,int,int,int)",vars);
}
/******************************************************************************
* saveAs
*
* strSaveFile
* void
*****************************************************************************/
bool WordOperate::saveAs(const QString& strSaveFile)
{
return m_wordDocuments->dynamicCall("SaveAs (const QString&)",
strSaveFile).toBool();
}
/******************************************************************************
* intsertTable
*
* row hang; column
* void
*****************************************************************************/
void WordOperate::intsertTable(int row, int column)
{
m_selection->querySubObject("ParagraphFormat")->dynamicCall("Alignment", "wdAlignParagraphCenter");
//selection->dynamicCall("TypeText(QString&)", "Table Test");//设置标题
QAxObject *range = m_selection->querySubObject("Range");
QAxObject *tables = m_wordDocuments->querySubObject("Tables");
QAxObject *table = tables->querySubObject("Add(QVariant,int,int)",range->asVariant(),row,column);
for(int i=1;i<=6;i++)
{
QString str = QString("Borders(-%1)").arg(i);
QAxObject *borders = table->querySubObject(str.toLatin1().data());
borders->dynamicCall("SetLineStyle(int)",1);
}
}
/******************************************************************************
* setCellString
*
* nTable ; row ; column ; text
* void
*****************************************************************************/
QAxObject* WordOperate::setCellString(int nTable, int row, int column, const QString& text)
{
QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
QAxObject* table = pTables->querySubObject("Item(int)", nTable);
if(table)
{
table->querySubObject("Cell(int, int)", row, column)
->querySubObject("Range")
->dynamicCall("SetText(QString)", text);
return table->querySubObject("Cell(int, int)", row, column);
}
}
/******************************************************************************
* insertCellPic
*
* nTable ; row ; column ; picPath
* void
*****************************************************************************/
void WordOperate::insertCellPic(int nTable, int row, int column,
const QString& picPath)
{
QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
QAxObject* table = pTables->querySubObject("Item(int)", nTable);
QAxObject* range = table->querySubObject("Cell(int, int)", row, column)
->querySubObject("Range");
range->querySubObject("InlineShapes")
->dynamicCall("AddPicture(const QString&,QVariant&)",picPath,"False");
}
void WordOperate::SetMarkPic(QString sLabel, QString sFile)
{
QAxObject*bookmark_pic=m_wordDocuments->querySubObject("Bookmarks(QVariant)","pic");
if(!bookmark_pic->isNull())
{
bookmark_pic->dynamicCall("Select(void)");
QAxObject *range;
range = bookmark_pic->querySubObject("Range");
QVariant tmp = range->asVariant();
QList<QVariant>qList;
qList<<QVariant("E://WorkSpace//QT//ChaosDataPlayerDemo//images//logo.png");
qList<<QVariant(false);
qList<<QVariant(true);
qList<<tmp;
QAxObject *Inlineshapes = m_wordDocuments->querySubObject("InlineShapes");
Inlineshapes->dynamicCall("AddPicture(const QString&, QVariant, QVariant ,QVariant)",qList);
}
}
/********************************************************************
description:
: nTable 1
nStartRow合并起始行1nStartCol合并起始列1
nEndRow合并终止行nEndCol合并终止列
:
********************************************************************/
void WordOperate::MergeCell(int nTable, int nStartRow, int nStartCol, int nEndRow, int nEndCol,bool bVerticalCenter)
{
QAxObject* tables = m_wordDocuments->querySubObject("Tables");
QAxObject* table = tables->querySubObject("Item(int)",nTable);
QAxObject* StartCell =table->querySubObject("Cell(int, int)",nStartRow,nStartCol);
QAxObject* EndCell = table->querySubObject("Cell(int, int)",nEndRow,nEndCol);
StartCell->querySubObject("Merge(QAxObject *)",EndCell->asVariant());
//设置对齐方式 -上下对齐
if(bVerticalCenter)
StartCell->dynamicCall("VerticalAlignment", "wdCellAlignVerticalCenter");
else
{
m_selection->querySubObject("ParagraphFormat")->dynamicCall("Alignment", "wdAlignParagraphJustify");
StartCell->dynamicCall("VerticalAlignment", "wdCellAlignVerticalTop");
}
}
/********************************************************************
description:
:
: nTable 1nStartRow行号1nStartCol列号1
nRow为要拆分的行数nCol为要拆分的列数
********************************************************************/
void WordOperate::SplitCell(int nTable, int nStartRow, int nStartCol, int nRow, int nCol)
{
QAxObject* tables = m_wordDocuments->querySubObject("Tables");
QAxObject* table = tables->querySubObject("Item(int)",nTable);
QAxObject* StartCell =table->querySubObject("Cell(int, int)",nStartRow,nStartCol);
StartCell->querySubObject("Split(int,int)",nRow,nCol);
}
/********************************************************************
description:
: nAlign对齐方式 0- 1 2
:
********************************************************************/
void WordOperate::SetAlign(AlignmentType nAlign)
{
switch(nAlign)
{
case 0:
m_selection->querySubObject("ParagraphFormat")->dynamicCall("Alignment", "wdAlignParagraphLeft");
break;
case 1:
m_selection->querySubObject("ParagraphFormat")->dynamicCall("Alignment", "wdAlignParagraphCenter");
break;
case 2:
m_selection->querySubObject("ParagraphFormat")->dynamicCall("Alignment", "wdAlignParagraphRight");
break;
}
}
void WordOperate::SetTableFont(QAxObject* cell, QString strFamily, int nSize, bool bBold, bool bItalic)
{
QAxObject *font = cell->querySubObject("Select");
m_selection->querySubObject("Font")->setProperty("Name", strFamily);
m_selection->querySubObject("Font")->setProperty("Size", nSize);
m_selection->querySubObject("Font")->setProperty("Bold", bBold);
m_selection->querySubObject("Font")->setProperty("Italic", bItalic);
m_selection->dynamicCall("MoveRight(const QString&,int)", "wdCharacter",1);
}
/********************************************************************
description: Table cell字体
:
: nTable
row
column
strFamily
nSize
bBold
bItalic
bool 线
********************************************************************/
void WordOperate::SetTableFont(int nTable,int row,int column,QString strFamily,int nSize,bool bBold ,bool bItalic,bool bUnderLine)
{
QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
QAxObject* table = pTables->querySubObject("Item(int)", nTable);
if(table)
{
QAxObject* range = table->querySubObject("Cell(int, int)", row, column)
->querySubObject("Range");
range->querySubObject("Font")
->setProperty("Name", strFamily);
range->querySubObject("Font")->setProperty("Size", nSize);
range->querySubObject("Font")->setProperty("Bold", bBold);
range->querySubObject("Font")->setProperty("Italic", bItalic);
if(bUnderLine)
range->querySubObject("Font")->setProperty("Underline",2);
else
range->querySubObject("Font")->setProperty("Underline",0);
//为Table的一行设置字体
// QAxObject* rowRange = table->querySubObject("Rows(int)",row)->querySubObject("Select");
// m_selection->querySubObject("Font")->setProperty("Name", strFamily);
// m_selection->querySubObject("Font")->setProperty("Size", nSize);
// m_selection->querySubObject("Font")->setProperty("Bold", bBold);
// m_selection->querySubObject("Font")->setProperty("Italic", bItalic);
}
}
/********************************************************************
description:
: strFamily nSize bBold bItalic
:
********************************************************************/
void WordOperate::SetFont(QString strFamily, int nSize, bool bBold, bool bItalic,bool bUnderLine)
{
m_selection->querySubObject("Font")->setProperty("Name", strFamily);
m_selection->querySubObject("Font")->setProperty("Size", nSize);
m_selection->querySubObject("Font")->setProperty("Bold", bBold);
m_selection->querySubObject("Font")->setProperty("Italic", bItalic);
if(bUnderLine)
m_selection->querySubObject("Font")->setProperty("Underline",2);
else
m_selection->querySubObject("Font")->setProperty("Underline",0);
}
void WordOperate::InsertPicture(QString picturePath)
{
m_selection->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)",picturePath);
}
void WordOperate::moveForEnd()//光标移到末尾
{
QAxObject* selection = m_wordWidget->querySubObject("Selection");
QVariantList params;
params.append(6);
params.append(0);
selection->dynamicCall("EndOf(QVariant&, QVariant&)", params).toInt();
}
void WordOperate::insertEnter()
{
QAxObject* selection = m_wordWidget->querySubObject("Selection");
selection->dynamicCall("TypeParagraph(void)");
}
void WordOperate::moveRight()
{
QAxObject* selection = m_wordWidget->querySubObject("Selection");
selection->dynamicCall("MoveRight(int)",1);
}
void WordOperate::SetText(QString strText)
{
m_selection->dynamicCall("TypeText(QString&)", strText);
}
void WordOperate::GetPageMargin(double &dbLeft, double &dbTop, double &dbRight, double &dbBottom)
{
QAxObject* pageSetup = m_selection->querySubObject("PageSetup");
dbLeft = pageSetup->property("LeftMargin").toDouble();
dbTop = pageSetup->property("TopMargin").toDouble();
dbRight = pageSetup->property("RightMargin").toDouble();
dbBottom = pageSetup->property("BottomMargin").toDouble();
}
void WordOperate::SetVisible(bool bVisable)
{
m_wordWidget->setProperty("Visible", bVisable);
}
void WordOperate::SetPageMargin(double dbLeft, double dbTop, double dbRight, double dbBottom)
{
QAxObject* pageSetup = m_selection->querySubObject("PageSetup");
pageSetup->setProperty("LeftMargin",dbLeft);
pageSetup->setProperty("RightMargin",dbRight);
pageSetup->setProperty("TopMargin",dbTop);
pageSetup->setProperty("BottomMargin",dbBottom);
}
bool WordOperate::SetTableRowHeight(int nTable,int row,int height)
{
bool flag=false;
QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
QAxObject* table = pTables->querySubObject("Item(int)", nTable);
if(table)
{
QAxObject* cell =table->querySubObject("Cell(int, int)",row,1);
flag = cell->setProperty("HeightRule", "wdRowHeightExactly");//wdRowHeightAtLeast wdRowHeightExactly wdRowHeightAuto
if(cell)
flag = cell->setProperty("Height", height);
}
return flag;
}
void WordOperate::SetParagraphSpacing(SpaceType type,int space)
{
QAxObject* para = m_selection->querySubObject("ParagraphFormat");
switch (type) {
case Space1:
para->setProperty("LineSpacingRule","wdLineSpaceSingle");
break;
case Space15:
para->setProperty("LineSpacingRule","wdLineSpace1pt5");
break;
case Space2:
para->setProperty("LineSpacingRule","wdLineSpaceDouble");
break;
case SpaceAtLeast:
para->setProperty("LineSpacingRule","wdLineSpaceAtLeast");
para->setProperty("LineSpacing",space);
break;
case SpaceExactly:
para->setProperty("LineSpacingRule","wdLineSpaceExactly");
para->setProperty("LineSpacing",space);
break;
case SpaceMuti:
para->setProperty("LineSpacingRule","wdLineSpaceMultiple");
para->setProperty("LineSpacing",space);
break;
default:
break;
}
}
void WordOperate::SetColumnWidth(int nTable, int column, int width)
{
QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
QAxObject* table = pTables->querySubObject("Item(int)", nTable);
if(table)
{
QAxObject* colum = table->querySubObject("Columns(int)",column);
if(colum)
colum->setProperty("Width",width);
}
}
void WordOperate::HideBorder(int nTable, int row, int column, int item)
{
QAxObject* pTables = m_wordDocuments->querySubObject( "Tables" );
QAxObject* table = pTables->querySubObject("Item(int)", nTable);
QAxObject* cell =table->querySubObject("Cell(int, int)",row,column);
QAxObject* border = cell->querySubObject("Borders(int)",item);
if(border)
border->setProperty("Visible",false);
/*
QAxObject* Borders = table->querySubObject("Borders");
Borders->setProperty("InsideLineStyle",1);
Borders->setProperty("OutsideLineStyle",1);
*/
}
void WordOperate::AddTableRow(int tableIndex ,int nRow,int rowCount)
{
QAxObject* tables=m_wordDocuments->querySubObject("Tables");
QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
QAxObject* rows =table->querySubObject("Rows");
int Count =rows->dynamicCall("Count").toInt();
if(0< nRow && nRow <= Count )
{
for(int i =0; i< rowCount; ++i)
{
QString sPos = QString("Item(%1)").arg(nRow+i);
QAxObject* row= rows->querySubObject(sPos.toStdString().c_str());
QVariant param =row ->asVariant();
rows->dynamicCall("Add(Variant)",param);
}
}
}