c++ word文档操作

c++ 专栏收录该内容
6 篇文章 0 订阅

本程序叙述了vc针对word程序的一些简单操作,程序经测试正常可用

程序包含两个.h文件和一个.cpp文件,import了3个库

具体代码如下:

loadLibraries.h

#pragma region Import the type libraries
#import "C:\\Program Files (x86)\\Common Files\microsoft shared\\OFFICE14\\MSO.DLL" \
rename("RGB", "MSORGB") \
rename("DocumentProperties", "MSODocumentProperties")


#import "C:\\Program Files (x86)\\Common Files\\microsoft shared\\VBA\\VBA6\\VBE6EXT.OLB"

#import "C:\\Program Files (x86)\\Microsoft Office\\Office14\\MSWORD.OLB" \
rename("ExitWindows", "WordExitWindows") \
rename("FindText", "WordFindText")

using namespace Office;

using namespace VBIDE;


ReportManager.h

#include <Windows.h>
#include <stdio.h>
#include "loadLibraries.h"
class ReportManager
{
public:
ReportManager();
virtual ~ReportManager(){};
BOOL InitCom();//初始化com库
BOOL CreateWordApplication();//创建word app
BOOL CreateWordDocument();//创建文档
void CreateFont();//设子字体
void SetPageStyle();//设置页面风格
void InserWordTxt();//插入文字
void InsertTextStatic(); //插入文本框
void InsertTable();//插入表格
void InsertImagie();//插入图片
void SaveAndExit();
DWORD GetModuleDirectory(LPWSTR pszDir, DWORD nSize);
void ExitReportManager();
void InsetDataIntoTableTitle();
protected:
private:
Word::_ApplicationPtr m_app;     //创建一个word应用
Word::_DocumentPtr m_doucumnet;//创建一个文档
Word::SelectionPtr m_sel;//选择要编辑的对象
Word::TablePtr m_tal;//表对象
Word::PageSetupPtr m_page;//页面
Word::_FontPtr m_font;//字体
Word::RangePtr m_rang;


float page_width; //页面宽度
float page_height;//页面高度
};


ReportManager.cpp



#include "ReportManager.h"
ReportManager::ReportManager()
{


}
BOOL ReportManager::InitCom()
{
if (::CoInitialize(NULL) != S_OK)
{
 return FALSE;
}


return TRUE;   


}
BOOL ReportManager::CreateWordApplication()
{
LPDISPATCH pDisp;
LPUNKNOWN pUnk;
CLSID clsid;


if (GetActiveObject(clsid ,NULL,&pUnk) == S_OK)
{
return FALSE;
}


HRESULT hr = m_app.CreateInstance(__uuidof(Word::Application));
if (FAILED(hr))
{
return FALSE;
}
return TRUE;
}
BOOL ReportManager::CreateWordDocument()
{
Word::DocumentsPtr  docs=m_app->GetDocuments();
m_doucumnet = docs->Add();
if (!m_doucumnet)
{
return FALSE;
}
return TRUE;
}
void ReportManager::SetPageStyle()
{
m_page = m_doucumnet->GetPageSetup();
page_width =m_page->GetPageWidth();
page_height = m_page->GetPageHeight();


//设置文档边距
m_page->PutTopMargin(page_height/10);
m_page->PutBottomMargin(page_height/10);
m_page->PutRightMargin(page_width/10);
m_page->PutLeftMargin(page_width/10);


Word::ParagraphsPtr paragraphs;//段落
paragraphs = m_doucumnet->GetParagraphs();
Word::ParagraphPtr   m_paragraph= paragraphs->GetLast();


m_rang = m_paragraph->GetRange();
}
void ReportManager::CreateFont()
{
m_sel = m_app->GetSelection();
m_font = m_sel->GetFont();
m_font->PutName(_bstr_t("宋体"));
}
void ReportManager::InserWordTxt()
{
 
// Word::_ParagraphFormatPtr parafmt = m_sel->GetParagraphFormat();
// parafmt->PutLineSpacingRule(Word::wdLineSpaceExactly);
// parafmt->PutLineSpacing(50);
// m_font = m_sel->GetFont();
// m_font->PutBold(1);
// m_font->PutColor(Word::wdColorGreen);
// m_font->PutSize(20);
// m_font->PutName(_bstr_t("宋体"));
// m_sel->TypeText(_bstr_t("Abc"));


}
void ReportManager::InsertTextStatic()
{
Word::ShapesPtr shapes = m_doucumnet->GetShapes();
Word::ShapePtr textbox= shapes->AddTextbox(Office::msoTextOrientationHorizontal,1,1,100,100);
}
void ReportManager::SaveAndExit()
{
wchar_t szFileName[MAX_PATH];
if (!GetModuleDirectory(szFileName, ARRAYSIZE(szFileName)))
{
_putws(L"GetModuleDirectory failed");
return;
}


wcsncat_s(szFileName, ARRAYSIZE(szFileName), L"test.doc", 12);



variant_t vtFileName(szFileName);


m_doucumnet->SaveAs(&vtFileName);
m_app->PutVisible(VARIANT_TRUE);
m_app->Quit();
ExitReportManager();


}
//为了得到完整的文件目录
DWORD ReportManager::GetModuleDirectory(LPWSTR pszDir, DWORD nSize)
{
nSize = GetModuleFileName(NULL, pszDir, nSize);
if (!nSize || GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
*pszDir = L'\0'; 
return 0;
}


for (int i = nSize - 1; i >= 0; i--)
{
if (pszDir[i] == L'\\' || pszDir[i] == L'/')
{
pszDir[i + 1] = L'\0';
nSize = i + 1;
break;
}
}
return nSize;
}


