Excel for WPF | ComponentOne
C1Excel Task-Based Help / Saving a CSV File (Silverlight)
In This Topic
    Saving a CSV File (Silverlight)
    In This Topic

    Excel for Silverlight supports saving and loading comma-separated values (CSV) files. CSV is a common file format that stores tabular data, including numbers and text, in plain text form for easy readability.

    The following code provides an example of how to save a .csv file.

    1. Add a reference to C1.Silverlight.Excel.dll.
    2. Add a standard TextBox control to the page and name it txt_Status.
    3. Add a Button control to the page and name it _btnSave.
    4. Select View | Code and add one of the following statements at the top of the form:
      Visual Basic
      Copy Code
      Imports C1.Silverlight.Excel
      

        

      C#
      Copy Code
      using C1.Silverlight.Excel;
      

       

    5. Create a new C1XLBook named _book and add a sheet with some values to the book. Use the following code:

      C#
      Copy Code
      public partial class MainPage : UserControl
          {
              C1XLBook _book = new C1XLBook();
              public MainPage()
              {
                  InitializeComponent();
      
                  XLSheet sheet = _book.Sheets[0];
      
                  for (int i = 0; i <= 9; i++)
                  {
                      sheet[i, 0].Value = i + 1;
                      sheet[i, 1].Value = 10 - i;
                  }
              }
      }
      

       

    6. Then add the following code that will save the book to a CSV file when the user clicks the button. The text box will show updates when the file is saving and when it is finished saving.

      C#
      Copy Code
      private void _btnSave_Click(object sender, RoutedEventArgs e)
              {
                  var dlg = new SaveFileDialog();
                  dlg.Filter = "Comma-Separated Values (*.csv)|*.csv";
                  if (dlg.ShowDialog().Value)
                  {
                      try
                      {
                          // information
                          txt_Status.Text = string.Format("Saving {0}...", dlg.SafeFileName);
      
                          // save workbook
                          using (var stream = dlg.OpenFile())
                          {
                              _book.Sheets[0].SaveCsv(stream);
                          }
                          txt_Status.Text = string.Format("Saved {0}", dlg.SafeFileName); ;
                      }
                      catch (Exception x)
                      {
                          MessageBox.Show(x.Message);
                      }
                  }
              }
      

       

    7. Click View | Designer to switch back to Design view.
    8. Select the button and in the Visual Studio Properties window, click the Events tab.
    9. Click the drop-down arrow next to the Click event and select _btnSave_Click to link the event to the code you just added.
    10. Press F5 to run the project, and then click the button.
    11. Save the file to the desired location and then open the file. It should look similar to this image:

      CSV Files