// 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);
}
- 1
- 2
前往页