OpenXml Doc sdk 2.0 不在输出中生成 DataSheet 数据

发布于 2025-01-01 17:14:51 字数 2707 浏览 0 评论 0 原文

我正在使用 Open XML 文档创建电子表格并将其输出到内存流中并从服务器返回。但是输出不包括数据表/工作表内容。我尝试了来自不同 URL 的各种示例,包括 VB.NET 的示例。所有这些都不包含数据表中的内容。但是,如果我使用 Worksheet.OuterXml 属性单独打印工作表的内容,则其显示正确。它不会作为 Workbook.OuterXml 属性的一部分打印。下面给出了示例代码和输出。

private MemoryStream testSpreadsheetOutputAsStream()
    {
        MemoryStream outStream = new MemoryStream();
        using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Create(outStream, SpreadsheetDocumentType.Workbook))
        {
            // create the workbook
            spreadSheet.AddWorkbookPart();
            spreadSheet.WorkbookPart.Workbook = new Workbook();     // create the worksheet
            spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet = new Worksheet();

            // create sheet data
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.AppendChild(new SheetData());

            // create row
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(new Row());

            // create cell with data
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().First().AppendChild(
                  new Cell() { CellValue = new CellValue("101") });

            // save worksheet
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.Save();

            // create the worksheet to workbook relation
            spreadSheet.WorkbookPart.Workbook.AppendChild(new Sheets());
            spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>().AppendChild(new Sheet()
            {
                Id = spreadSheet.WorkbookPart.GetIdOfPart(spreadSheet.WorkbookPart.WorksheetParts.First()),
                SheetId = 1,
                Name = "test"
            });

            spreadSheet.WorkbookPart.Workbook.Save();

            System.Diagnostics.Debug.WriteLine(spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.OuterXml);
            System.Diagnostics.Debug.WriteLine(spreadSheet.WorkbookPart.Workbook.OuterXml);
        }
        return outStream;

    }

输出窗口的输出如下。

Worksheet output <x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:sheetData><x:row><x:c><x:v>101</x:v></x:c></x:row></x:sheetData></x:worksheet>

Workbook output

<x:workbook xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:sheets><x:sheet name="test" sheetId="1" r:id="R0ed99ee34c894367" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" /></x:sheets></x:workbook>

I am using Open XML doc to create a spreadsheet and output it in memory stream and return it from server. However the output does not include DataSheet/Worksheet contents. I tried various examples from different urls including VB.NET ones. All of them don't not include the contents from DataSheet. However, If I print the contents of the worksheet separately using Worksheet.OuterXml Property, its shown properly. Its not being printed as part of Workbook.OuterXml property. The sample code and output is given below.

private MemoryStream testSpreadsheetOutputAsStream()
    {
        MemoryStream outStream = new MemoryStream();
        using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Create(outStream, SpreadsheetDocumentType.Workbook))
        {
            // create the workbook
            spreadSheet.AddWorkbookPart();
            spreadSheet.WorkbookPart.Workbook = new Workbook();     // create the worksheet
            spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet = new Worksheet();

            // create sheet data
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.AppendChild(new SheetData());

            // create row
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(new Row());

            // create cell with data
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().First().AppendChild(
                  new Cell() { CellValue = new CellValue("101") });

            // save worksheet
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.Save();

            // create the worksheet to workbook relation
            spreadSheet.WorkbookPart.Workbook.AppendChild(new Sheets());
            spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>().AppendChild(new Sheet()
            {
                Id = spreadSheet.WorkbookPart.GetIdOfPart(spreadSheet.WorkbookPart.WorksheetParts.First()),
                SheetId = 1,
                Name = "test"
            });

            spreadSheet.WorkbookPart.Workbook.Save();

            System.Diagnostics.Debug.WriteLine(spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.OuterXml);
            System.Diagnostics.Debug.WriteLine(spreadSheet.WorkbookPart.Workbook.OuterXml);
        }
        return outStream;

    }

The output from the output window is as below.

Worksheet output <x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:sheetData><x:row><x:c><x:v>101</x:v></x:c></x:row></x:sheetData></x:worksheet>

Workbook output

<x:workbook xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:sheets><x:sheet name="test" sheetId="1" r:id="R0ed99ee34c894367" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" /></x:sheets></x:workbook>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2025-01-08 17:14:51

我遇到了类似的问题,并通过在调用 spreadSheet.WorkbookPart.Workbook.Save() 后调用 spreadSheet.Close() 来修复它。

I had a similar problem and fixed it by calling spreadSheet.Close() after the call to spreadSheet.WorkbookPart.Workbook.Save().

毁我热情 2025-01-08 17:14:51

您必须关闭电子表格。调用 close 会递归调用 save 对所有子项的调用。

http://msdn.microsoft.com/en-us /library/documentformat.openxml.packaging.openxmlpackage.close

与其尝试从头开始创建电子表格,不如考虑使用 ClosedXML 或看看我写入。具体来说,请注意 SheetDataIndexerSharedStringTableIndexer。我一直在研究这些示例,因为我在自己的工作中进行了大量的 OpenXML 操作。

You have to close the spreadsheet. A call to close recursively calls save on all the child-items.

http://msdn.microsoft.com/en-us/library/documentformat.openxml.packaging.openxmlpackage.close

Instead of trying to create a spreadsheet from scratch, consider using ClosedXML or the take a look at the tiny spreadsheet library that I wrote. Specifically, pay attention to the SheetDataIndexer and the SharedStringTableIndexer. I'm continually working on these examples as I do quite a bit of OpenXML manipulation in my own work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文