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:
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"); } |
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"); } |