首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在OpenXML SpeadsheetDocument中设置或清除作者

如何在OpenXML SpeadsheetDocument中设置或清除作者
EN

Stack Overflow用户
提问于 2018-07-27 23:43:49
回答 1查看 757关注 0票数 0

在C#.Net中使用OpenXML SpreadsheetDocument类创建电子表格时,Authors和Last saved by字段被设置为"James Westgate“。

如何清除或覆盖James的名字?

代码语言:javascript
复制
SpreadsheetDocument doc = SpreadsheetDocument.Open(stream, true);

doc.PackageProperties.Creator = "sh";

...is对我不起作用

更新:-

代码语言:javascript
复制
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);
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-28 06:33:20

这对我很有效(在OpenXml包中添加完NuGet之后):

代码语言:javascript
复制
using (var doc = SpreadsheetDocument.Open(@"C:\tmp\MyExcelFile.xlsx", true))
{
    var props = doc.PackageProperties;
    props.Creator = "Flydog57";
    props.LastModifiedBy = "Flydog57";
    doc.Save();
}

它甚至在我第一次尝试的时候就起作用了!这是一件不寻常的事情。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51561363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档