16,472
社区成员
发帖
与我相关
我的任务
分享
class COPword
{
public:
_Application m_wdApp;
Window mWindowActive; //定义活动窗口对象
Pane mPane; //定义当前窗格对象
View mViewActive; //定义活动视图对象
Documents m_wdDocs;
_Document m_wdDoc;
_Font m_wdFt;
Selection m_wdSel;
Table m_wdTb;
InlineShapes m_wdInlineShapes;
InlineShape m_wdInlineShape;
Shapes m_Shapes;
COleVariant vTrue;
COleVariant vFalse;
COleVariant vOpt;
public:
COPword();
virtual ~COPword();
//**********************创建新文档*******************************************
BOOL CreateApp(); //创建一个新的WORD应用程序
BOOL CreateDocumtent(); //创建一个新的Word文档
BOOL Create(); //创建新的WORD应用程序并创建一个新的文档
void ShowApp(); //显示WORD文档
//**********************文本书写操作*******************************************
void WriteText(CString szText); //写文本
void NewLine(int nLineCount); //回车换N行
void WriteNewLineText(CString szText, int nLineCount = 1); //换N行写字
void WriteHeaderRoot(); //设置页眉页脚
//**********************表格操作*******************************************
void CreateTable(int nRow, int nColumn);
void WriteCellText(int nRow, int nColumne, CString szText); //往表格中写字
void WriteCellNewLineText(int nRow, int nColumne, CString szText, int nLineCount = 1); //表格换N行写字
void New2StringArray(CString** pszArr, int nRow, int nColumn); //创建二维字符串数组
void WriteTableText(CString** pszText, int nRow, int nColumn); //用二维字符串数组填充表格
void SelectCell(int nRow, int nColumn); //选中表格
//*********************字体设置*******************************************
void SetFont(CString szFontName, float fSize = 9, long lFontColor = 0, long lBackColor=0);
void SetFont(BOOL bBold, BOOL bItalic = FALSE, BOOL bUnderLine = FALSE,int i = 2);
void SetTableFont(int nRow, int nColumn, CString szFontName = "宋体", float fSize=9, long lFontColor=0, long lBackColor = 0);
void SetTableFont(int nRow, int nColumn, BOOL bBold, BOOL bItalic = FALSE, BOOL bUnderLine = FALSE);
//*********************格式设置*******************************************
void SetParaphformat(int nAlignment);//文字对齐方法
//*********************标题设置*******************************************
void SetTitle(int nStyle);// -2为一级标题,...,-10为九级标题
//*********************行间距*******************************************
void SetLineSpace(int nDistance);
//*********************保存文档*******************************************
void SaveAs(CString WordName);
//*********************关闭文档*******************************************
void Close();
//*********************插入图片*******************************************
void AddPic(CString FileName);
};
COPword::COPword()
{
}
COPword::~COPword()
{
}
//******word操作方法***************************************************
BOOL COPword::CreateApp() //创建一个新的WORD应用程序
{
if (FALSE == m_wdApp.CreateDispatch("Word.Application"))
{
AfxMessageBox("Application创建失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
// m_wdApp.SetVisible(TRUE);
return TRUE;
}
BOOL COPword::CreateDocumtent() //创建一个新的Word文档
{
if (!m_wdApp.m_lpDispatch)
{
AfxMessageBox("Application为空,Documents创建失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
m_wdDocs.AttachDispatch(m_wdApp.GetDocuments());
if (!m_wdDocs.m_lpDispatch)
{
AfxMessageBox("Documents创建失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
COleVariant varTrue(short(1),VT_BOOL);
CComVariant Template(_T("")); //为了简单,没有使用WORD的文档模板
CComVariant NewTemplate(false),DocumentType(0),Visible;
m_wdDocs.Add(&Template,&NewTemplate,&DocumentType,&Visible);
//得到document和selection变量
m_wdDoc = m_wdApp.GetActiveDocument();
if (!m_wdDoc.m_lpDispatch)
{
AfxMessageBox("Document获取失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
m_wdSel = m_wdApp.GetSelection();
if (!m_wdSel.m_lpDispatch)
{
AfxMessageBox("Select获取失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
return TRUE;
}
BOOL COPword::Create() //创建新的WORD应用程序并创建一个新的文档
{
if (FALSE == CreateApp())
{
return FALSE;
}
return CreateDocumtent();
}
void COPword::ShowApp() //显示WORD文档
{
m_wdApp.SetVisible(TRUE);
}
void COPword::WriteText(CString szText) //写文本
{
SetFont(true,FALSE,FALSE,2);
m_wdSel.TypeText(szText);
}
void COPword::WriteNewLineText(CString szText, int nLineCount ) //换N行写字
{
int i;
if (nLineCount <= 0)
{
nLineCount = 0;
}
for (i = 0; i < nLineCount; i++)
{
m_wdSel.TypeParagraph();
}
}
void COPword::SetFont(CString szFontName ,float fSize, long lFontColor ,long lBackColor)
{
if (!m_wdSel.m_lpDispatch)
{
AfxMessageBox("Select为空,字体设置失败!", MB_OK|MB_ICONWARNING);
return;
}
m_wdSel.SetText("F");
m_wdFt = m_wdSel.GetFont();
m_wdFt.SetSize(fSize);
m_wdFt.SetName(szFontName);
m_wdFt.SetColor(lFontColor);
m_wdSel.SetFont(m_wdFt);
Range r = m_wdSel.GetRange();
r.SetHighlightColorIndex(lBackColor);
}
void COPword::SetFont(BOOL bBold, BOOL bItalic , BOOL bUnderLine,int i)
{
if (!m_wdSel.m_lpDispatch)
{
AfxMessageBox("Select为空,字体设置失败!", MB_OK|MB_ICONWARNING);
return;
}
if(1 == i)
{
m_wdSel.SetText("F");
m_wdFt = m_wdSel.GetFont();
m_wdFt.SetName("黑体");
m_wdFt.SetSize(27);
m_wdSel.SetFont(m_wdFt);
}
if(2 == i)
{
m_wdSel.SetText("F");
m_wdFt = m_wdSel.GetFont();
m_wdFt.SetName("宋体");
m_wdFt.SetBold(bBold);
m_wdFt.SetSize(11);
m_wdSel.SetFont(m_wdFt);
}
if(3 == i)
{
m_wdSel.SetText("F");
m_wdFt = m_wdSel.GetFont();
m_wdFt.SetName("宋体");
m_wdFt.SetSize(12);
m_wdSel.SetFont(m_wdFt);
}
}
void COPword::SetTableFont(int nRow, int nColumn, CString szFontName, float fSize, long lFontColor, long lBackColor)
{
Cell c = m_wdTb.Cell(nRow, nColumn);
c.Select();
_Font ft = m_wdSel.GetFont();
ft.SetName(szFontName);
ft.SetSize(fSize);
ft.SetColor(lFontColor);
m_wdSel.SetFont(ft);
Range r = m_wdSel.GetRange();
r.SetHighlightColorIndex(lBackColor);
}
void COPword::SetTableFont(int nRow, int nColumn, BOOL bBold, BOOL bItalic , BOOL bUnderLine )
{
Cell c = m_wdTb.Cell(nRow, nColumn);
c.Select();
_Font ft = m_wdSel.GetFont();
ft.SetBold(bBold);
ft.SetItalic(bItalic);
ft.SetUnderline(bUnderLine);
m_wdSel.SetFont(ft);
}
void COPword::SetParaphformat(int nAlignment) //文字对齐方法
{
_ParagraphFormat p = m_wdSel.GetParagraphFormat();
p.SetAlignment(nAlignment);
m_wdSel.SetParagraphFormat(p);
}
void COPword::CreateTable(int nRow, int nColumn)
{
m_wdDoc = m_wdApp.GetActiveDocument();
Tables tbs = m_wdDoc.GetTables();
VARIANT vtDefault, vtAuto;
vtDefault.vt = VT_I4;
vtAuto.vt = VT_I4;
vtDefault.intVal = 1;
vtAuto.intVal = 0;
tbs.Add(m_wdSel.GetRange(), nRow, nColumn, &vtDefault, &vtAuto);
m_wdTb = tbs.Item(1);
VARIANT vtstyle;
vtstyle.vt = VT_BSTR;
_bstr_t bstr = "网格型";
vtstyle.bstrVal = bstr;
if (m_wdTb.GetStyle().bstrVal == vtstyle.bstrVal)
{
m_wdTb.SetStyle(&vtstyle);
m_wdTb.SetApplyStyleFirstColumn(TRUE);
m_wdTb.SetApplyStyleHeadingRows(TRUE);
m_wdTb.SetApplyStyleLastColumn(TRUE);
m_wdTb.SetApplyStyleLastRow(TRUE);
}
}
void COPword::WriteCellText(int nRow, int nColumne, CString szText) //往表格中写字
{
Cell c = m_wdTb.Cell(nRow, nColumne);
c.Select();
m_wdSel.TypeText(szText);
}
void COPword::SaveAs(CString WordName)
{
m_wdDoc=m_wdApp.GetActiveDocument(); //得到ActiveDocument
CComVariant FileName(_T(WordName)); //文件名
CComVariant FileFormat(0);
CComVariant LockComments(false),Password(_T(""));
CComVariant AddToRecentFiles(true),WritePassword(_T(""));
CComVariant ReadOnlyRecommended(false),EmbedTrueTypeFonts(false);
CComVariant SaveNativePictureFormat(false),SaveFormsData(false);
CComVariant SaveAsAOCELetter(false);
CComVariant InsertLineBreaks(true),LineEnding(0);
CComVariant AllowSubstitutions(true),AddBiDiMarks(true);
CComVariant Encoding(51936);
m_wdDoc.SaveAs(&FileName,&FileFormat,&LockComments,&Password,
&AddToRecentFiles,&WritePassword,&ReadOnlyRecommended,
&EmbedTrueTypeFonts,&SaveNativePictureFormat,&SaveFormsData,
&SaveAsAOCELetter,&Encoding,&InsertLineBreaks,&AllowSubstitutions,&LineEnding,&AddBiDiMarks);
}
void COPword::Close()
{
m_wdSel.ReleaseDispatch(); //Selection 不用了,一定要释放
m_wdDocs.ReleaseDispatch(); //Documents 也不用了
CComVariant SaveChanges(false),OriginalFormat,RouteDocument;
m_wdApp.Quit(&SaveChanges,&OriginalFormat,&RouteDocument);
m_wdApp.ReleaseDispatch();
}
void COPword::SetTitle(int nStyle) // -2为一级标题,...,-10为九级标题
{
COleVariant vUnit((long)5);
COleVariant vCount((long)2);
COleVariant vExtend((long)2);
COleVariant vStyle1((long)nStyle);// -2为一级标题,...,-10为九级标题
_ParagraphFormat pf = m_wdSel.GetParagraphFormat() ;
pf.SetStyle(&vStyle1);
}
void COPword::SetLineSpace(int nDistance)
{
m_wdSel.WholeStory();
_ParagraphFormat pf=m_wdSel.GetParagraphFormat();
pf.SetLineSpacingRule(nDistance);
CComVariant Unit(1),Count(0),Extend(_T(""));
m_wdSel.MoveRight(&Unit,&Count,&Extend);
}
void COPword::AddPic(CString FileName)
{
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_wdInlineShapes=m_wdSel.GetInlineShapes();
m_wdInlineShape=m_wdInlineShapes.AddPicture(FileName,vFalse,vTrue,vOptional);
// m_Shapes.AddPicture(FileName,vFalse,vTrue,0,0,100,100);
m_wdInlineShape.SetWidth(150);
m_wdInlineShape.SetHeight(150);
}