Back to Devexpress

How to: Load Document Properties for a Workbook

officefileapi-402506-spreadsheet-document-api-examples-workbooks-how-to-load-document-properties.md

latest5.1 KB
Original Source

How to: Load Document Properties for a Workbook

  • Sep 19, 2023
  • 2 minutes to read

Use the Workbook.LoadDocumentProperties method to load document metadata (document properties) without loading the workbook itself. You can use the retrieved metadata to search for a specific document, to organize files, or to collect statistics.

Important

The Workbook class is defined in the DevExpress.Docs.v25.2.dll assembly. Add this assembly to your project to use the Workbook API. You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this assembly in production code.

The following example retrieves document properties from workbooks and sorts files by their modification date:

csharp
using DevExpress.Spreadsheet;
using System.Diagnostics;
using System.IO;
using System;
// ...

static void Main(string[] args)
{
    DirectoryInfo directoryInfo = 
        new DirectoryInfo(@"C:\Users\Public\Documents\DevExpress Demos 20.2\Components\Data");
    if (directoryInfo.Exists)
    {
        FileInfo[] files = directoryInfo.GetFiles("*.xlsx");
        foreach (FileInfo file in files)
        {
            SortDocuments(file);
        }
        Process.Start("explorer.exe", @"D:\ExcelDocuments");
    }
}

private static void SortDocuments(FileInfo file)
{
    using (Workbook workbook = new Workbook())
        {
        // Load metadata from the document.
        ReadOnlyDocumentProperties docProperties = 
            workbook.LoadDocumentProperties(file.FullName);
        DateTime date = docProperties.Modified;

        // Check the year when the document was last modified,
        // and copy the file to the appropriate folder.
        switch (date.Year)
        {
            case 2017:
                string destFolder = @"D:\ExcelDocuments\2017";
                CheckDirectory(destFolder);
                file.CopyTo(Path.Combine(destFolder, file.Name), true);
                break;
            case 2018:
                destFolder = @"D:\ExcelDocuments\2018";
                CheckDirectory(destFolder);
                file.CopyTo(Path.Combine(destFolder, file.Name), true);
                break;
            case 2019:
                destFolder = @"D:\ExcelDocuments\2019";
                CheckDirectory(destFolder);
                file.CopyTo(Path.Combine(destFolder, file.Name), true);
                break;
            case 2020:
                destFolder = @"D:\ExcelDocuments\2020";
                CheckDirectory(destFolder);
                file.CopyTo(Path.Combine(destFolder, file.Name), true);
                break;
        }
    }
}

private static void CheckDirectory(string path)
{
    if (!Directory.Exists(path))
        Directory.CreateDirectory(path);
}
vb
Imports DevExpress.Spreadsheet
Imports System.Diagnostics
Imports System.IO
Imports System
' ...

Sub Main()
    Dim directoryInfo As New DirectoryInfo("C:\Users\Public\Documents\DevExpress Demos 20.2\Components\Data")
    If directoryInfo.Exists Then
        Dim files() As FileInfo = directoryInfo.GetFiles("*.xlsx")
        For Each file As FileInfo In files
            SortDocuments(file)
        Next file
        Process.Start("explorer.exe", "D:\ExcelDocuments")
    End If
End Sub

Private Sub SortDocuments(file As FileInfo)
    Using workbook As New Workbook()
        ' Load metadata from the document.
        Dim docProperties As ReadOnlyDocumentProperties = 
            workbook.LoadDocumentProperties(file.FullName)
        Dim dateValue As DateTime = docProperties.Modified

        Dim destFolder As String
        ' Check the year when the document was last modified,
        ' and copy the file to the appropriate folder.
        Select Case dateValue.Year
            Case 2017
                destFolder = "D:\ExcelDocuments\2017"
                CheckDirectory(destFolder)
                file.CopyTo(Path.Combine(destFolder, file.Name), True)
            Case 2018
                destFolder = "D:\ExcelDocuments\2018"
                CheckDirectory(destFolder)
                file.CopyTo(Path.Combine(destFolder, file.Name), True)
            Case 2019
                destFolder = "D:\ExcelDocuments\2019"
                CheckDirectory(destFolder)
                file.CopyTo(Path.Combine(destFolder, file.Name), True)
            Case 2020
                destFolder = "D:\ExcelDocuments\2020"
                CheckDirectory(destFolder)
                file.CopyTo(Path.Combine(destFolder, file.Name), True)
        End Select
    End Using
End Sub

Private Sub CheckDirectory(path As String)
    If Not Directory.Exists(path) Then
        Directory.CreateDirectory(path)
    End If
End Sub

See Also

How to: Specify Document Properties