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.
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 xlsx format provider.
Unlike the CsvFormatProvider and TxtFormatProvider classes, using XlsxFormatProvider requires references to the following assemblies:
Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.dll
Telerik.Windows.Zip.dll
The XlsFormatProvider requires a reference to the following assembly:
- Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.dll
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 one string argument, which specifies the extension of the file to be imported, and a Stream argument, which 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: the workbook that is to be exported, a string that specified the extension of the saved file, and a 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.