我使用Open XML文檔創建電子表格並將其輸出到內存流中並從服務器返回。但是,輸出不包括DataSheet/Worksheet內容。我嘗試了各種不同的網址,包括VB.NET的。它們都不包括DataSheet中的內容。但是,如果我使用Worksheet.OuterXml屬性分別打印工作表的內容,則會正確顯示它。它不被打印爲Workbook.OuterXml屬性的一部分。示例代碼和輸出如下所示。OpenXml Doc sdk 2.0不生成輸出中的DataSheet數據
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>
我想這是正確的,當工作表的內容沒有出現在工作簿xml中。這可能只是因爲它只包含對工作表的引用,並且它們中的每一個的內容都存儲在不同的部分中,所以您的文件似乎沒問題。您是否嘗試過打開它並查看它是否包含您輸入的數據? – 2012-02-02 19:56:18