MFC下使用C++操作Word文档

        由于项目需求,在MFC环境下操作将数据和图片写入word文档,之前没有接触过,网上查阅了很多资料,终于把任务完成。理解的不是很透彻,但是这些东西应该够用了,写篇博客记录一下,以便以后再用到。

      (1)VS环境下创建一个MFC应用程序,然后在项目上单击右键,选择  类向导-----添加类----类型库中的MFC类。在弹出的对话框中选择文件,位置选择office安装目录下的MSWORD.OLB文件,然后从接口中选择需要的类,可以选择全部接口,然后生成全部类,点击完成。

    (2)将生成的所有头文件中的导入MSWORD.OLB的代码注释掉。

//#import "C:\\Program Files\\Microsoft Office\\Office14\\MSWORD.OLB" no_namespace
//注意每个头文件中的此句话都要注释

    (3)接下来就可以写操作Word文档的代码了。需要用到哪个类就引入哪个类的头文件。我这里引入了以下头文件:

#include "CApplication.h"
#include "CBookmark.h"
#include "CBookmarks.h"
#include "CCell.h"
#include "CCells.h"
#include "CColumn.h"
#include "CColumns.h"
#include "CDocument.h"
#include "CDocuments.h"
#include "CRange.h"
#include "CRow.h"
#include "CRows.h"
#include "CSelection.h"
#include "CTable.h"
#include "CTables.h"
#include "CnlineShape.h"
#include "CnlineShapes.h"
#include "CFont.h"
#include "CParagraphs.h"
#include "CBorder.h"
#include "CBorders.h"
#include "CParagraph.h"

    (4)现在可以开始操作word文档了。操作word文档有两种方式,第一种是先制作好一个word模板,在模板中添加标签,然后通过标签将数据写入word。第二种就是直接使用代码动态创建word文档。第一种方式比较简单。

      4.1  第一种方式,使用word模板

              4.1.1 打开word,编辑好自己的模板,向需要添加数据的地方添加标签,然后保存为.dot文件。

             4.1.2 接下来开始向书签处写入数据。

COleVariant covTrue((short)TRUE),
        covFalse((short)FALSE),
        covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR),
        dot(_T("c:\\Template.dot"));

CApplication wordApp;//创建word应用程序
if (!wordApp.CreateDispatch(_T("Word.Application")))
{
   AfxMessageBox(_T("请先安装Office软件!"));
   return;
}
wordApp.put_Visible(TRUE);//设置word可见,这样写入数据的过程我们就能直接看到
CDocuments docs = wordApp.get_Documents();//创建word文档集
CDocument docx = docs.Add(dot, covOptional, covOptional, covOptional);
CBookmarks bookmarks= docx.get_Bookmarks();

//在标签处插入文字
CBookmark bookmark = bookmarks.Item(&_variant_t(_T("标题")));
range = bookmark.get_Range();
range.put_Text(_T("四川省沐川县及昭觉县典型乡镇1:5万土地质量地球化学调查"));

//在标签处插入图片
bookmark = bookmarks.Item(&_variant_t(_T("图片")));
range = bookmark.get_Range();	
CnlineShapes ishaps = range.get_InlineShapes();
CnlineShape ishap = ishaps.AddPicture(_T("C:\\out.JPG"), covFalse, covTrue, covOptional);

//保存word文件
CString strSavePath = _T("C:");
strSavePath += _T("\\test.docx");
docx.SaveAs(COleVariant(strSavePath), covOptional, covOptional, covOptional, covOptional,
        covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);

// 退出word应用
docx.Close(covFalse, covOptional, covOptional);
wordApp.Quit(covOptional, covOptional, covOptional);
range.ReleaseDispatch();
bookmarks.ReleaseDispatch();
wordApp.ReleaseDispatch();

AfxMessageBox(_T("word生成成功!"));

4.2  第二种方式:用代码动态创建word文档

//1.开启word
CApplication word_app;
	if (!word_app.CreateDispatch(_T("Word.Application")))
	{
		AfxMessageBox(_T("本机没有安装word产品!"));
		return;
	}
	word_app.put_Visible(true);//设置word可见

//2.新建文档
	COleVariant vTrue((short)TRUE), vFalse((short)FALSE), VOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
	CDocuments docs;
	CDocument0 doc;
	docs = word_app.get_Documents();
	doc = docs.Add(new CComVariant(_T("")), new CComVariant(false), new CComVariant(0), new CComVariant());

//3.开始向word写入数据
	CSelection sel = word_app.get_Selection();
	
	CParagraphs wordParagraphs = sel.get_ParagraphFormat();
	wordParagraphs.put_Alignment(1);//设置文字居中
	
	CFont font = sel.get_Font();
	font.put_Bold(1);
	font.put_Size(15);
	font.put_Name(_T("宋体"));//设置字体属性
	sel.TypeText(TEXT("四川省沐川县及昭觉县典型乡镇1:5万土地质量地球化学调查\n")); 
	sel.TypeText(TEXT("野外工作航迹图(2019)"));

	CTables tables = doc.get_Tables();
	CTable table= tables.Add(sel.get_Range(),4, 2, new CComVariant(), new CComVariant());//创建4行2列的表格
	CBorders borders = table.get_Borders();
	borders.put_InsideLineStyle(1);
	borders.put_OutsideLineStyle(1);//设置表格边框,默认没有边框
	CCell cell=	table.Cell(1,2);
	cell.Merge(table.Cell(2,2));
	cell.Merge(table.Cell(3, 2));
	CCell cell2 = table.Cell(4, 1);
	cell2.Merge(table.Cell(4,2));
	cell2.put_Height(400);//设置单元格的高

	wordParagraphs.put_Alignment(0);//设置文字居左
	font.put_Size(12);
	CCell cell3= table.Cell(1, 1);
	cell3.Select();
	sel.TypeText(_T("日期:2019-08-31"));
	sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
	wordParagraphs.put_Alignment(0);
	font.put_Size(12);
	sel.TypeText(_T("编  号:GPS5313-20190831"));
	sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
	wordParagraphs.put_Alignment(0);
	font.put_Size(12);
	sel.TypeText(_T("GPS坐标数据文件号:5313-01"));
	
	CCell cell4 = table.Cell(1,2);
	cell4.Select();
	cell4.put_VerticalAlignment(1);//设置单元格上下居中
	wordParagraphs.put_Alignment(1);//设置水平居中
	font.put_Size(12);
	sel.TypeText(_T("工作单位:中国地质科学院地球物理地球化学勘查研究所"));

    //插入图片
	sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
	CnlineShapes ishapes = doc.get_InlineShapes();
	CnlineShape ishape = ishapes.AddPicture(_T("D:\\timg.jpg"), vFalse,vTrue, VOpt);
//4.保存并退出
	doc.SaveAs(COleVariant(_T("D:\\野外航迹图.doc")), VOpt, VOpt, VOpt, VOpt,VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt);
	doc.Close(vFalse, VOpt,VOpt);
	word_app.Quit(VOpt, VOpt, VOpt);

结果图:

 

 

 

 

 

 

 

 

 

  • 16
    点赞
  • 101
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值