Manipulate Excel files
How can I add to the Author property of an Excel document开发者_C百科 automatically? I want to use c# 4 for this.
Document Properties This link explains how to read the document properties and gives a list of properties which you can access.
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;
}
}
}
Here's a list of imports required :
using Microsoft.Office.Interop.Excel; using Microsoft.Office.Core; // (Com Object, Office 12 object library)`
Microsoft.Office.Core.DocumentProperties a = (Microsoft.Office.Core.DocumentProperties)workbook.BuiltinDocumentProperties; a[2].Value = "new Author";
Hope that helps
As you specified C# 4, you can use the following:
Workbook wbk = app.Workbooks.Add();
dynamic properties = wbk.BuiltinDocumentProperties;
dynamic property = properties.Item("Author");
property.Value = "J K Rowling";
精彩评论