Opening and Saving Text Files
Spread WinRT Documentation > Developer's Guide > Managing Data > Opening and Saving Text Files

You can load or save text files. You can specify the open or save flags as well as the column, row, and cell delimiters. You can also specify the encoding.

You can use the OpenTextFileAsync method to load text files and the SaveTextFileRangeAsync method to save text files.

Using Code

This example opens and saves text files.

CS
Copy Code
private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                var filePicker = new Windows.Storage.Pickers.FileSavePicker();
                filePicker.FileTypeChoices.Add("Text Files", new List<string>() { ".txt" });
                filePicker.SuggestedFileName = "New SpreadSheet File";
                Windows.Storage.StorageFile storageFile = await filePicker.PickSaveFileAsync();
                if (storageFile != null)
                {
                    using (var stream = await storageFile.OpenStreamForWriteAsync())
                    {
                        var fileName = storageFile.FileType.ToUpperInvariant();
                                await gcSpreadSheet1.Sheets[0].SaveTextFileRangeAsync(0, 0, 5, 5, stream, GrapeCity.Xaml.SpreadSheet.Data.TextFileSaveFlags.AsViewed, "\r", ",", "", System.Text.Encoding.UTF8);
                    }
                }
            }
            catch (Exception ex)
            {
                Windows.UI.Popups.MessageDialog dialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Error");
                dialog.ShowAsync();
            }          
        }
        private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            try
            {
                var filePicker = new Windows.Storage.Pickers.FileOpenPicker();
                filePicker.FileTypeFilter.Add(".txt");               
                Windows.Storage.StorageFile storageFile = await filePicker.PickSingleFileAsync();
                if (storageFile != null)
                {
                    using (var stream = await storageFile.OpenStreamForReadAsync())
                    {
                                await gcSpreadSheet1.Sheets[0].OpenTextFileAsync(stream, GrapeCity.Xaml.SpreadSheet.Data.TextFileOpenFlags.None, "\r", ",", "");
                    }
                }
            }
            catch (Exception ex)
            {
                Windows.UI.Popups.MessageDialog dialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Error");
                dialog.ShowAsync();
            }
        }
VB
Copy Code
Private Async Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
        Try
            Dim filePicker As New Windows.Storage.Pickers.FileSavePicker()
            filePicker.FileTypeChoices.Add("Text Files", New List(Of String)() From {".txt"})
            filePicker.SuggestedFileName = "New SpreadSheet File"
            Dim storageFile As Windows.Storage.StorageFile = Await filePicker.PickSaveFileAsync()
            If storageFile IsNot Nothing Then
                Using stream = Await storageFile.OpenStreamForWriteAsync()
                    Dim fileName = storageFile.FileType.ToUpperInvariant()                  
                        await gcSpreadSheet1.Sheets(0).SaveTextFileRangeAsync(0, 0, 5, 5, stream, GrapeCity.Xaml.SpreadSheet.Data.TextFileSaveFlags.AsViewed, ChrW(13), ",", "", System.Text.Encoding.UTF8)
                End Using
            End If
        Catch ex As Exception
            Dim dialog As Windows.UI.Popups.MessageDialog = New Windows.UI.Popups.MessageDialog(ex.Message, "Error")
            dialog.ShowAsync()
        End Try
    End Sub
   
    Private Async Sub Button_Click_2(sender As Object, e As RoutedEventArgs)
        Try
            Dim filePicker As New Windows.Storage.Pickers.FileOpenPicker()
            filePicker.FileTypeFilter.Add(".txt")
            Dim storageFile As Windows.Storage.StorageFile = Await filePicker.PickSingleFileAsync()
            If storageFile IsNot Nothing Then
                Using stream = Await storageFile.OpenStreamForReadAsync()
                        await gcSpreadSheet1.Sheets(0).OpenTextFileAsync(stream, GrapeCity.Xaml.SpreadSheet.Data.TextFileOpenFlags.None, ChrW(13), ",", "")
                End Using
            End If
        Catch ex As Exception
            Dim dialog As Windows.UI.Popups.MessageDialog = New Windows.UI.Popups.MessageDialog(ex.Message, "Error")
            dialog.ShowAsync()
        End Try
    End Sub
See Also