首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OpenXML创建Word文档总是损坏C#

OpenXML创建Word文档总是损坏C#
EN

Stack Overflow用户
提问于 2022-04-20 13:34:50
回答 1查看 104关注 0票数 1

我正在开发一个旧的webforms应用程序,并试图在其中一个页面中通过OpenXml in C#创建一个word文档。我正在尝试读取SQL中的数据,以填充我将在word文档中创建的表,方法是遵循在http://www.ludovicperrichon.com/create-a-word-document-with-openxml-and-c/中找到的示例

但是,每次运行代码时,它都会生成Word报告损坏的文档,并显示以下消息:

我看不出问题是什么,我一直试图找出类似的问题来解决,但我只能找到对流问题的提及,但这似乎只是在阅读现有文件以编辑而不是创建新文档时才提到的问题。

下面是我的示例代码(不包括加载数据的DataTable dt,因为该元素工作得很好)。

代码语言:javascript
运行
复制
                var header = "";

                using (var stream = new MemoryStream())
                {
                    using (var wordDoc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document))
                    {
                        // Add a main document part.
                        MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();

                        // Create the document structure and add some text.
                        mainPart.Document = new Document();
                        Body docBody = new Body();
                        mainPart.Document.Body = docBody;

                        // Load study plan and build word table to hold data
                        // Create an empty table.
                        var table = new DocumentFormat.OpenXml.Drawing.Table();

                        // Create a TableProperties object and specify its border information.
                        TableProperties tblProp = new TableProperties(
                            new TableBorders(
                                new TopBorder()
                                {
                                    Val =
                                    new EnumValue<BorderValues>(BorderValues.Dashed),
                                    Size = 24
                                },
                                new BottomBorder()
                                {
                                    Val =
                                    new EnumValue<BorderValues>(BorderValues.Dashed),
                                    Size = 24
                                },
                                new LeftBorder()
                                {
                                    Val =
                                    new EnumValue<BorderValues>(BorderValues.Dashed),
                                    Size = 24
                                },
                                new RightBorder()
                                {
                                    Val =
                                    new EnumValue<BorderValues>(BorderValues.Dashed),
                                    Size = 24
                                },
                                new InsideHorizontalBorder()
                                {
                                    Val =
                                    new EnumValue<BorderValues>(BorderValues.Dashed),
                                    Size = 24
                                },
                                new InsideVerticalBorder()
                                {
                                    Val =
                                    new EnumValue<BorderValues>(BorderValues.Dashed),
                                    Size = 24
                                }
                            )
                        );

                        // Append the TableProperties object to the empty table.
                        table.AppendChild<TableProperties>(tblProp);

                        // Add header row
                        // Create a row.
                        var hr = new DocumentFormat.OpenXml.Drawing.TableRow();

                        // Create a cell.
                        var hc1 = new DocumentFormat.OpenXml.Drawing.TableCell();

                        // Specify the width property of the table cell.
                        hc1.Append(new TableCellProperties(
                            new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

                        // Specify the table cell content.
                        hc1.Append(new Paragraph(new Run(new Text("Start Date"))));

                        // Append the table cell to the table row.
                        hr.Append(hc1);

                        // Create a cell
                        var hc2 = new DocumentFormat.OpenXml.Drawing.TableCell();

                        hc2.Append(new TableCellProperties(
                        new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

                        hc2.Append(new Paragraph(new Run(new Text("End Date"))));

                        // Append the table cell to the table row.
                        hr.Append(hc2);

                        // Create a cell
                        var hc3 = new DocumentFormat.OpenXml.Drawing.TableCell();

                        hc3.Append(new TableCellProperties(
                            new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

                        hc3.Append(new Paragraph(new Run(new Text("Type"))));

                        // Append the table cell to the table row.
                        hr.Append(hc3);

                        // Create a cell
                        var hc4 = new DocumentFormat.OpenXml.Drawing.TableCell();

                        hc4.Append(new TableCellProperties(
                            new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

                        hc4.Append(new Paragraph(new Run(new Text("Status"))));

                        // Append the table cell to the table row.
                        hr.Append(hc4);

                        // Create a cell
                        var hc5 = new DocumentFormat.OpenXml.Drawing.TableCell();

                        hc5.Append(new TableCellProperties(
                            new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

                        hc5.Append(new Paragraph(new Run(new Text("Description"))));

                        // Append the table cell to the table row.
                        hr.Append(hc5);

                        // Append the table row to the table.
                        table.Append(hr);

                        // Add data rows
                        for (var i = 0; i < dt.Rows.Count; i++)
                        {
                            // Create a row.
                            var tr = new DocumentFormat.OpenXml.Drawing.TableRow();

                            // Create a cell.
                            var tc1 = new DocumentFormat.OpenXml.Drawing.TableCell();

                            // Specify the width property of the table cell.
                            tc1.Append(new TableCellProperties(
                                new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

                            // Specify the table cell content.
                            tc1.Append(new Paragraph(new Run(new Text(dt.Rows[i]["start"].ToString()))));

                            // Append the table cell to the table row.
                            tr.Append(tc1);

                            //Create a cell
                            var tc2 = new DocumentFormat.OpenXml.Drawing.TableCell();

                            tc2.Append(new TableCellProperties(
                                new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

                            tc2.Append(new Paragraph(new Run(new Text(dt.Rows[i]["end"].ToString()))));

                            //Append the table cell to the table row.
                            tr.Append(tc2);

                            ///Create a cell
                            var tc3 = new DocumentFormat.OpenXml.Drawing.TableCell();

                            tc3.Append(new TableCellProperties(
                                new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

                            tc3.Append(new Paragraph(new Run(new Text(dt.Rows[i]["type"].ToString()))));

                            //Append the table cell to the table row.
                            tr.Append(tc3);

                            // Create a cell
                            var tc4 = new DocumentFormat.OpenXml.Drawing.TableCell();

                            tc4.Append(new TableCellProperties(
                                new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

                            tc4.Append(new Paragraph(new Run(new Text(dt.Rows[i]["status"].ToString()))));

                            //Append the table cell to the table row.
                            tr.Append(tc4);

                            //Create a cell
                            var tc5 = new DocumentFormat.OpenXml.Drawing.TableCell();

                            tc5.Append(new TableCellProperties(
                                new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

                            tc5.Append(new Paragraph(new Run(new Text(dt.Rows[i]["description"].ToString()))));

                            //Append the table cell to the table row.
                            tr.Append(tc5);

                            //Append the table row to the table.
                            table.Append(tr);
                        }

                        //Append the table to the document.                        
                        wordDoc.MainDocumentPart.Document.Body.Append(table);

                        header = "attachment;filename=" + DateTime.Today.ToString("dd/MM/yyyy") + student.Replace(" ", "_") + "_study_plan.docx";
                    }

                    Context.Response.AppendHeader("Content-Disposition", header);
                    stream.Position = 0;
                    stream.CopyTo(Context.Response.OutputStream);
                    Context.Response.Flush();
                    Context.Response.End();
                }
EN

Stack Overflow用户

回答已采纳

发布于 2022-04-20 14:49:19

您正在使用来自错误命名空间的表元素。而不是

代码语言:javascript
运行
复制
var table = new DocumentFormat.OpenXml.Drawing.Table();

使用

代码语言:javascript
运行
复制
var table = new DocumentFormat.OpenXml.Wordprocessing.Table();

这也适用于所有其他表元素(单元格等)。

我建议使用OpenXMLSDK。如果您最终得到了一个无效的文件,它将显示出它的问题所在。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71940665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档