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

发布于 2024-11-30 02:39:07 字数 2744 浏览 1 评论 0原文

我正在尝试保存从另一个电子表格传递到此方法的行,并将此电子表格保存为电子邮件的附件。电子邮件发送正常,但附件无法打开,并出现错误“文件已损坏且无法打开”。我尝试将文件保存到 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"),
                            "[email protected]",
                            acsis.Common.AppContext.Configuration.GetValue(Constants.ApplicationName, "InstitutionalDatabaseAdminEmail"),
                            "Failed rows from bulk investor spreadsheet upload",
                            "Test",
                            false,
                            attachments
                        );
                }
            }
        }

I am trying to save rows pass through to this method from another spreadsheet and save this spreadsheet as an attachment to an email. The email sends fine, but the attachment does not open with the error "file is corrupt and cannot be opened". I tried saving the file to a filstream on the c: but got the same error. What is wrong with this code?

 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"),
                            "[email protected]",
                            acsis.Common.AppContext.Configuration.GetValue(Constants.ApplicationName, "InstitutionalDatabaseAdminEmail"),
                            "Failed rows from bulk investor spreadsheet upload",
                            "Test",
                            false,
                            attachments
                        );
                }
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱要勇敢去追 2024-12-07 02:39:07

在 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 标识符并将其放在那里,但之后您应该就可以了去。

希望有帮助!

Inside your WorkbookPart you need to add a element specifying each of the sheets that are in the workbook. It's tricky to write up exactly the change you need to your code because you've got a lot of unassigned creators, but basically you need:

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;

You'll have to get the relId identifier and put that in there, but then you should be good to go.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文