c++操作word接口
注意事项:1多用Range,少用Selection,因为Word中,Selection对象只有一个。【1】开启word
_ApplicationPtr word_app;
HRESULT hr = word_app.CreateInstance( " Word.Application " , NULL);
【2】新建一个文档
HRESULT hr = word_app.CreateInstance( " Word.Application " , NULL);
COleVariant vTrue((
short
)TRUE),vFalse((
short
)FALSE),vOpt((
long
)DISP_E_PARAMNOTFOUND, VT_ERROR);
DocumentsPtr docs;
_DocumentPtr oDoc;
docs = word_app -> GetDocuments();
doc = docs -> Add(vOpt, vOpt, vOpt, vOpt);
【3】设置文档的页面布局
DocumentsPtr docs;
_DocumentPtr oDoc;
docs = word_app -> GetDocuments();
doc = docs -> Add(vOpt, vOpt, vOpt, vOpt);
PageSetupPtr page_setup
=
doc
->
GetPageSetup();
page_setup -> PutTopMargin( 0 );
page_setup -> PutBottomMargin( 0 );
page_setup -> PutRightMargin( 0 );
page_setup -> PutLeftMargin( 0 );
【4】插入文本
page_setup -> PutTopMargin( 0 );
page_setup -> PutBottomMargin( 0 );
page_setup -> PutRightMargin( 0 );
page_setup -> PutLeftMargin( 0 );
SelectionPtr selection;
selection = word_app -> GetSelection();
_ParagraphFormatPtr parafmt = selection -> GetParagraphFormat();
parafmt -> PutLineSpacingRule(wdLineSpaceExactly);
parafmt -> PutLineSpacing( 50 );
_FontPtr font;
font = oSel -> GetFont();
font -> PutBold( 1 );
font -> PutColor(wdColorGreen);
font -> PutSize( 20 );
font -> PutName(_T( " 宋体 " ));
selection -> TypeText( " ABC " );
oSel -> TypeParagraph();
oSel -> TypeText( " 12345678901234567890 " );
oSel -> TypeParagraph();
【5】插入文本框
selection = word_app -> GetSelection();
_ParagraphFormatPtr parafmt = selection -> GetParagraphFormat();
parafmt -> PutLineSpacingRule(wdLineSpaceExactly);
parafmt -> PutLineSpacing( 50 );
_FontPtr font;
font = oSel -> GetFont();
font -> PutBold( 1 );
font -> PutColor(wdColorGreen);
font -> PutSize( 20 );
font -> PutName(_T( " 宋体 " ));
selection -> TypeText( " ABC " );
oSel -> TypeParagraph();
oSel -> TypeText( " 12345678901234567890 " );
oSel -> TypeParagraph();
ShapesPtr shapes
=
doc
->
GetShapes();
ShapePtr textbox = shapspp -> AddTextbox(Office::msoTextOrientationHorizontal, 1 , 1 , 100 , 100 );
【6】文本框中插入文本
ShapePtr textbox = shapspp -> AddTextbox(Office::msoTextOrientationHorizontal, 1 , 1 , 100 , 100 );
1
ShapesPtr shapes
=
doc
->
GetShapes();
2 ShapePtr textbox = shapspp -> AddTextbox(Office::msoTextOrientationHorizontal, 1 , 1 , 100 , 100 );
3 TextFramePtr textframe = textbox -> GetTextFrame();
4 RangePtr range = textframe -> GetTextRange();
5 long insert_before_count = range -> Characters -> GetCount();
6 range -> InsertAfter( " TEXT " );
7 if (insert_before_count != 1 )
8 range -> MoveStart(COleVariant(( long )Word::wdCharacter), COleVariant(insert_before_count - 1 ));
9 if (range != 0 )
10 {
11 _FontPtr font = range->GetFont();
12 font->PutBold(isBold);
13 font->PutItalic(isItalic);
14 font->PutColor((Word::WdColor)FontColor());
15 font->PutSize(FontSize);
16 font->PutName(FontType().c_str());
17}
【7】设置文本框的属性
2 ShapePtr textbox = shapspp -> AddTextbox(Office::msoTextOrientationHorizontal, 1 , 1 , 100 , 100 );
3 TextFramePtr textframe = textbox -> GetTextFrame();
4 RangePtr range = textframe -> GetTextRange();
5 long insert_before_count = range -> Characters -> GetCount();
6 range -> InsertAfter( " TEXT " );
7 if (insert_before_count != 1 )
8 range -> MoveStart(COleVariant(( long )Word::wdCharacter), COleVariant(insert_before_count - 1 ));
9 if (range != 0 )
10 {
11 _FontPtr font = range->GetFont();
12 font->PutBold(isBold);
13 font->PutItalic(isItalic);
14 font->PutColor((Word::WdColor)FontColor());
15 font->PutSize(FontSize);
16 font->PutName(FontType().c_str());
17}
1
textbox
->
GetTextFrame()
->
PutAutoSize(Office::msoAutoShape);
2 textbox -> GetTextFrame() -> PutMarginBottom( 0 );
3 textbox -> GetTextFrame() -> PutMarginTop( 0 );
4 textbox -> GetTextFrame() -> PutMarginLeft( 0 );
5 textbox -> GetTextFrame() -> PutMarginRight( 0 );
6 textbox -> GetLine() -> PutVisible(Office::msoFalse);
7 textbox -> GetFill() -> PutTransparency( 1 );
【8】插入图片,这里需要注意,必须得先用InlineShape,这样可以把图片插入到指定的页中,不然,所有的图片只在第一页。
2 textbox -> GetTextFrame() -> PutMarginBottom( 0 );
3 textbox -> GetTextFrame() -> PutMarginTop( 0 );
4 textbox -> GetTextFrame() -> PutMarginLeft( 0 );
5 textbox -> GetTextFrame() -> PutMarginRight( 0 );
6 textbox -> GetLine() -> PutVisible(Office::msoFalse);
7 textbox -> GetFill() -> PutTransparency( 1 );
1
ShapesPtr shapes
=
m_WordDoc
->
GetShapes();
2 InlineShapesPtr inline_shapes = selection_doc -> GetRange() -> GetInlineShapes();
3 InlineShapePtr inline_shape = inline_shapes -> AddPicture(“picture_path”, COleVariant(( long ) 0 ), COleVariant(( long ) 1 ));
4 ShapePtr shape = inline_shape -> ConvertToShape();
5 shape -> PutWidth(width);
6 shape -> PutHeight(hehight());
7 shape -> PutTop(Y);
8 shape -> PutLeft(X);
9 if (shape -> GetType() == Office::msoPicture)
10 {
11 Word::WrapFormatPtr wrapp = shape->GetWrapFormat();
12 wrapp->PutType(Word::wdWrapBehind);
13}
14
【9】插入直线
2 InlineShapesPtr inline_shapes = selection_doc -> GetRange() -> GetInlineShapes();
3 InlineShapePtr inline_shape = inline_shapes -> AddPicture(“picture_path”, COleVariant(( long ) 0 ), COleVariant(( long ) 1 ));
4 ShapePtr shape = inline_shape -> ConvertToShape();
5 shape -> PutWidth(width);
6 shape -> PutHeight(hehight());
7 shape -> PutTop(Y);
8 shape -> PutLeft(X);
9 if (shape -> GetType() == Office::msoPicture)
10 {
11 Word::WrapFormatPtr wrapp = shape->GetWrapFormat();
12 wrapp->PutType(Word::wdWrapBehind);
13}
14
1
ShapesPtr shapes
=
doc
->
GetShapes();
2 Word::ShapePtr line = shapes -> AddLine(x1,y1, x2,y2);
3 if (line -> GetType() == Office::msoLine)
4 {
5 Word::LineFormatPtr LineFmt = line->GetLine();
6 LineFmt->PutWeight(lr->weight_);
7}
【10】插入分隔符
2 Word::ShapePtr line = shapes -> AddLine(x1,y1, x2,y2);
3 if (line -> GetType() == Office::msoLine)
4 {
5 Word::LineFormatPtr LineFmt = line->GetLine();
6 LineFmt->PutWeight(lr->weight_);
7}
selection
->
InsertBreak(COleVariant((
long
)wdColumnBreak));
selection -> InsertBreak(COleVariant(( long )wdSectionBreakContinuous));
selection -> InsertBreak(COleVariant(( long )wdPageBreak));
selection -> InsertBreak(COleVariant(( long )wdSectionBreakContinuous));
selection -> InsertBreak(COleVariant(( long )wdPageBreak));
【11】设置栏目个数和栏目的宽度
这里一定要注意add函数的逻辑
1
SectionsPtr word_sections
=
doc
->
GetSections();
2 long num = word_sections -> GetCount();
3 SectionPtr word_section = word_sections -> Item(num - 1 );
4 PageSetupPtr page_setup = word_section -> GetPageSetup();
5 TextColumnsPtr text_cols = page_setup > GetTextColumns();
6 text_cols -> PutEvenlySpaced( 0 );
7 text_cols -> Add(COleVariant(col_width), COleVariant(( long ) 0 ), COleVariant(( long ) false ));
【12】插入表格
2 long num = word_sections -> GetCount();
3 SectionPtr word_section = word_sections -> Item(num - 1 );
4 PageSetupPtr page_setup = word_section -> GetPageSetup();
5 TextColumnsPtr text_cols = page_setup > GetTextColumns();
6 text_cols -> PutEvenlySpaced( 0 );
7 text_cols -> Add(COleVariant(col_width), COleVariant(( long ) 0 ), COleVariant(( long ) false ));
1
TablesPtr tables
=
oSel
->
GetTables();
2 TablePtr table = tables -> Add(oSel -> GetRange(), 2 , 5 );
3
4 BordersPtr bords = table -> GetBorders();
5 bords -> PutOutsideLineStyle(wdLineStyleSingle);
6 bords -> PutInsideLineStyle(wdLineStyleSingle);
7 for ( int i = 1 ; i <= 2 ; i ++ )
8 {
9 for (int j = 1; j<=5; j++)
10 {
11 table->Cell(i,j)->GetRange()->PutText("20");
12 }
13}
14
15 CellPtr cell = table -> Cell( 1 , 1 );
16 cell -> Merge(table -> Cell( 1 , 2 ));
【13】保存文档并退出
2 TablePtr table = tables -> Add(oSel -> GetRange(), 2 , 5 );
3
4 BordersPtr bords = table -> GetBorders();
5 bords -> PutOutsideLineStyle(wdLineStyleSingle);
6 bords -> PutInsideLineStyle(wdLineStyleSingle);
7 for ( int i = 1 ; i <= 2 ; i ++ )
8 {
9 for (int j = 1; j<=5; j++)
10 {
11 table->Cell(i,j)->GetRange()->PutText("20");
12 }
13}
14
15 CellPtr cell = table -> Cell( 1 , 1 );
16 cell -> Merge(table -> Cell( 1 , 2 ));
1
COleVariant vTrue((
short
)TRUE),vFalse((
short
)FALSE),vOpt((
long
)DISP_E_PARAMNOTFOUND, VT_ERROR);
2 _DocumentPtr active_doc;
3 active_doc = word_app -> GetActiveDocument();
4 active_doc -> SaveAs(COleVariant( " D:\\doc1.doc " ),
5 COleVariant(( short ) 0 ),
6 vFalse, COleVariant( "" ), vTrue, COleVariant( "" ),
7 vFalse, vFalse, vFalse, vFalse, vFalse);
8 word_app -> Quit(vOpt, vOpt, vOpt);
在word优秀的构架中还有许许多多的接口,上面只是举例实现一个普通的文档,希望对大家有用。
2 _DocumentPtr active_doc;
3 active_doc = word_app -> GetActiveDocument();
4 active_doc -> SaveAs(COleVariant( " D:\\doc1.doc " ),
5 COleVariant(( short ) 0 ),
6 vFalse, COleVariant( "" ), vTrue, COleVariant( "" ),
7 vFalse, vFalse, vFalse, vFalse, vFalse);
8 word_app -> Quit(vOpt, vOpt, vOpt);