ComponentOne Excel for .NET
In This Topic
    Saving and Loading CSV Files
    In This Topic

    Excel for .NET 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 and load a .csv file:

    1. Double-click the C1XLBook component in the Toolbox to add it to your form.
    2. Select View | Code and add one of the following statements at the top of the form:
      • Import C1.C1Excel (Visual Basic)
      • using C1.C1Excel; (C#)
    3. Add the following code to the Form_Load event to create a sheet with 10 values and save the workbook to .csv format:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub Form1_Load(sender As Object, e As EventArgs)
         Dim sheet As XLSheet = c1XLBook1.Sheets(0)
          For i As Integer = 0 To 9
                sheet(i, 0).Value = i + 1
                sheet(i, 1).Value = 10 – i;
         Next
          sheet.SaveCsv("c:\test.csv")
         System.Diagnostics.Process.Start("C:\test.csv")
       End Sub
      

      To write code in C#

      C#
      Copy Code
      private void Form1_Load(object sender, EventArgs e)
              {
                  XLSheet sheet = c1XLBook1.Sheets[0];
                   for (int i = 0; i <= 9; i++)
                  {
      sheet[i, 0].Value = i + 1;
      sheet[i, 1].Value = 10 - i;
                  }
                   sheet.SaveCsv(@"c:\test.csv");
                  System.Diagnostics.Process.Start(@"C:\test.csv");
       
              }
      
    4. Press F5 to run the project and view the .csv file:
    5. Add some new values to the test.csv file. You'll need to save again in order to save the new values to the file. You can do this by adding code for the LoadCsv and SaveCsv methods to the Form1_Load event so it now looks like this:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub Form1_Load(sender As Object, e As EventArgs)
         Dim sheet As XLSheet = c1XLBook1.Sheets(0)
         For i As Integer = 0 To 9
                sheet(i, 0).Value = i + 1
                sheet(i, 1).Value = 10 – 1
         Next
         sheet.SaveCsv("c:\test.csv")
         sheet.LoadCsv("c:\test.csv")
       
         For i As Integer = 10 To 19
                sheet(i, 0).Value = i + 1
                sheet(i, 1).Value = 10 -1
         Next
       
         sheet.SaveCsv("c:\test.csv")
         System.Diagnostics.Process.Start("C:\test.csv")
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void Form1_Load(object sender, EventArgs e)
              {
                 XLSheet sheet = c1XLBook1.Sheets[0];
                 for (int i = 0; i <= 9; i++)
                 {
      sheet[i, 0].Value = i + 1;
      sheet[i, 1].Value = 10 – i;
                 }
                sheet.SaveCsv(@"c:\test.csv");
                sheet.LoadCsv(@"c:\test.csv");
       
                 for (int i = 10; i <= 19; i++)
                 {
      sheet[i, 0].Value = i + 1;
      sheet[i, 1].Value = 20 – i;
                 }
                  sheet.SaveCsv(@"c:\test.csv");
                 System.Diagnostics.Process.Start(@"C:\test.csv");
              }
      
    6. Press F5 to run the project again and view the .csv file: