9

We are trying to generate MS Excel workbook using OOXML and populate data using SSIS. We are able to generate Workbook and sheets, also able to create columns and insert data in the Header cell. We can also populate data using SSIS.

But the Sheet (DocumentFormat.OpenXml.Spreadsheet.Sheet) and all cells (DocumentFormat.OpenXml.Spreadsheet.Cell) becomes OpenXmlUnknownElement. So we are not able to read sheet / cell using following code: Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Sheet1").SingleOrDefault<Sheet>();

We are able to read the same file if we first open it using MS Excel and save. Does anyone know how to resolve this?

4

1 回答 1

1

你可能忘了给你的工作表起个名字。您可以使用

    Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault 

你会看到你的工作表名称要么是未定义的,要么是垃圾文本。

如果那没有帮助。在代码中创建一个简单的文档,将其保存在 OOXML 中并在 xml 查看器中打开它。然后制作一个副本在Excel中打开它并保存它并查看xml中的差异。这通常是查看 excel 默认添加到文档中的内容的良好开端。

Excel 非常容忍您在代码中创建文档时做错的事情,并在您再次打开文档时神奇地修复它们。

一个糟糕的 hack 将使用互操作打开文档,再次将其保存在代码中。这将为您解决所有问题。

    Workbook wrkbk = app.Workbooks.Open(@"c:\del.xls");
    wrkbk.Save();
    wrkbk.Close();
于 2012-12-12T10:23:19.670 回答