创建 Excel 文档作为共享点列表上的附件
我在将 Excel 文档作为附件添加到自定义列表时遇到一些问题。我制作了一个事件接收器,它从其他列表收集数据并将数据放入 Excel 文档中。
我试图纠正问题:
- 在单独的控制台应用程序中测试 CreateContentRow(index, item) (有效)
- 检查从其他列表获取信息是否有问题(有效)
- 尝试保存到文档库(文件保存时没有内容)
- 尝试打开 xlsx 文档以检查 xml 是否有问题(未添加自定义 xml)。
代码工作正常,但文档已保存,但与模板相同,没有添加内容。
using (var memory = new MemoryStream())
{
var binary = template.OpenBinary();
memory.Write(binary, 0, binary.Length);
using (var document = SpreadsheetDocument.Open(memory, true))
{
var workbookPart = document.WorkbookPart;
var worksheetparts = workbookPart.WorksheetParts;
var worksheetpart = worksheetparts.FirstOrDefault();
var sheetData = worksheetpart.Worksheet.GetFirstChild<SheetData>();
var index = 2;
foreach (var item in items)
{
var row = CreateContentRow(index, item);
index++;
sheetData.AppendChild(row);
}
properties.ListItem.Attachments.Add("name" + string.Format("{0:yyyy-MM-dd_HHmmss}", DateTime.Now) + ".xlsx", memory.ToArray());
properties.ListItem.Update();
}
}
我在另一个列表上使用相同的方法,在那里我生成 .docx 文档,它工作得很好。根据文档的 .xlsx 还是 .docx 保存文档的方式是否有很大差异?
当我调试时,单元格和行会添加到工作表数据中,但不会保存。关于如何解决这个问题有什么想法吗?
I've got some problems adding a excel document as attachment on a custom list. I've made an eventreceiver which collects data from other lists and puts the data into an excel document.
What i've tried to do to correct the problem:
- tested CreateContentRow(index, item) in a separate console application (works)
- checked if theres something wrong with fetching information from other lists (works)
- tried saving to a document library (the file gets saved without content)
- tried to open the xlsx document to se if there's something wrong with the xml (no custom xml added).
The code works just fine, but the document is saved but it's identical to the template, no content added.
using (var memory = new MemoryStream())
{
var binary = template.OpenBinary();
memory.Write(binary, 0, binary.Length);
using (var document = SpreadsheetDocument.Open(memory, true))
{
var workbookPart = document.WorkbookPart;
var worksheetparts = workbookPart.WorksheetParts;
var worksheetpart = worksheetparts.FirstOrDefault();
var sheetData = worksheetpart.Worksheet.GetFirstChild<SheetData>();
var index = 2;
foreach (var item in items)
{
var row = CreateContentRow(index, item);
index++;
sheetData.AppendChild(row);
}
properties.ListItem.Attachments.Add("name" + string.Format("{0:yyyy-MM-dd_HHmmss}", DateTime.Now) + ".xlsx", memory.ToArray());
properties.ListItem.Update();
}
}
I'm using the same approach on another list where i generate .docx documents there it works just fine. Is there a big difference in how i should save the document depending on if its .xlsx or .docx ?
When i debug the cells and rows are added to the sheetdata, but it does not get saved. Any ideas on how to fix this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了保存内容,我需要添加保存语句。
当我在添加附件之前添加上面的行时,一切都按预期工作。
In order to get the content saved i needed to add save statements.
When i added the lines above before adding the attachemnt everything worked as it should.