C++操作word:插入文字、图片、表格,设置样式字体

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

想要控制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
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值