日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

WORD自動化部分操作

系統 2766 0

為了實現在WORD中添加文本和圖片,在網上找了一下,找到了一些資源,自己組織了一下,形成一個簡單的類,一些方法是利用模板的,一些是不用的。運行時,將彈出WORD窗口。

class WordUtil
{
??? object oMissing = System.Reflection.Missing.Value;
??? object oEndOfDoc = "\\endofdoc"; /**//* \endofdoc is a predefined bookmark */
??? Word._Application oWord = null;
??? Word._Document oDoc = null;

??? public WordUtil()
??? {
??????? oWord = new Word.Application();
??????? oWord.Visible = true;
??????? oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
??? }

??? public WordUtil(string templateFile)
??? {
??????? oWord = new Word.Application();
??????? oWord.Visible = true;
??????? object oTemplate = templateFile;
??????? oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
??? }

??? public void addTxt(string txt)
??? {
??????? Word.Paragraph oPara;
??????? object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
??????? oPara = oDoc.Content.Paragraphs.Add(ref oRng);
??????? oPara.Range.Text = txt;
??????? oPara.Format.SpaceAfter = 6;
??????? oPara.Range.InsertParagraphAfter();
??? }

??? public void addTxt(string bookMark, string txt)
??? {
??????? object oBookMark = bookMark;
??????? oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = txt;
??? }

??? public void addPicture(string fileName, float width, float height)
??? {
??????? Word.Paragraph oPara;
??????? object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
??????? oPara = oDoc.Content.Paragraphs.Add(ref oRng);
??????? Word.InlineShape shape = oPara.Range.InlineShapes.AddPicture(fileName, ref oMissing, ref oMissing, ref oMissing);
??????? shape.Width = oWord.InchesToPoints(width);
??????? shape.Height = oWord.InchesToPoints(height);
??????? oPara.Format.SpaceAfter = 6;
??????? oPara.Range.InsertParagraphAfter();
??? }

??? public void addPicture(string bookMark, string fileName, float width, float height)
??? {
??????? object oBookMark = bookMark;
??????? Word.InlineShape shape = oDoc.Bookmarks.get_Item(ref oBookMark).Range.InlineShapes.AddPicture(fileName, ref oMissing, ref oMissing, ref oMissing);
??????? shape.Width = oWord.InchesToPoints(width);
??????? shape.Height = oWord.InchesToPoints(height);
??? }
}

?

例子:

WordUtil wt = new WordUtil(@"c:\temp.dot");
wt.addTxt("SOA_CUSTOM", "您好");
wt.addTxt("SOA_EOTITLE", "hello the world");
wt.addPicture("SOA_EOTYPE", @"D:\MyPrograms\BMP\img12.jpg", 2f, 1.5f);
wt.addTxt("SOA_EOREASON", "測試");

?

效果:

a

?

在此也非常感謝網上網友的無私共享。

WORD自動化部分操作


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 栾城县| 三河市| 古蔺县| 红原县| 沂源县| 交城县| 项城市| 疏勒县| 闸北区| 上杭县| 疏附县| 湘阴县| 禄劝| 曲周县| 永昌县| 淮安市| 丹阳市| 沙雅县| 五莲县| 保亭| 辽阳县| 永昌县| 封丘县| 平潭县| 湘西| 碌曲县| 道真| 浦江县| 独山县| 合水县| 文山县| 镇赉县| 邢台市| 利川市| 健康| 兴仁县| 铜陵市| 广南县| 涿州市| 宜宾县| 蛟河市|