日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++操作word接口

發布時間:2023/12/29 c/c++ 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++操作word接口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

注意事項:1多用Range,少用Selection,因為Word中,Selection對象只有一個。
【1】開啟word
_ApplicationPtr?word_app;
HRESULT?hr?
=?word_app.CreateInstance("Word.Application",?NULL);?【2】新建一個文檔
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】設置文檔的頁面布局
PageSetupPtr?page_setup?=?doc->GetPageSetup();
page_setup
->PutTopMargin(0);
page_setup
->PutBottomMargin(0);
page_setup
->PutRightMargin(0);
page_setup
->PutLeftMargin(0);【4】插入文本
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】插入文本框
ShapesPtr?shapes?=?doc->GetShapes();
ShapePtr?textbox?
=??shapspp->AddTextbox(Office::msoTextOrientationHorizontal,?1,?1,?100,?100);【6】文本框中插入文本
?1ShapesPtr?shapes?=?doc->GetShapes();
?2ShapePtr?textbox?=??shapspp->AddTextbox(Office::msoTextOrientationHorizontal,?1,?1,?100,?100);
?3TextFramePtr?textframe?=?textbox->GetTextFrame();
?4RangePtr?range?=?textframe->GetTextRange();
?5long?insert_before_count?=?range->Characters->GetCount();
?6range->InsertAfter("TEXT");
?7if?(insert_before_count?!=?1)
?8????range->MoveStart(COleVariant((long)Word::wdCharacter),?COleVariant(insert_before_count-1));
?9if(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】設置文本框的屬性
1textbox->GetTextFrame()->PutAutoSize(Office::msoAutoShape);
2textbox->GetTextFrame()->PutMarginBottom(0);
3textbox->GetTextFrame()->PutMarginTop(0);
4textbox->GetTextFrame()->PutMarginLeft(0);
5textbox->GetTextFrame()->PutMarginRight(0);
6textbox->GetLine()->PutVisible(Office::msoFalse);
7textbox->GetFill()->PutTransparency(1);【8】插入圖片,這里需要注意,必須得先用InlineShape,這樣可以把圖片插入到指定的頁中,不然,所有的圖片只在第一頁。
?1ShapesPtr?shapes?=?m_WordDoc->GetShapes();
?2InlineShapesPtr?inline_shapes?=?selection_doc->GetRange()->GetInlineShapes();
?3InlineShapePtr?inline_shape?=?inline_shapes->AddPicture(“picture_path”,?COleVariant((long)0),?COleVariant((long)1));
?4ShapePtr?shape?=?inline_shape->ConvertToShape();
?5shape->PutWidth(width);
?6shape->PutHeight(hehight());
?7shape->PutTop(Y);
?8shape->PutLeft(X);
?9if(shape->GetType()?==?Office::msoPicture)
10{
11????Word::WrapFormatPtr?wrapp?=?shape->GetWrapFormat();
12????wrapp->PutType(Word::wdWrapBehind);
13}
14【9】插入直線
1ShapesPtr?shapes?=?doc->GetShapes();
2Word::ShapePtr?line?=?shapes->AddLine(x1,y1,?x2,y2);
3if?(line->GetType()?==?Office::msoLine)
4{
5????Word::LineFormatPtr?LineFmt?=?line->GetLine();
6????LineFmt->PutWeight(lr->weight_);
7}【10】插入分隔符
selection->InsertBreak(COleVariant((long)wdColumnBreak));
selection
->InsertBreak(COleVariant((long)wdSectionBreakContinuous));
selection
->InsertBreak(COleVariant((long)wdPageBreak));
【11】設置欄目個數和欄目的寬度
這里一定要注意add函數的邏輯
1SectionsPtr?word_sections?=?doc->GetSections();
2long?num?=?word_sections->GetCount();
3SectionPtr?word_section?=?word_sections->Item(num-1);
4PageSetupPtr?page_setup?=?word_section->GetPageSetup();
5TextColumnsPtr?text_cols?=?page_setup>GetTextColumns();
6text_cols->PutEvenlySpaced(0);
7text_cols->Add(COleVariant(col_width),?COleVariant((long)0),?COleVariant((long)false));【12】插入表格
?1TablesPtr?tables?=?oSel->GetTables();????
?2TablePtr?table?=?tables->Add(oSel->GetRange(),?2,?5);
?3????
?4BordersPtr?bords?=?table->GetBorders();
?5bords->PutOutsideLineStyle(wdLineStyleSingle);
?6bords->PutInsideLineStyle(wdLineStyleSingle);
?7for?(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
15CellPtr?cell?=?table->Cell(1,1);
16cell->Merge(table->Cell(1,2));【13】保存文檔并退出
1COleVariant?vTrue((short)TRUE),vFalse((short)FALSE),vOpt((long)DISP_E_PARAMNOTFOUND,?VT_ERROR);
2_DocumentPtr?active_doc;?
3active_doc?=?word_app->GetActiveDocument();
4active_doc->SaveAs(COleVariant("D:\\doc1.doc"),?
5???????????????????COleVariant((short)0),
6???????????????????vFalse,?COleVariant(""),?vTrue,?COleVariant(""),
7???????????????????vFalse,?vFalse,?vFalse,?vFalse,?vFalse);
8word_app->Quit(vOpt,?vOpt,?vOpt);

?

轉自:http://www.cppblog.com/codeart/archive/2010/08/31/125430.aspx

總結

以上是生活随笔為你收集整理的c++操作word接口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。