OpenXml Doc sdk 2.0 不在输出中生成 DataSheet 数据
我正在使用 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题,并通过在调用 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().
您必须关闭电子表格。调用 close 会递归调用 save 对所有子项的调用。
http://msdn.microsoft.com/en-us /library/documentformat.openxml.packaging.openxmlpackage.close
与其尝试从头开始创建电子表格,不如考虑使用 ClosedXML 或看看我写入。具体来说,请注意 SheetDataIndexer和 SharedStringTableIndexer。我一直在研究这些示例,因为我在自己的工作中进行了大量的 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.