Search
Sponsor
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Blogs Author
Thu, 27-Nov-2014 Authors
All Time Authors
Samirbhogayta
9750
Satyapriyanayak
7575
Jvprabhusanthi
6150

Latest members | More ...


(Statistics delayed by 5 minutes)
 Blogs

Easy Way to Save Excel File as CSV with C#, VB.NET

Lacy
Posted by Lacy under C# on 5/27/2012 10:27:26 PM | Points: 75 | Views : 17841 | 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.

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. 

Main Code:

C# Code  Convert Excel to CSV:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Xls;
namespace xls2csv
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"C:\e-iceblue\employee.xls");
            Worksheet sheet = workbook.Worksheets[0];
            sheet.SaveToFile("sample.csv", ",", Encoding.UTF8);
            System.Diagnostics.Process.Start("sample.csv");
        }
    }
}

VB.NET Code Convert Excel to CSV:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Spire.Xls
Namespace xls2csv
         Class Program
                 Private Shared Sub Main(args As String())
                          Dim workbook As New Workbook()
                          workbook.LoadFromFile("C:\e-iceblue\employee.xls")
                          Dim sheet As Worksheet = workbook.Worksheets(0)
                          sheet.SaveToFile("sample.csv", ",", Encoding.UTF8)
                          System.Diagnostics.Process.Start("sample.csv")
                 End Sub
         End Class
End Namespace

Preview
                   
                                    Excel File
                                                                           
                                                                                                       csv table

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. 


Found interesting? Add this to:



Comments or Responses

Posted by: Zaiba on: 9/16/2013 | Level:Starter | Status: [Member] | Points: 15

Aspose provides .NET Library for converting excel file to CSV Format. Below is the link of the library you can try it:

http://www.aspose.com/.net/excel-component.aspx


Login to post response