# Saving a CSV File (Silverlight)

## Content



**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:
    
    ```vbnet
    Imports C1.Silverlight.Excel
    ```
    
    ```csharp
    using C1.Silverlight.Excel;
    ```
    
5.  Create a new [C1XLBook](/componentone/api/wpf/online-excel/dotnet-framework-api/C1.WPF.Excel.4.6.2/C1.WPF.Excel.C1XLBook.html) named \_book and add a sheet with some values to the book. Use the following code:
    
    ```csharp
    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.
    
    ```csharp
    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](https://cdn.mescius.io/document-site-files/images/4e8fb55a-cee4-4e5b-a6f1-f4ba4ccbfde1/c1excel_graphics/csvfiles.png)