Using TxtFormatProvider
TxtFormatProvider makes it easy to import and export TXT files. Note that TXT is a plain text format and holds only the contents of the worksheet without its formatting. Exporting a file to this format strips all styling and saves only cell's result value with their format applied using tab as a delimiter. Moreover, it exports only the contents of the active worksheet – no support for exporting multiple worksheets into a txt at once is available. Importing from TXT respectively creates a new workbook with a single worksheet named Sheet1.
In order to import and export txt files, you need an instance of TxtFormatProvider, which is contained in the Telerik.Windows.Documents.Spreadsheet.FormatProviders.TextBased.Txt namespace. The TxtFormatProvider implements the interface IWorkbookFormatProvider that appears in the Telerik.Windows.Documents.Spreadsheet.FormatProviders namespace.
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.
Import
Example 1 shows how to import a txt file using a FileStream. The sample instantiates a TxtFormatProvider and passes a FileStream to its Import() method:
Example 1: Import TXT file
Workbook workbook;
string fileName = "input.txt";
IWorkbookFormatProvider formatProvider = new TxtFormatProvider();
using (Stream input = new FileStream(fileName, FileMode.Open))
{
//workbook = formatProvider.Import(input); //This method is obsolete since Q4 2024.
workbook = formatProvider.Import(input, TimeSpan.FromSeconds(10));
}
Export
Example 2 demonstrates how to export an existing Workbook to a TXT file. The snippet creates a new workbook with a single worksheet. Further, it creates a TxtFormatProvider and invokes its Export() method:
Example 2: Export TXT file
Workbook workbook = new Workbook();
workbook.Worksheets.Add();
string fileName = "SampleFile.txt";
IWorkbookFormatProvider formatProvider = new TxtFormatProvider();
using (Stream output = new FileStream(fileName, FileMode.Create))
{
//formatProvider.Export(workbook, output); //This method is obsolete since Q4 2024.
formatProvider.Export(workbook, output, TimeSpan.FromSeconds(10));
}