Excel File Properties

Following sample demonstrates how to read and write Document Properties like Title, Author, Comments, etc.

Screenshot

Document Properties Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using System.Collections.Generic;
3using System.Text;
4using GemBox.Spreadsheet;
5using GemBox.Spreadsheet.ConditionalFormatting;
6using GemBox.Spreadsheet.PivotTables;
7
8class Sample
9{
10    [STAThread]
11    static void Main(string[] args)
12    {
13        // If using Professional version, put your serial key below.
14        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
15
16        ExcelFile ef = ExcelFile.Load("TemplateUse.xlsx");
17
18        // Add Sheet
19        ExcelWorksheet ws = ef.Worksheets.ActiveWorksheet = ef.Worksheets.InsertEmpty(0, "Document Properties");
20
21        int rowIndex = 0;
22        // Read Built-in Document Properies 
23        ws.Cells[rowIndex++, 0].Value = "Built-in document properties";
24
25        ws.Cells[rowIndex, 0].Value = "Property";
26        ws.Cells[rowIndex++, 1].Value = "Value";
27
28        foreach (KeyValuePair<BuiltInDocumentProperties, string> keyValue in ef.DocumentProperties.BuiltIn)
29        {
30            ws.Cells[rowIndex, 0].Value = keyValue.Key.ToString();
31            ws.Cells[rowIndex++, 1].Value = keyValue.Value;
32        }
33
34        // Read Custom Document Properties
35        ws.Cells[++rowIndex, 0].Value = "Custom Document Properties";
36
37        ws.Cells[++rowIndex, 0].Value = "Property";
38        ws.Cells[rowIndex++, 1].Value = "Value";
39
40        foreach (KeyValuePair<string, object> keyValue in ef.DocumentProperties.Custom)
41        {
42            ws.Cells[rowIndex, 0].Value = keyValue.Key;
43            ws.Cells[rowIndex++, 1].Value = keyValue.Value.ToString();
44        }
45
46        // Write/Modifiy Document Properties
47        ef.DocumentProperties.BuiltIn[BuiltInDocumentProperties.Author] = "John Doe";
48        ef.DocumentProperties.BuiltIn[BuiltInDocumentProperties.Title] = "Genrated title";
49
50        ws.Columns[0].AutoFit();
51        ws.Columns[1].AutoFit();
52
53        ef.Save("Document Properties.xls");
54    }
55}
1Imports System
2Imports System.Collections.Generic
3Imports System.Text
4Imports GemBox.Spreadsheet
5Imports GemBox.Spreadsheet.ConditionalFormatting
6Imports GemBox.Spreadsheet.PivotTables
7
8Module Samples
9
10    Sub Main()
11
12        ' If using Professional version, put your serial key below.
13        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
14
15        Dim ef As ExcelFile = ExcelFile.Load("TemplateUse.xlsx")
16
17        ' Add Sheet
18        Dim ws As ExcelWorksheet = ef.Worksheets.InsertEmpty(0, "Document Properties")
19        ef.Worksheets.ActiveWorksheet = ws
20
21        Dim rowIndex As Integer = 0
22        ' Read Built-in Document Properies 
23        ws.Cells(rowIndex, 0).Value = "Built-in document properties"
24        rowIndex = rowIndex + 1
25
26        ws.Cells(rowIndex, 0).Value = "Property"
27        ws.Cells(rowIndex, 1).Value = "Value"
28        rowIndex = rowIndex + 1
29
30        For Each keyValue As KeyValuePair(Of BuiltInDocumentProperties, String) In ef.DocumentProperties.BuiltIn
31
32            ws.Cells(rowIndex, 0).Value = keyValue.Key.ToString()
33            ws.Cells(rowIndex, 1).Value = keyValue.Value
34            rowIndex = rowIndex + 1
35
36        Next
37
38        ' Read Custom Document Properties
39        rowIndex = rowIndex + 1
40        ws.Cells(rowIndex, 0).Value = "Custom Document Properties"
41
42        rowIndex = rowIndex + 1
43        ws.Cells(rowIndex, 0).Value = "Property"
44        ws.Cells(rowIndex, 1).Value = "Value"
45        rowIndex = rowIndex + 1
46
47        For Each keyValue As KeyValuePair(Of String, Object) In ef.DocumentProperties.Custom
48
49            ws.Cells(rowIndex, 0).Value = keyValue.Key
50            ws.Cells(rowIndex, 1).Value = keyValue.Value.ToString()
51            rowIndex = rowIndex + 1
52
53        Next
54
55        ' Write/Modifiy Document Properties
56        ef.DocumentProperties.BuiltIn(BuiltInDocumentProperties.Author) = "John Doe"
57        ef.DocumentProperties.BuiltIn(BuiltInDocumentProperties.Title) = "Genrated title"
58
59        ws.Columns(0).AutoFit()
60        ws.Columns(1).AutoFit()
61
62        ef.Save("Document Properties.xls")
63
64    End Sub
65
66End Module

Check next sample or find out more about GemBox.Spreadsheet and GemBox Software.