void ReportManager::InsertTable()
{
Word::TablesPtr tables = m_sel->GetTables();
m_tal = tables->Add(m_sel->GetRange(),9,2);
Word::BordersPtr bords=m_tal->GetBorders();



bords->PutOutsideLineStyle(Word::wdLineStyleSingle);
bords->PutInsideLineStyle(Word::wdLineStyleSingle);
bords->PutOutsideLineWidth(Word::wdLineWidth025pt);
bords->PutEnable(1);
m_rang = m_tal->GetRange();


//对表格整体设计
//Word::CellsPtr cells = m_rang->GetCells();


//合并第一行表格
Word::CellPtr cell;
cell = m_tal->Cell(1,1);
cell->Merge(m_tal->Cell(1,2));


cell = m_tal->Cell(6,1);
cell->Merge(m_tal->Cell(6,2));
cell->PutHeight(page_height/4);
cell = m_tal->Cell(7,1);
cell->Merge(m_tal->Cell(7,2));

cell->PutHeight(page_height/4);


float width=cell->GetWidth();


//设置表格宽度
m_tal->Cell(2,1)->SetWidth(width/5,Word::wdAdjustSameWidth);
m_tal->Cell(3,1)->SetWidth(width/5,Word::wdAdjustSameWidth);
m_tal->Cell(4,1)->SetWidth(width/5,Word::wdAdjustSameWidth);
m_tal->Cell(5,1)->SetWidth(width/5,Word::wdAdjustSameWidth);
m_tal->Cell(8,1)->SetWidth(width/5,Word::wdAdjustSameWidth);
m_tal->Cell(9,1)->SetWidth(width/5,Word::wdAdjustSameWidth);
}
void ReportManager::InsetDataIntoTableTitle()
{
Word::CellPtr cell = m_tal->Cell(1,1);
cell->PutVerticalAlignment(Word::wdCellAlignVerticalCenter);
Word::RangePtr rang=cell->GetRange();


rang->PutBold(1); //设置表格种字体为黑体
rang->PutText("a");
cell = m_tal->Cell(2,1);

rang = cell->GetRange();
rang->PutText("b:");

rang =m_tal->Cell(3,1)->GetRange();
rang->PutBold(1);
rang->PutText("c:");




rang= m_tal->Cell(4,1)->GetRange();
rang->PutBold(1);
rang->PutText("d:");




rang = m_tal->Cell(5,1)->GetRange();
rang->PutBold(1);
rang->PutText("e:");




rang = m_tal->Cell(8,1)->GetRange();
rang->PutBold(1);
rang->PutText("f:");




rang = m_tal->Cell(9,1)->GetRange();
rang->PutBold(1);
rang->PutText("g:");


}


void ReportManager::InsertImagie()
{
  Word::CellPtr cell=m_tal->Cell(6,1);
  Word::RangePtr range = cell->GetRange();
Word::ShapesPtr shapes = m_doucumnet->GetShapes();
Word::InlineShapesPtr inLines = m_sel->GetRange()->GetInlineShapes();




VARIANT vt1;
vt1.vt = VT_I4;
vt1.lVal = (long)0;
VARIANT vt2;
vt2.vt=VT_I4;
vt2.lVal =(long)1;
VARIANT vt3;
vt3.vt = VT_DISPATCH;
vt3.pdispVal = range;
char ch[15]="F:\\meinv.jpg";
Word::InlineShapePtr inLine = inLines->AddPicture(ch,&vt1,&vt2,&vt3);




Word::ShapePtr shape = inLine->ConvertToShape();
float w=cell->GetWidth()-10;
float h=cell->GetHeight()-10;
shape->PutWidth(w);
shape->PutHeight(h);

// if (shape->GetType() == Office::msoPicture)
// {
// Word::WrapFormatPtr wrap=shape->GetWrapFormat();
// wrap->PutType(Word::wdWrapBehind);
// }


}
void ReportManager::ExitReportManager()
{
CoUninitialize();
}


int main()
{
ReportManager report;
  report.InitCom();
  report.CreateWordApplication();
  report.CreateWordDocument();
  report.SetPageStyle();
  report.CreateFont();
  report.InserWordTxt();
  report.InsertTable();
  report.InsetDataIntoTableTitle();
  report.InsertImagie();
  report.SaveAndExit();
getchar();
return 0;
}

程序实现如下


其实还有一种方法,是将loadLibraries.h文件编译后,会生成3个.tlh(头文件)文件和3个tli(实现文件)文件,有了这几个文件就可以不用import库,直接在程序中包含这几个头文件就可以了

  • 2
    点赞
  • 1
    评论
  • 17
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

yufengaotianya

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值