使用 openXML 2.0 保存 Excel 文档时文件已损坏且无法打开
我正在尝试保存从另一个电子表格传递到此方法的行,并将此电子表格保存为电子邮件的附件。电子邮件发送正常,但附件无法打开,并出现错误“文件已损坏且无法打开”。我尝试将文件保存到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 WorkbookPart 中,您需要添加一个元素来指定工作簿中的每个工作表。准确地写出您需要对代码进行的更改是很棘手的,因为您有很多未分配的创建者,但基本上您需要:
您必须获取 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:
You'll have to get the relId identifier and put that in there, but then you should be good to go.
Hope that helps!