使用单词互操作和c#更改文本格式

问题描述 投票:0回答:3

我有一个包含一些文本的Word文件,我想更改文本格式,例如Tabing,数字键,页码计算,粗体等。我该如何使用C#和单词互操作。

谢谢所有

c# interop ms-word
3个回答
1
投票

如果可以使用docx,我建议使用Microsoft的OpenXML Api。比Word Interop容易得多。


1
投票

Interop比较复杂,但是可以用于除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;

希望这会有所帮助


0
投票

以下是一些示例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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.