Easy Way to Save Excel File as CSV with C#, VB.NET
Posted by Lacy
on 5/27/2012 10:27:26 PM | Points: 75
| Views : 18731 | Status : [Member]
What is CSV?
CSV( Comma-Separated Value) is a file which stores tabular data including numbers and text in a plain text form. That is to say, a CSV file is a sequence of characters. No data in it has been interpreted instead. Any number of records contained in CSV file are separated by line breaks of some kind. Also there are fields in each record. They are separated by other character, comma, string or a literal TAB. So in common cases, all records own an sequence of fields that is exactly alike.
What is the function of CSV?
CSV, as a relatively simple file format, is widely supported by business, consumer and scientific applications. It is best used to represent sequences of records. Besides, a CSV file enables users to transfer information from a database program to a spreadsheet that uses a completely different format. However, CSV format, as its name indicate, using the comma to separate data fields, lacks of standardization. Anyhow, CSV is indispensable in people's daily work, which requires people to convert documents of other formats to CSV. So I would like to share a simple method to save excel file as CSV with C#, VB.NET.
In order to quickly finish Excel to CSV conversion with C#,VB.NET, I choose a .NET component Spire.XLS to help me. It works rather well and only needs three steps.
How to save Excel file as CSV with C#,VB.NET?
Using Spire.XLS, you need not use Microsoft Office Excel installed in your computer. It supports C#, VB.NET, ASP. NET, ASP.NET MVC and Silverlight. Please see the below simple procedure.
Step1. Create a new workbook.
Step2. Load an excel file from system, and transfer the information into workbook.
Step3. Save workbook as any CSV file format and launch the file.
C# Code Convert Excel to CSV:
static void Main(string args)
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets;
sheet.SaveToFile("sample.csv", ",", Encoding.UTF8);
VB.NET Code Convert Excel to CSV:
Private Shared Sub Main(args As String())
Dim workbook As New Workbook()
Dim sheet As Worksheet = workbook.Worksheets(0)
sheet.SaveToFile("sample.csv", ",", Encoding.UTF8)
Besides save Excel file as CSV, Spire.XLS also allows users to convert Excel to other different formats, such as convert Excel to PDF, HTML, XML, Image and so on.