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

General Information

RadSpreadProcessing's document model allows you to easily open and save files of different formats. This article will share more details on the supported formats, available format providers, the additional assembly references each provider requires and the Format Providers Manager.

Supported formats

  • Xlsx: Rich text format, which exports the whole content of a workbook: worksheets, formula values, formatting, hyperlinks etc.
  • Xls: Rich text format, which exports the content of a workbook: worksheets, formula values, formatting, hyperlinks etc. Supported in older applications. This format is not supported in Silverlight.
  • Xlsm: Rich text format, which exports all that is included in the Xlsx format with the addition of macro instructions.
  • Pdf: Fixed format, which preserves the content of a workbook in independent from software or hardware manner.
  • Csv(comma separated): Plain text format that saves the content of the cell in the active worksheet. The format strips all formatting and keeps only the result values of cells. These values are separated by a culture dependent delimiter.
  • Txt(tab delimited): Plain text format, which preserves only the content of the cells in the active worksheet. The format does not save any formatting and keeps only the result values of the cells. These values are delimited via tabs.
  • DataTable: This allows you to convert the DataTable that is coming form your database to a spreadsheet and vice versa.

Format providers​

The document model exposes separate format providers that work with each of the formats above:

Some FormatProviders require additional assembly references. Check them out in the Additional Assemlby References section.

Import and Export methods

All of the listed providers implement the IWorkbookFormatProvider and IBinaryWorkbookFormatProvider interfaces and, thus, share a common API that enables import and export of files. To conform to the interfaces each of the providers implements two methods that, respectively, turn a Stream or byte[] into a workbook and save the contents of the workbook into a Stream or byte[]. In the IWorkbookFormatProvider interface methods and IBinaryWorkbookFormatProvider interface methods sections below you can see the interfaces' declaration and an example usage of the Import and Export methods.

IWorkbookFormatProvider interface methods

public interface IWorkbookFormatProvider
{
    string Name { get; }
    string FilesDescription { get; }
    IEnumerable<string> SupportedExtensions { get; }
    bool CanImport { get; }
    bool CanExport { get; }

    // Import and Export methods with Stream support
    void Export(Workbook workbook, Stream output);
    Workbook Import(Stream input);
}

Example: Use FormatProver's Import() and Export() methods with Stream array

var path = "MyWorkbook.xlsx";
Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook;
IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider();

// Import 
using (Stream input = new FileStream(path, FileMode.Open))
{
    workbook = formatProvider.Import(input);
}

// Export
using (Stream output = new FileStream(path, FileMode.Create))
{
    formatProvider.Export(workbook, output);
}

IBinaryWorkbookFormatProvider interface methods

public interface IBinaryWorkbookFormatProvider : IWorkbookFormatProvider
{
    // Overloading IWorkbookFormatProvider's Import and Export methods to support byte[]
    byte[] Export(Workbook workbook);
    Workbook Import(byte[] input);
}

Example: Use FormatProver's Import() and Export() methods with byte[] array

var path = "MyWorkbook.xlsx";
Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook;
IBinaryWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider();

// Import
byte[] fileAsByteArray = File.ReadAllBytes(path);
workbook = formatProvider.Import(fileAsByteArray);

// Export
byte[] workbookAsByteArray = formatProvider.Export(workbook);

For more examples of importing and exporting workbooks check out the Import/Load and Export/Save RadSpreadProcessing Workbook knowledge base article.

Additional assembly references

Unlike the CsvFormatProvider, TxtFormatProvider and DataTableFormatProvider classes, the other RadSpreadProcessing format providers require references to additional assemblies.

  • XlsxFormatProvider and XlsmFormatProvider additional assembly references

    • Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.dll
    • Telerik.Windows.Zip.dll
  • PdfFormatProvider

    • Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.dll
  • XlsFormatProvider

    • Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.dll

Format Providers Manager

The document model of RadSpreadProcessing also contains a WorkbookFormatProvidersManager class, which exposes a whole set of useful static methods. The manager also allows you to specify a set of format providers you would like to use. Then you can import and export a file leaving the manager to choose the appropriate format provider to use. You only need to specify the extension of the file that you open or save.

More information on the Format Providers Manager and the WorkbookFormatProvidersManager class can be found in the dedicated Format Providers Manager article.

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);
}

See Also

In this article