VC中进行Office编程

来源:互联网 发布:油蜡皮沙发价格知乎 编辑:程序博客网 时间:2024/03/29 17:35
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 Files/Microsoft Office/OFFICE11/MSWORD.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 <afxdisp.h> //COlVirant
/**********************************************************************************
*        类名:    CWzjOffice,CWzjWordOffice,CWzjExcelOffice   头文件 WzjOffice.h    
*        描述:    Office操作(WORD,EXCEL)                            信息:    汪自军   吉林大学    2007.01
*        联系:    wzj23020723@163.com  TM: 39600719
*        blog:    www.blog.163.com/wzj23020723
*
************************************************************************************/

//============================CWzjOffice=============================================
class CWzjOffice  
{
public:
   CWzjOffice();
   virtual ~CWzjOffice();
};
//===================================================================================


//===================================================================================
//===========================CWzjWordOffice==========================================
class CWzjWordOffice:CWzjOffice
{
private:
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_FILE
static 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;
   vtDocType.intVal =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();
   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 CWzjWordOffice::WriteCellText(int nRow, int nColumne, CString szText)
{
   Cell c = m_wdTb.Cell(nRow, nColumne);
   c.Select();
   m_wdSel.TypeText(szText);
}