想要控制word文档,向word写入文字,图片,表格等并控制其格式,可以引入第三方库,#include "msword.h",网上的资料很多,在此不再赘述。下面封装好的代码,
,很简单,可以直接调用:
// WordHandle.h: interface for the WordHandle class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_WORDHANDLE_H__7F5096E5_F09B_4DA1_9FC2_28FA2D2C1573__INCLUDED_)
#define AFX_WORDHANDLE_H__7F5096E5_F09B_4DA1_9FC2_28FA2D2C1573__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//段落对齐方式
enum wdAlignParagraphAlignment {
wdAlignParagraphLeft = 0, //左对齐
wdAlignParagraphCenter = 1, //居中
wdAlignParagraphRight = 2, //右对齐
wdAlignParagraphJustify = 3 //两端对齐
};
//单元格垂直对齐方式
enum WdCellVerticalAlignment{
wdCellAlignVerticalTop = 0, //文字与单元格上框线对齐
wdCellAlignVerticalCenter = 1, //文字与单元格中心对齐
wdCellAlignVerticalBottom = 3 //文字与单元格底边框线对齐
};
#include "msword.h"
#include <atlbase.h>
#include "comdef.h"
#include <afxdisp.h> //COlVirant
class WordHandle
{
private:
_Application wordapp;
COleVariant vTrue, vFalse, vOptional;
Documents worddocs;
CComVariant tpl, Visble, DocType, NewTemplate;
Selection wordsel;
_Document worddoc;
Tables wordtables;
Table wordtable;
_ParagraphFormat paragraphformat;
Cell cell;
Cells cells;
_Font wordFt;
Range range;
CComVariant SaveChanges,OriginalFormat,RouteDocument;
CComVariant ComVariantTrue,ComVariantFalse;
InlineShapes ishaps;
InlineShape ishap;
public:
void AddPicture(LPCTSTR picname);
void AddTime(CTime time);
void CreateChart();
BOOL MergeCell(int cell1row,int cell1col,int cell2row,int cell2col);
void CloseWordSave(LPCTSTR wordname);
void CloseWord();
void SetTableFormat(int nRow, int nColumn, long horizontalAlignment, long verticalAlignment);
void SetTableFormat(int beginRow, int beginColumn, int endRow, int endColumn, long horizontalAlignment = wdAlignParagraphCenter, long verticalAlignment =
wdCellAlignVerticalCenter);
void SetTablePadding(float topPadding = 4.0, float bottomPadding = 4.0, float leftPadding = 4.0, float rightPadding = 4.0);
void SetTableFont(int nRow, int nColumn, BOOL bBold, BOOL bItalic = FALSE, BOOL bUnderLine = FALSE);
void SetTableFont(int nRow, int nColumn, LPCTSTR szFontName = "宋体", float fSize=9, long lFontColor=0, long lBackColor = 0);
void SetTableFont(int beginRow, int beginColumn, int endRow, int endColumn, LPCTSTR szFontName = "宋体", float fSize=9, long lFontColor=0, long lBackColor =
0);
void SetFont(BOOL bBold, BOOL bItalic = FALSE, BOOL bUnderLine = FALSE );
void SetFont(LPCTSTR szFontName ,float fSize = 9, long lFontColor = 0, long lBackColor=0);
void AddParagraph();
void WriteText(LPCTSTR szText);
BOOL CreateWord();
void ShowWord(BOOL show);
void WriteCell(int nRow, int nColumne, LPCTSTR szText);
void WriteCell(int nRow, int nColumne, LPCTSTR lpszFormat, float num);
BOOL CreateTable(int nRow, int nColumn);
void SetParagraphFormat(int nAlignment);
BOOL CreateDocumtent();
BOOL CreateApp();
WordHandle();
virtual ~WordHandle();
};
#endif // !defined(AFX_WORDHANDLE_H__7F5096E5_F09B_4DA1_9FC2_28FA2D2C1573__INCLUDED_)
下面是实现文件:
// WordHandle.cpp: implementation of the WordHandle class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "WordHandle.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
WordHandle::WordHandle(){
OleInitialize(NULL);
//CoInitialize(NULL);
ComVariantTrue=CComVariant(true);
ComVariantFalse=CComVariant(false);
vTrue=COleVariant((short)TRUE);
vFalse=COleVariant ((short)FALSE);
tpl=CComVariant(_T(""));
DocType=CComVariant(0);
NewTemplate=CComVariant(false);
vOptional=COleVariant((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
}
WordHandle::~WordHandle(){
wordtable.ReleaseDispatch();
wordtables.ReleaseDispatch();
wordsel.ReleaseDispatch();
worddocs.ReleaseDispatch();
worddoc.Rele

本文介绍了如何使用C++控制Word文档,包括插入文字、图片和表格,并详细讲述了设置文档格式的步骤。通过引用第三方库`msword.h`,可以实现对Word的API操作。文中还分享了一个简单的VS2010项目,演示了如何通过DAO连接ACCESS数据库生成Word文档。此外,还提供了利用MFC类向导生成操作Word接口文件的方法,并提及该方法同样适用于Excel和其他Office文件。
最低0.47元/天 解锁文章
2860

被折叠的 条评论
为什么被折叠?



