• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            codeArt

            codeArt

            c++操作word接口

            注意事項(xiàng):1多用Range,少用Selection,因?yàn)閃ord中,Selection對(duì)象只有一個(gè)。
            【1】開啟word
            _ApplicationPtr word_app;
            HRESULT hr 
            = word_app.CreateInstance("Word.Application", NULL); 
            【2】新建一個(gè)文檔
            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】設(shè)置文檔的頁面布局
            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, 11100100);
            【6】文本框中插入文本
             1ShapesPtr shapes = doc->GetShapes();
             2ShapePtr textbox =  shapspp->AddTextbox(Office::msoTextOrientationHorizontal, 11100100);
             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】設(shè)置文本框的屬性
            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】設(shè)置欄目個(gè)數(shù)和欄目的寬度
            這里一定要注意add函數(shù)的邏輯
            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(), 25);
             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);
            在word優(yōu)秀的構(gòu)架中還有許許多多的接口,上面只是舉例實(shí)現(xiàn)一個(gè)普通的文檔,希望對(duì)大家有用。

            posted on 2010-08-31 19:35 codeArt 閱讀(9030) 評(píng)論(4)  編輯 收藏 引用 所屬分類: COM

            評(píng)論

            # re: c++操作word接口 2010-10-26 17:51 tch

            so good ,3q
              回復(fù)  更多評(píng)論   

            # re: c++操作word接口 2012-02-29 22:25 徐世富

            你用的是word哪個(gè)版本?生成app后,如果沒有裝.net還需要哪些dll,如果本身開著一個(gè)word,保存的時(shí)候會(huì)不會(huì)出現(xiàn)無法保存的問題,出現(xiàn)rfc服務(wù)器錯(cuò)誤
            希望能發(fā)我郵箱543483382@qq.com,謝謝
              回復(fù)  更多評(píng)論   

            # re: c++操作word接口[未登錄] 2012-08-07 17:35 劉偉

            太好了。多謝博主  回復(fù)  更多評(píng)論   

            # re: c++操作word接口 2014-04-21 17:08 仔仔

            vs2010怎么使用能不能提供一下啊!我的郵箱是min0073@163.com  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2010年9月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章檔案

            編程與開源

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久青青草原精品国产软件| 一本色道久久综合狠狠躁| 国产精品va久久久久久久| 亚洲国产天堂久久久久久| 久久婷婷人人澡人人爽人人爱| 久久精品国产亚洲av水果派| 久久一区二区免费播放| 久久久无码一区二区三区| 国产毛片久久久久久国产毛片| 狠狠色狠狠色综合久久| 亚洲国产成人久久综合一| 精品人妻伦九区久久AAA片69| 国产精品日韩深夜福利久久| 亚洲愉拍99热成人精品热久久 | 日本福利片国产午夜久久| 婷婷久久综合九色综合绿巨人| 久久精品9988| 久久午夜无码鲁丝片| 欧美伊人久久大香线蕉综合69| 99久久人妻无码精品系列| 狠狠色婷婷久久综合频道日韩| 99久久婷婷国产一区二区| 99国产欧美精品久久久蜜芽| 欧美精品乱码99久久蜜桃| 国产女人aaa级久久久级| 国产精品视频久久久| 国产偷久久久精品专区| 亚洲国产成人精品女人久久久| 久久91这里精品国产2020| 久久精品国产99国产精品澳门 | 久久精品国产亚洲av水果派| 久久这里都是精品| 亚洲国产成人精品女人久久久| 亚洲一区二区三区日本久久九| 99久久超碰中文字幕伊人| 久久亚洲AV成人无码国产| 日韩精品久久久久久免费| 欧美黑人又粗又大久久久| 久久久久久久亚洲Av无码| 久久er99热精品一区二区| 7777久久亚洲中文字幕|