文件已损坏,使用openXML 2.0保存Excel文档时无法打开

时间:2011-08-18 08:34:24

标签: c# excel openxml

我正在尝试从另一个电子表格中保存行传递给此方法,并将此电子表格另存为电子邮件的附件。电子邮件发送正常,但附件未打开,错误“文件已损坏且无法打开”。我尝试将文件保存到c:上的filstream但是得到了同样的错误。这段代码有什么问题?

 public static void CreateErrorMailWithExcelAttachment(List<Row> rows)
        {
            if (rows.Count > 0)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    //Create a spreadsheet document by supplying the file name.
                    using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook))
                    {

                        // Add a WorkbookPart to the document.
                        spreadsheetDocument.AddWorkbookPart();
                        spreadsheetDocument.WorkbookPart.Workbook = new Workbook();

                        spreadsheetDocument.WorkbookPart.AddNewPart<WorksheetPart>();
                        spreadsheetDocument.WorkbookPart.WorksheetParts.First().Worksheet = new Worksheet();

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

                        // Add Rows to the Sheet.
                        foreach (Row row in rows)
                        {
                            spreadsheetDocument.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(new Row(row.OuterXml));
                        }

                        spreadsheetDocument.WorkbookPart.Workbook.Save();
                    }

                    Dictionary<string, byte[]> attachments = new Dictionary<string, byte[]>();
                    attachments.Add("Book1.xlsx", stream.ToArray());

                    SendEmail
                        (
                            acsis.Common.AppContext.Configuration.GetValue(Constants.ApplicationName, "SMTPServer"),
                            acsis.Common.AppContext.Configuration.GetValue(Constants.ApplicationName, "SMTPUser"),
                            acsis.Common.AppContext.Configuration.GetValue(Constants.ApplicationName, "SMTPPass"),
                            "shaun.bosch@acsis.co.za",
                            acsis.Common.AppContext.Configuration.GetValue(Constants.ApplicationName, "InstitutionalDatabaseAdminEmail"),
                            "Failed rows from bulk investor spreadsheet upload",
                            "Test",
                            false,
                            attachments
                        );
                }
            }
        }

1 个答案:

答案 0 :(得分:3)

在WorkbookPart中,您需要添加一个元素,指定工作簿中的每个工作表。准确地编写代码所需的更改是很棘手的,因为你有很多未分配的创建者,但基本上你需要:

workbook1.AddNamespaceDeclaration("r","http://schemas.openxmlformats.org/officeDocument/2006/relationships");
Sheets sheets1 = new Sheets();
Sheet sheet1 = new Sheet(){ Name = "Sheet1", SheetId = (UInt32Value)1U, Id = "R71b609d3bfb541ee" };
sheets1.Append(sheet1);
workbook1.Append(sheets1);
workbookPart1.Workbook = workbook1;

你必须获得relId标识符并将其放在那里,但是你应该好好去。

希望有所帮助!