在 OPEN XML 中创建电子表格文档并向其中添加表
我正在尝试通过open xml和表格创建电子表格。但是,在打开创建的 Excel 文件时,总是返回错误“Excel 在 test1.xlsx 中发现不可读的内容,是否要恢复。为什么总是发生此错误?当创建没有表的 Excel 时,我没有收到任何错误。请查找下面是我用来创建的示例代码。
using (SpreadsheetDocument spreadsheetdocument = SpreadsheetDocument.Create(@"c://test.xlsx", SpreadsheetDocumentType.Workbook))
{
// add a workbookpart to the document.
WorkbookPart workbookpart = spreadsheetdocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
// add a worksheetpart to the workbookpart.
WorksheetPart worksheetpart = workbookpart.AddNewPart<WorksheetPart>();
worksheetpart.Worksheet = new Worksheet(new SheetData());
// add sheets to the workbook.
Sheets sheets = spreadsheetdocument.WorkbookPart.Workbook.
AppendChild<Sheets>(new Sheets());
// append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
Id = spreadsheetdocument.WorkbookPart.
GetIdOfPart(worksheetpart),
SheetId = 1,
Name = "mysheet"
};
sheets.Append(sheet);
TableDefinitionPart tableDefinitionPart = worksheetpart.AddNewPart<TableDefinitionPart>("rId1");
GenerateTablePartContent(ref tableDefinitionPart);
TableParts tableParts1 = new TableParts() { Count = (UInt32Value)2U };
TablePart tablePart1 = new TablePart() { Id = "rId1" };
tableParts1.Append(tablePart1);
worksheetpart.Worksheet.Append(tableParts1);
spreadsheetdocument.Close();
}
//Added spaces to get the code in one block
private void GenerateTablePartContent(TableDefinitionPart part)
{
Table table1 = new Table() { Id = (UInt32Value)1U, Name = "Table1", DisplayName = "Table1", Reference = "A1:C3", TotalsRowShown = false };
AutoFilter autoFilter1 = new AutoFilter() { Reference = "A1:C3" };
TableColumns tableColumns1 = new TableColumns() { Count = (UInt32Value)3U };
TableColumn tableColumn1 = new TableColumn() { Id = (UInt32Value)1U, Name = "Column1" };
TableColumn tableColumn2 = new TableColumn() { Id = (UInt32Value)2U, Name = "Column2" };
TableColumn tableColumn3 = new TableColumn() { Id = (UInt32Value)3U, Name = "Column3" };
tableColumns1.Append(tableColumn1);
tableColumns1.Append(tableColumn2);
tableColumns1.Append(tableColumn3);
TableStyleInfo tableStyleInfo1 = new TableStyleInfo() { Name = "TableStyleMedium2", ShowFirstColumn = false, ShowLastColumn = false, ShowRowStripes = true, ShowColumnStripes = false };
table1.Append(autoFilter1);
table1.Append(tableColumns1);
table1.Append(tableStyleInfo1);
part.Table = table1;
}
I am trying to create an spread sheet by open xml with an table. But while opening the created excel file always it returns the error "Excel found unreadable content in test1.xlsx, do u want to recover. Why this error always happens? When the excel is created without table, i didnt get any error. Please find the below sample code which i used to create.
using (SpreadsheetDocument spreadsheetdocument = SpreadsheetDocument.Create(@"c://test.xlsx", SpreadsheetDocumentType.Workbook))
{
// add a workbookpart to the document.
WorkbookPart workbookpart = spreadsheetdocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
// add a worksheetpart to the workbookpart.
WorksheetPart worksheetpart = workbookpart.AddNewPart<WorksheetPart>();
worksheetpart.Worksheet = new Worksheet(new SheetData());
// add sheets to the workbook.
Sheets sheets = spreadsheetdocument.WorkbookPart.Workbook.
AppendChild<Sheets>(new Sheets());
// append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
Id = spreadsheetdocument.WorkbookPart.
GetIdOfPart(worksheetpart),
SheetId = 1,
Name = "mysheet"
};
sheets.Append(sheet);
TableDefinitionPart tableDefinitionPart = worksheetpart.AddNewPart<TableDefinitionPart>("rId1");
GenerateTablePartContent(ref tableDefinitionPart);
TableParts tableParts1 = new TableParts() { Count = (UInt32Value)2U };
TablePart tablePart1 = new TablePart() { Id = "rId1" };
tableParts1.Append(tablePart1);
worksheetpart.Worksheet.Append(tableParts1);
spreadsheetdocument.Close();
}
//Added spaces to get the code in one block
private void GenerateTablePartContent(TableDefinitionPart part)
{
Table table1 = new Table() { Id = (UInt32Value)1U, Name = "Table1", DisplayName = "Table1", Reference = "A1:C3", TotalsRowShown = false };
AutoFilter autoFilter1 = new AutoFilter() { Reference = "A1:C3" };
TableColumns tableColumns1 = new TableColumns() { Count = (UInt32Value)3U };
TableColumn tableColumn1 = new TableColumn() { Id = (UInt32Value)1U, Name = "Column1" };
TableColumn tableColumn2 = new TableColumn() { Id = (UInt32Value)2U, Name = "Column2" };
TableColumn tableColumn3 = new TableColumn() { Id = (UInt32Value)3U, Name = "Column3" };
tableColumns1.Append(tableColumn1);
tableColumns1.Append(tableColumn2);
tableColumns1.Append(tableColumn3);
TableStyleInfo tableStyleInfo1 = new TableStyleInfo() { Name = "TableStyleMedium2", ShowFirstColumn = false, ShowLastColumn = false, ShowRowStripes = true, ShowColumnStripes = false };
table1.Append(autoFilter1);
table1.Append(tableColumns1);
table1.Append(tableStyleInfo1);
part.Table = table1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Open XML SDK Productivity Tool。使用正常方式创建一个 Excel 文件,并将相同的数据放在那里,然后将该文件与您使用代码创建的文件进行比较。您将看到那里的变化。我也面临同样的问题,我正在尝试使用该工具修复更改。让我们看看......
如果您已经解决了问题,请指导我如何做。
use Open XML SDK Productivity Tool for it. Create an excel file using normal way and put same data there as well and compare that file with the file you are creating with your code. You will see the changes there. I am also facing same issue and I am trying to fix the changes using that tool. Lets see....
and If you have solved the issue please guide me how to do it.