Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Silverlight | UI for Xamarin | UI for WinUI | UI for ASP.NET Core | UI for .NET MAUI

New to Telerik Document Processing? Download free 30-day trial

Format Providers Manager

RadSpreadprocessing contains a WorkbookFormatProvidersManager class that allows you to specify a set of format providers and import or export files letting the manager choose the appropriate format provider. The class also exposes methods that return all registered providers and supported file extensions.

public class WorkbookFormatProvidersManager
{
    public static IEnumerable<IWorkbookFormatProvider> FormatProviders { get; }

    public static void Export(Workbook workbook, string extension, Stream output);
    public static void Export(Workbook workbook, string extension, Stream output, IEnumerable<IWorkbookFormatProvider> formatProviders);

    public static IWorkbookFormatProvider GetProviderByExtension(string extension);
    public static IWorkbookFormatProvider GetProviderByName(string providerName);

    public static IEnumerable<string> GetSupportedExtensions();

    public static Workbook Import(string extension, Stream input);
    public static Workbook Import(string extension, Stream input, IEnumerable<IWorkbookFormatProvider> formatProviders);

    public static void RegisterFormatProvider(IWorkbookFormatProvider provider);
    public static void UnregisterFormatProvider(IWorkbookFormatProvider provider);
}

Registering and Unregistering Format Providers

The WorkbookFormatProvidersManager class contains two methods that allow you to register and unregister format providers respectively. The manager has the csv and txt format providers registered by default. The snippet in Example 1 illustrates how to register the XlsxFormatProvider.

Some Format Providers require additional assembly references. Check the full list of the FormatProviders' additional reference requirements in Format Providers - Additional assembly references.

Example 1: Register provider

WorkbookFormatProvidersManager.RegisterFormatProvider(new XlsxFormatProvider()); 
WorkbookFormatProvidersManager.RegisterFormatProvider(New XlsxFormatProvider()) 

You can also unregister format providers using the UnregisterFormatProvider() method. Example 2 demonstrates how to unregister the TxtFormatProvider.

Example 2: Unregister provider

IWorkbookFormatProvider provider = WorkbookFormatProvidersManager.GetProviderByName("TxtFormatProvider"); 
if (provider != null) 
{ 
    WorkbookFormatProvidersManager.UnregisterFormatProvider(provider); 
} 
Dim provider As IWorkbookFormatProvider = WorkbookFormatProvidersManager.GetProviderByName("TxtFormatProvider") 
If provider IsNot Nothing Then 
    WorkbookFormatProvidersManager.UnregisterFormatProvider(provider) 
End If 

Import

The format providers manager exposes an Import() method that takes two arguments:

  • string argument - specifies the extension of the file to be imported,
  • Stream argument - provides access to the file.

The method tries to find a registered format provider that can handle the extension of the file you would like to import, and if such a provider is registered the file is imported. If the manager does not have an appropriate format registered, an UnsupportedFileFormatException is thrown.

Example 3 demonstrates how to present the user with an OpenFileDialog and try to import the selected file. Note that you can use the GetOpenFileDialogFilter() method of the FileDialogHelper class to constructs the correct filter for all registered format providers.

Example 3: Import a file using OpenFileDialog

OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog(); 
openFileDialog.Filter = Telerik.Windows.Controls.Spreadsheet.Utilities.FileDialogsHelper.GetOpenFileDialogFilter(); 
if (openFileDialog.ShowDialog() == true) 
{ 
    try 
    { 
        string extension = Path.GetExtension(openFileDialog.SafeFileName); 
        using (Stream input = openFileDialog.OpenFile()) 
        { 
            workbook = WorkbookFormatProvidersManager.Import(extension, input); 
        } 
    } 
    catch (IOException ex) 
    { 
        throw new IOException("The file cannot be opened. It might be locked by another application.", ex); 
    } 
} 
Dim openFileDialog As New Microsoft.Win32.OpenFileDialog() 
openFileDialog.Filter = FileDialogsHelper.GetOpenFileDialogFilter() 
If openFileDialog.ShowDialog() = True Then 
    Try 
        Dim extension As String = Path.GetExtension(openFileDialog.GetFileName()) 
        Using input As Stream = openFileDialog.OpenFileStream() 
            editor.Owner.Workbook = WorkbookFormatProvidersManager.Import(extension, input) 
        End Using 
    Catch ex As IOException 
        Throw New IOException("The file cannot be opened. It might be locked by another application.", ex) 
    End Try 
End If 

The OpenFileDialog class exposes a different API in Silverlight. The name of the file could be obtained through the File.Name property of OpenFileDialog and the stream you can get using File.OpenRead().

You can achieve the same result through using the OpenFile command. In fact, the command executes exactly the same code as above. That said, make sure you register the format providers you would like to use before using the command.

Export

The format providers manager contains an Export() method that takes three arguments:

  • Workbook argument - the workbook to be exported
  • string argument - specifies the extension of the saved file
  • Stream argument - the Stream that will contain the data.

The method attempts to find a provider that can handle a file of the specified extension and if such a provider is registered, the file is saved. If the manager does not have an appropriate registered provider, an UnsupportedFileFormatException is raised.

Example 4 illustrates how to use the Export() method to save a file. The sample code presents the user with the SaveFileDialog. Note that here again you can use the GetOpenFileDialogFilter() method of the FileDialogHelper class to construct the correct filter for all registered format providers.

Example 4: Save a file using SaveFileDialog

Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog(); 
saveFileDialog.Filter = Telerik.Windows.Controls.Spreadsheet.Utilities.FileDialogsHelper.GetSaveFileDialogFilter(); 
 
if (saveFileDialog.ShowDialog() == true) 
{ 
    string extension = Path.GetExtension(saveFileDialog.SafeFileName); 
    using (Stream output = saveFileDialog.OpenFile()) 
    { 
        WorkbookFormatProvidersManager.Export(workbook, extension, output); 
    } 
} 
Dim saveFileDialog As New Microsoft.Win32.SaveFileDialog() 
saveFileDialog.Filter = FileDialogsHelper.GetSaveFileDialogFilter() 
 
If saveFileDialog.ShowDialog() = True Then 
    Dim extension As String = Path.GetExtension(saveFileDialog.SafeFileName) 
    Using output As Stream = saveFileDialog.OpenFile() 
        WorkbookFormatProvidersManager.Export(workbook, extension, output) 
    End Using 
End If 

You can achieve the same result through using the SaveFile command. In fact, the command executes exactly the same code as above. That said, make sure you register the format providers you would like to use before using the command.

Retrieve Registered Providers and Supported Extensions

Currently RadSpreadProcessing supports the following extensions: .xlsx, .xls, .csv, .txt and .pdf (export only). The format providers available for them are respectively XlsxFormatProvider, XlsFormatProvider, CsvFormatProvider, TxtFormatProvider and PdfFormatProvider.

The WorkbookFormatProvidersManager class offers several approaches to retrieve the registered format providers. The class offers the GetProviderByName() static method that searches through the registered providers to find a provider with a specific name. Also, the manager exposes the GetProvderByExtension extension. The class also contains a static method GetSupportedExtensions() that returns an IEnumeable of the currently supported file extensions.

For more examples and application scenarios of Importing and Exporting a Workbook to various formats using a FormatProvider check out the Import/Load and Export/Save RadSpreadProcessing Workbook knowledge base article.

See Also

In this article