【问题标题】:Changing the formating of text using word interop and c#使用 word interop 和 c# 更改文本格式
【发布时间】:2020-03-08 03:29:00
【问题描述】:

我有一个包含一些文本的 word 文件,我想更改文本的格式,例如 Tabing、numbaring、页码计算、粗体等。 我怎么能用 C# 和 word 互操作来做到这一点。

谢谢大家

【问题讨论】:

    标签: c# interop ms-word


    【解决方案1】:

    如果您可以使用 docx,我会推荐 Microsoft 的 OpenXML Api。比 Word 互操作容易得多。

    【讨论】:

      【解决方案2】:

      互操作更复杂,但可以用于 Docx 以外的其他文件格式

      使用互操作:

      // Make Text Between Start And End Bold
      MyWordDoc.Range(start, end).Font.Bold = 1;
      

      // Make Selected Text Bold
      MyWordApp.Selection.Range.Font.Bold = 1;
      

      希望对你有帮助

      【讨论】:

        【解决方案3】:

        这里有一些示例 C# 代码,其中包含一些 Interop Word 使用演示,包括一种选择性地将单个单词设置为粗体的方法。诀窍是缩小 Word 样式和 Word 对象类型的每次更改或偏差之间的范围。

        using System;
        using Word = Microsoft.Office.Interop.Word;
        using System.Runtime.InteropServices;
        
        namespace WordDemo {
            public class WordDemo {
                string wordFile;
                Word.Application wordApp;
                Word.Document wordDoc;
                Word.Range range;
        
                public WordDemo() {
                    // Set your filename here; defaults to saving to your desktop
                    wordFile = SetFileDetails("WordCreationTest.docx");
                    InitializeWordObjects();
                    AddTextToWord();
                    AddTableToWord();
                    SaveCloseQuitWord();
                }
        
                private string SetFileDetails(string filename) {
                    string userDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    return userDesktop + "\\" + filename;
                }
        
                private void InitializeWordObjects() {
                    wordApp = new Word.Application();
                    wordApp.Visible = true;
                    wordDoc = new Word.Document();
                    wordDoc = wordApp.Documents.Add();
                    range = wordDoc.Range();
                }
        
                private void AddTextToWord() {
                    range.InsertAfter("Testing 1, 2, 3");
                    range.InsertParagraphAfter();
                    range.InsertParagraphAfter();
                    range.InsertAfter(
                        "Adding a collection of text to the document here, some " +
                        "really incredibly interesting stuff to read, I'm sure... " +
                        "or at least that's the rumor. Also, "
                        );
                    range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
                    range.InsertAfter("these words");
                    // Interop doesn't recognize C# booleans, so we convert to integers:
                    range.Font.Bold = Convert.ToInt32(true);
                    range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
                    range.InsertAfter(" are bold.");
                    range.Font.Bold = Convert.ToInt32(false);
                    range.InsertParagraphAfter();
                    range.InsertParagraphAfter();
                }
        
                private void AddTableToWord() {
                    range.InsertAfter(
                        "Okay, well, that was interesting. Next, here is a table creation " +
                        "demonstration. This table is comprised of 3 rows and 4 columns, " +
                        "defaulting to autofit, and setting the inside and outside " +
                        "line styles to single line style."
                        );
                    range.InsertParagraphAfter();
                    range.InsertParagraphAfter();
                    range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
                    Word.Table table = wordDoc.Tables.Add(
                        range, 3, 4,
                        Word.WdDefaultTableBehavior.wdWord9TableBehavior
                        );
                    table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    range = wordDoc.Content;
                    range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
                    range.InsertParagraphAfter();
                }
        
                private void SaveCloseQuitWord() {
                    wordDoc.SaveAs2(wordFile);
                    wordDoc.Close();
                    Marshal.FinalReleaseComObject(wordDoc);
                    wordApp.Quit();
                    Marshal.FinalReleaseComObject(wordApp);
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-09
          • 2020-03-16
          • 2021-12-19
          相关资源
          最近更新 更多