【问题标题】:Manipulate Excel files处理 Excel 文件
【发布时间】:2026-01-31 14:20:05
【问题描述】:

如何自动添加到 Excel 文档的 Author 属性?我想为此使用 c# 4。

【问题讨论】:

标签: c# .net excel


【解决方案1】:

文档属性 此链接说明如何读取文档属性并提供您可以访问的属性列表。

private void DisplayBuiltinDocumentProperties()

{ Office.DocumentProperties documentProperties1 = (Office.DocumentProperties)this.BuiltinDocumentProperties;

if (documentProperties1 != null)
{
    for (int i = 1; i <= documentProperties1.Count; i++)
    {
        Office.DocumentProperty dp = documentProperties1[i];
        Globals.Sheet1.Range["A" + i.ToString(), missing].Value2 =
            dp.Name;
    }
}

}

这是所需的进口清单:

使用 Microsoft.Office.Interop.Excel; 使用 Microsoft.Office.Core; //(Com 对象,Office 12 对象库)`

Microsoft.Office.Core.DocumentProperties a = (Microsoft.Office.Core.DocumentProperties)workbook.BuiltinDocumentProperties; a[2].Value = "新作者";

希望对你有帮助

【讨论】:

  • 我可以在不安装 Office 的情况下直接复制二进制文件到机器上吗?
【解决方案2】:

当您指定 C# 4 时,您可以使用以下内容:

Workbook wbk = app.Workbooks.Add();

dynamic properties = wbk.BuiltinDocumentProperties;
dynamic property = properties.Item("Author");

property.Value = "J K Rowling";    

【讨论】: