DataPlayer/ChaosDataPlayer/WordOperate.cpp
2022-07-30 11:50:01 +08:00

540 lines
19 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/********************************************************************
@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合并起始行从1开始nStartCol合并起始列从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 表格索引从1开始nStartRow行号从1开始nStartCol列号从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);
}
}
}