使用OpenXML附加到DOCX文件。

时间:2022-05-03 22:37:56

I am using OpemXML in C# in order to build my DOCX file. My code looks like this:

为了构建DOCX文件,我在c#中使用了OpemXML。我的代码是这样的:

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(wordFileNamePath, true))
{
    for (int i = 0; i < length; i++)
    {
        using (StreamWriter sw = new StreamWriter(i == 0 ? wordDoc.MainDocumentPart.GetStream(FileMode.Create) : wordDoc.MainDocumentPart.GetStream(FileMode.Append, FileAccess.Write)))
        {
            sw.Write(tempDocText.ToString());
        }
        if (i < length - 1)
        {
            tempDocText = CreateNewStringBuilder();
            InsertPageBreak(wordDoc);
        }
    }
    wordDoc.MainDocumentPart.Document.Save();
}

On the second loop, when it comes to wordDoc.MainDocumentPart.GetStream(FileMode.Append, FileAccess.Write) I'm getting an ArgumentException saying "FileMode value is not supported."

在第二个循环中,当涉及到wordDoc.MainDocumentPart.GetStream(FileMode)时。我得到一个ArgumentException,表示“不支持FileMode值”。

1 个解决方案

#1


0  

I think there is a problem in your code, you are using tempDocText.ToString() before initializing it in the for loop as shown below

我认为在您的代码中存在一个问题,您使用tempDocText.ToString(),然后在for循环中初始化它,如下所示。

using (StreamWriter sw = new StreamWriter(i == 0 ? wordDoc.MainDocumentPart.GetStream(FileMode.Create) : wordDoc.MainDocumentPart.GetStream(FileMode.Append, FileAccess.Write)))
{
    sw.Write(tempDocText.ToString()); //<-Used before Initialization
}

and Initializing it as in the later code block

并在后面的代码块中初始化它。

if (i < length - 1)
{
    tempDocText = CreateNewStringBuilder(); //<-Initializing it here.
    InsertPageBreak(wordDoc);
}

Unless you provide more information about tempDocText, its dificult to help.

除非你提供更多关于tempDocText的信息,否则它会提供帮助。

Anyways, if you just want to add text to a docx file then the following code may help. I found it here.

无论如何,如果您只想将文本添加到docx文件,那么下面的代码可能会有所帮助。我发现在这里。

public static void OpenAndAddTextToWordDocument(string filepath, string txt)
{   
    // Open a WordprocessingDocument for editing using the filepath.
    WordprocessingDocument wordprocessingDocument = 
        WordprocessingDocument.Open(filepath, true);

    // Assign a reference to the existing document body.
    Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

    // Add new text.
    Paragraph para = body.AppendChild(new Paragraph());
    Run run = para.AppendChild(new Run());
    run.AppendChild(new Text(txt));

    // Close the handle explicitly.
    wordprocessingDocument.Close();
}

#1


0  

I think there is a problem in your code, you are using tempDocText.ToString() before initializing it in the for loop as shown below

我认为在您的代码中存在一个问题,您使用tempDocText.ToString(),然后在for循环中初始化它,如下所示。

using (StreamWriter sw = new StreamWriter(i == 0 ? wordDoc.MainDocumentPart.GetStream(FileMode.Create) : wordDoc.MainDocumentPart.GetStream(FileMode.Append, FileAccess.Write)))
{
    sw.Write(tempDocText.ToString()); //<-Used before Initialization
}

and Initializing it as in the later code block

并在后面的代码块中初始化它。

if (i < length - 1)
{
    tempDocText = CreateNewStringBuilder(); //<-Initializing it here.
    InsertPageBreak(wordDoc);
}

Unless you provide more information about tempDocText, its dificult to help.

除非你提供更多关于tempDocText的信息,否则它会提供帮助。

Anyways, if you just want to add text to a docx file then the following code may help. I found it here.

无论如何,如果您只想将文本添加到docx文件,那么下面的代码可能会有所帮助。我发现在这里。

public static void OpenAndAddTextToWordDocument(string filepath, string txt)
{   
    // Open a WordprocessingDocument for editing using the filepath.
    WordprocessingDocument wordprocessingDocument = 
        WordprocessingDocument.Open(filepath, true);

    // Assign a reference to the existing document body.
    Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

    // Add new text.
    Paragraph para = body.AppendChild(new Paragraph());
    Run run = para.AppendChild(new Run());
    run.AppendChild(new Text(txt));

    // Close the handle explicitly.
    wordprocessingDocument.Close();
}