Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用NPOI在docx文档的开头插入表_C#_.net_Ms Word_Npoi - Fatal编程技术网

C# 如何使用NPOI在docx文档的开头插入表

C# 如何使用NPOI在docx文档的开头插入表,c#,.net,ms-word,npoi,C#,.net,Ms Word,Npoi,我试图使用NPOI在现有docx Word文档的开头插入一个新表。下面是代码的样子: FileStream fs = new FileStream("C:\\temp\\input.docx", FileMode.Open); XWPFDocument doc = new XWPFDocument(fs); XWPFTable table = new XWPFTable(new CT_Tbl(), doc, 2, 2); var

我试图使用NPOI在现有docx Word文档的开头插入一个新表。下面是代码的样子:

        FileStream fs = new FileStream("C:\\temp\\input.docx", FileMode.Open);
        XWPFDocument doc = new XWPFDocument(fs);

        XWPFTable table = new XWPFTable(new CT_Tbl(), doc, 2, 2);
        var par = table.GetRow(0).GetCell(0).AddParagraph();
        var run = new XWPFRun(new CT_R(), (IRunBody) par);
        run.AppendText("The quick brown fox was here.");
        par.AddRun(run);

        doc.InsertTable(0, table);

        FileStream output = new FileStream("C:\\temp\\output.docx", FileMode.Create);

        doc.Write(output);

        output.Close();
        fs.Close();
不幸的是,这不起作用。在output.docx中找不到字符串和表。有人知道为什么吗

我不太确定什么是CT\R或CT\Tbl,或者如何制作CT\R或CT\Tbl,所以这可能是问题的一部分。由于几乎没有文档,希望有人有这个流行的NuGet库的经验


注意:其他示例显示了如何在文档末尾添加表格,而不是在文档开头添加表格。

如果文档很少或没有文档,您使用NPOI库的具体原因是什么?也许可以看看xceed Docx。它是免费的,运行良好。网上有很多例子和信息@PieterAlberts我确实尝试过docx,但我注意到它不支持我们的客户希望支持的旧文档格式。令人沮丧的是,POI有很多文档,但NPOI没有文档,而且与POI有着奇怪的不同。您为什么不使用OpenXMLSDK?它支持完整的“对象模型”(许多其他库只支持“流行”操作),并且有完整的文档记录。