• WORD自动化部分操作


    为了实现在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

     

    在此也非常感谢网上网友的无私共享。

  • 相关阅读:
    使用Java+SAP云平台+SAP Cloud Connector调用ABAP On-Premise系统里的函数
    使用Java程序消费SAP Leonardo的机器学习API
    如何在SAP Server Side JavaScript里消费destination
    使用HANA Web-based Development Workbench创建最简单的Server Side JavaScript
    如何用WebIDE打开并运行CRM Fiori应用
    SAP成都研究院的体育故事
    ABAP和Java的destination和JNDI
    使用SAP云平台 + JNDI访问Internet Service
    让SAP云平台上的Web应用使用destination服务
    如何处理SAP HANA Web-Based Development Workbench的403 Forbidden错误
  • 原文地址:https://www.cnblogs.com/tianfu/p/1745561.html
Copyright © 2020-2023  润新知