VC中进行office编程.doc_第1页
VC中进行office编程.doc_第2页
VC中进行office编程.doc_第3页
VC中进行office编程.doc_第4页
VC中进行office编程.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

VC中进行office编程操作word介绍VC中进行office编程的操作word,可以设置文字的样式,新建表格,这里提供给大家一个类CWzjWordOffice:CWzjOffice类,用来对WORD进行简单的操作。读者评分 3 评分次数 1 正文大家先看一下CWzjWordOffice类怎么使用 CWzjWordOffice wd; /定义一个操作word的对象 wd.Create(); /创建一个word应用程序增加一个文档 wd.SetFont(25,魏碑,RGB(128, 0, 0); /设置字体(字号,字体名,颜色) wd.SetFont(1,0,0); /设置字体为粗体,不是斜体,没有下划线 wd.SetParaphformat(1); /设置文字为居中对齐 wd.WriteText(软件工程成绩); /写文字 wd.m_wdSel.TypeParagraph(); /回车换行 /设置表格字体 wd.SetFont(9,宋体); wd.SetFont(0,0,0); wd.CreateTable(2,4); /创建一个2行4列的表格 wd.WriteCellText(1,1,学号); /往1行1列单元写“学号” wd.WriteCellText(1,2,姓名); /往1行2列单元写“姓名” wd.WriteCellText(1,3,年龄); /. wd.WriteCellText(1,4,省份); wd.WriteCellText(2,1,23020723); wd.WriteCellText(2,2,汪自军); wd.WriteCellText(2,3,25); wd.WriteCellText(2,4,湖北); wd.ShowApp(); /显示word后面是效果图。此主题相关图片如下:按此查看图片详细信息正在读取此图片的详细信息,请稍候 .要 在VC中进行OFFICE编程,先要导入OFFICE组件库,方法是:VC类向导增加类From a type library找到D:Program FilesMicrosoft OfficeOFFICE11MSWORD.OLB增加一些类,在WORD中要增加的 _Application,_Document,_Font,_Paragraphformat,要是后面用到不够的话,还可以回来用同样的方法加上。下面是类的两个文件.h文件/ WzjOffice.h: interface for the CWzjOffice class./#if !defined(AFX_WZJOFFICE_H_5E694706_F031_4E02_9674_69A4B7300931_INCLUDED_)#define AFX_WZJOFFICE_H_5E694706_F031_4E02_9674_69A4B7300931_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000#include atlbase.h#include msword.h#include comdef.h#include /COlVirant/* 类名: CWzjOffice,CWzjWordOffice,CWzjExcelOffice 头文件 WzjOffice.h * 描述: Office操作(WORD,EXCEL) 信息: 汪自军 吉林大学 2007.01* 联系: TM: 39600719* blog: /wzj23020723*/=CWzjOffice=class CWzjOffice public: CWzjOffice(); virtual CWzjOffice();/=/=/=CWzjWordOffice=class CWzjWordOffice:CWzjOfficeprivate:public: _Application m_wdApp; Documents m_wdDocs; _Document m_wdDoc; _Font m_wdFt; Selection m_wdSel; Table m_wdTb;public: CWzjWordOffice(); virtual CWzjWordOffice(); /操作/*创建新文档* 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 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); 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);/方法;#endif / !defined(AFX_WZJOFFICE_H_5E694706_F031_4E02_9674_69A4B7300931_INCLUDED_).cpp文件/ WzjOffice.cpp: implementation of the CWzjOffice class./#include stdafx.h#include WzjOffice.h#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE=_FILE_;#define new DEBUG_NEW#endif/ Construction/Destruction/CWzjOffice:CWzjOffice()CWzjOffice:CWzjOffice()/=CWzjWordOffice=CWzjWordOffice:CWzjWordOffice()CWzjWordOffice:CWzjWordOffice()BOOL CWzjWordOffice:CreateApp() if (FALSE = m_wdApp.CreateDispatch(Word.Application) AfxMessageBox(Application创建失败!, MB_OK|MB_ICONWARNING); return FALSE; / m_wdApp.SetVisible(TRUE); return TRUE;BOOL CWzjWordOffice:CreateDocumtent() 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); /*VARIANT vtTtemplate, vtNewTemplate, vtDocType, vtVisible; vtTtemplate.vt = VT_BSTR; _bstr_t bstr = ; vtTtemplate.bstrVal = bstr; vtNewTemplate.vt = VT_BOOL; vtNewTemplate.boolVal = FALSE; vtDocType.vt = VT_I4; vtDocTVal =0; vtVisible.vt = VT_BOOL; vtVisible.boolVal = TRUE; pDocs-Add(&vtTtemplate,&vtNewTemplate,&vtDocType,&vtVisible);*/ 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 CWzjWordOffice:Create() if (FALSE = CreateApp() return FALSE; return CreateDocumtent();void CWzjWordOffice:ShowApp() m_wdApp.SetVisible(TRUE);void CWzjWordOffice:WriteText(CString szText) m_wdSel.TypeText(szText);void CWzjWordOffice:WriteNewLineText(CString szText, int nLineCount /* = 1 */) int i; if (nLineCount = 0) nLineCount = 0; for (i = 0; i nLineCount; i+) m_wdSel.TypeParagraph(); void CWzjWordOffice:SetFont(CString szFontName /*= 宋体*/,float fSize, long lFontColor /*= 0*/,long lBackColor /*= 0*/) 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 CWzjWordOffice:SetFont(BOOL bBold, BOOL bItalic /* = FALSE */, BOOL bUnderLine /* = FALSE */) if (!m_wdSel.m_lpDispatch) AfxMessageBox(Select为空,字体设置失败!, MB_OK|MB_ICONWARNING); return; m_wdSel.SetText(F); m_wdFt = m_wdSel.GetFont(); m_wdFt.SetBold(bBold); m_wdFt.SetItalic(bItalic); m_wdFt.SetUnderline(bUnderLine); m_wdSel.SetFont(m_wdFt);void CWzjWordOffice: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 CWzjWordOffice:SetTableFont(int nRow, int nColumn, BOOL bBold, BOOL bItalic /* = FALSE */, BOOL bUnderLine /* = FALSE */) 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 CWzjWordOffice:SetParaphformat(int nAlignment) _Paragraphformat p = m_wdSel.GetParagraphformat(); p.SetAlignment(nAlignment); m_wdSel.SetParagraphformat(p);void CWzjWordOffice:CreateTable(int nRow, int nColumn) m_wdDoc = m_wdApp.GetActiveDocument(); T

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论