Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 9070 |  Welcome, Guest!   Register  Login
 Home > Blogs > C# > Easy Way to Save Excel File as CSV with C#, VB.NET ...
Lacy

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

 Blog author: Lacy | Posted on: 5/27/2012 | Category: C# Blogs | Views: 5226 | Status: [Member] | Points: 75 | Alert Moderator   


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:


About Keating Arly

Experience:1 year(s)
Home page:http://www.e-iceblue.com
Member since:Tuesday, March 27, 2012
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 4:27:54 PM