在C#.Net中使用OpenXML SpreadsheetDocument类创建电子表格时,“作者”和“最后保存者”字段设置为“詹姆斯·韦斯特盖特”。
如何清除或覆盖James的名字?
SpreadsheetDocument doc = SpreadsheetDocument.Open(stream, true);
doc.PackageProperties.Creator = "sh";
...对我不起作用。
更新:-
using System;
using System.Windows.Forms;
using System.IO;
using DocumentFormat.OpenXml.Extensions;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace OpenXMLProps
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public MemoryStream Execute()
{
MemoryStream stream = SpreadsheetReader.Create();
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(stream, true))
{
WorksheetPart worksheetPart = SpreadsheetReader.GetWorksheetPartByName(doc, "Sheet1");
WorksheetWriter writer = new WorksheetWriter(doc, worksheetPart);
doc.PackageProperties.Creator = "Finbar mahoolahan";
SpreadsheetWriter.Save(doc);
return new MemoryStream(stream.ToArray());
}
}
private void button1_Click(object sender, EventArgs e)
{
using (FileStream fs = new FileStream("excel_test.xlsx", FileMode.Create, FileAccess.Write))
{
MemoryStream excel_stream = Execute();
excel_stream.WriteTo(fs);
}
}
}
}
答案 0 :(得分:1)
我认为(现在不用看大约8年的代码了)有一个基础文档构成了模板,并且这个模板可能与我的名字相关联。这是作为资源嵌入dll中的。
我建议尝试从CodePlex下载原始的 SimpleOOXml 源代码并尝试修改文档。您也可以针对现代版本的OpenXML库进行重新编译。
答案 1 :(得分:0)
这对我有用(在OpenXml软件包中的NuGet-ing之后):
using (var doc = SpreadsheetDocument.Open(@"C:\tmp\MyExcelFile.xlsx", true))
{
var props = doc.PackageProperties;
props.Creator = "Flydog57";
props.LastModifiedBy = "Flydog57";
doc.Save();
}
它甚至在我第一次尝试时就起作用了!那是不寻常的事情。