如何在每个 sheet word 文档 C# 上写下所需的第一行

How to write the desired first line on each sheet word document C#

如何将文本插入 Word 文档(使用 C# Interop)以便每个 sheet 的第一行都有题词,例如“table 的延续”?我想这样做...

找到每一页的行,在每一页上找到第一行并插入。也许它可以以不同的方式完成……更优雅。但即使采用我的方法,也并非所有行都显示出来。

var rng = docDocument.Range();
int LineCount = rng.ComputeStatistics(Word.WdStatistic.wdStatisticLines);

try
{
    for (int ii = 1; ii < LineCount; ii++)
    {
        CurrentPageNumber = (Convert.ToInt32(ii.ToString()));
        NextPageNumber = (Convert.ToInt32((ii + 1).ToString()));

        // Get start position of current page
        Start = oWord.Selection.GoTo(ref What, ref Which, ref CurrentPageNumber, ref Miss).Start;

        // Get end position of current page
        End = oWord.Selection.GoTo(ref What, ref Which, ref NextPageNumber, ref Miss).End;

        var rng_page = docDocument.Range(Start, End);
        string textLine = rng_page.Text;
        Console.WriteLine(textLine);

        var line_rng = docDocument.Range(unit);
        string textline_line = line_rng.Text;
        //Console.WriteLine(textline_line);
    }
}
catch (Exception ex)
{
    _ = ex.Message;
}

好像有什么东西动了... 但是每个新页面都有一两行新行。另外,由于正在编辑的文件有大量的sheet,需要额外的行和额外的sheet,所以不再对这些sheet执行此功能。

for (int Index = 2; Index <= PagesCount; Index++)
{
    CurrentPageNumber = (Convert.ToInt32(Index.ToString()));
    Word.Range range1 = docDocument.GoTo(ref objWhat, ref objWhich, CurrentPageNumber, ref Miss);
    Word.Range range2 = range1.GoTo(Word.WdGoToItem.wdGoToLine);
    object objStart = range1.Start;
    object objEnd = range2.End;
    //string str = docDocument.Range(ref objStart, ref objEnd).Text;
    range1.InsertParagraph();
    range1.Text = "new_TEXT_new_TXET\n";

}

我最初的逆向工作建议最终没有按预期工作。因为每页添加一个新行都会强制额外的行并继续向下推送数据,所以在原始页面结束新页面开始的地方,无法进一步推送。因此,从文档的顶部开始并向前推进更有效,例如...

在您的代码顶部,我将其添加为 using 上下文,因此您可以使用 WordAutomation[=20] 的 shorthand 名称=] 而不是较长的 Microsoft.Office.Interop.Word,正如您将在函数中看到的那样。

using WordAutomation = Microsoft.Office.Interop.Word;
protected void TestWordAuto()
{
    // I just sampled with a bogus Word Document with some text
    // in it and put on my desktop, you can obviously change
    var wordSampleFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TestAuto1.docx");

    // Create an instance of a word, make it visible to see
    // what is happening throughout if you want to, and
    // open / activate the file
    var wrd = new WordAutomation.Application();
    wrd.Visible = true;
    wrd.Documents.Open(wordSampleFile);
    wrd.ActiveDocument.Activate();

    // Object handle to active document
    var holdMainDoc = wrd.ActiveDocument;

    // Get a "Selection" object of the current document
    var sel = wrd.Selection;

    // Now, we can do a loop through all the pages starting at the end
    var lastPageNumber = 1;
    do
    {
        // Go to the top of each page
        sel.GoTo(WordAutomation.WdGoToItem.wdGoToPage, WordAutomation.WdGoToDirection.wdGoToNext, null, lastPageNumber);

        // Insert some text
        sel.TypeText($"Some Text At top of page {lastPageNumber}\r");

        // Add to page counter loop in document
        lastPageNumber++;
        // Keep going until the page number we just finished
        // was the last page in the document.
    } while (lastPageNumber < holdMainDoc.Words.Last.Information[WordAutomation.WdInformation.wdActiveEndPageNumber]);

    MessageBox.Show("Finished");
}