Back to Devexpress

Workbook.LoadDocumentProperties(String) Method

officefileapi-devexpress-dot-spreadsheet-dot-workbook-dot-loaddocumentproperties-x28-system-dot-string-x29.md

latest6.8 KB
Original Source

Workbook.LoadDocumentProperties(String) Method

SECURITY-RELATED CONSIDERATIONS

Using file paths sourced from untrusted input may expose unauthorized files or allow unintended file access. Always validate and normalize all external paths to prevent path manipulation.

Loads document properties from the specified workbook. The document format is determined automatically.

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this method in production code.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Docs.v25.2.dll

NuGet Package : DevExpress.Document.Processor

Declaration

csharp
public ReadOnlyDocumentProperties LoadDocumentProperties(
    string fileName
)
vb
Public Function LoadDocumentProperties(
    fileName As String
) As ReadOnlyDocumentProperties

Parameters

NameTypeDescription
fileNameString

The path to the target file.

|

Returns

TypeDescription
ReadOnlyDocumentProperties

An object that stores the retrieved document properties.

|

Remarks

Use this 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.

The Spreadsheet Document API can load document properties from workbooks in the following formats:

  • XLSX,

  • XLSM,

  • XLSB,

  • XLS,

  • XLTX,

  • XLTM,

  • XLT,

  • XML (XML Spreadsheet 2003).

For CSV and TXT files or encrypted documents, the LoadDocumentProperties method returns empty properties. If the loaded document is corrupted or its format is unknown, the LoadDocumentProperties method throws an exception.

Example

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: Load Document Properties for a Workbook

Workbook Class

Workbook Members

DevExpress.Spreadsheet Namespace