CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: how to open .csv file

  1. #1
    Join Date
    Feb 2012
    Posts
    7

    how to open .csv file

    hi ,this is jagadeesh kumar ,in my project i have to open existing .csv file using c# and how cani add another sheet to that file . please help me.
    thank you in advance

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: how to open .csv file

    Quote Originally Posted by jagadeeshv View Post
    hi ,this is jagadeesh kumar ,in my project i have to open existing .csv file using c# and how cani add another sheet to that file . please help me.
    thank you in advance
    Simple use the Excel classes by adding them to the project. Which one depends on what excel file it should create e.g. Excel 2003 or 2007

    The open command can open the file and then save it as a separate xls or xlsx file. Then close Workbook and quit ecel application Thats all
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: how to open .csv file

    CSV stands for comma seperated values. A record is stored on each row with fields seperated by commas. So if you had a CSV file representing students with three fields - name, age, grade - then you could get a file that looks like:

    Code:
    Bob,19,92.5
    Sally,20,96.3
    Joe,20,44.5
    You could read them with something like this
    Code:
    System.IO.StreamReader r = new StreamReader(path);
    while( !r.EndOfStream )
    {
        string line = r.ReadLine();     //Read the line
        string[] fields = line.Split(','); //Split the line into fields whenever a comma is encountered
    
        //Parse fields into appropriate datatypes
        string name = fields[0];
        int age = Int32.Parse(fields[1]);
        double grade = Double.Parse(fields[2]);
    
        //Do something with these 
    }
    Beware, some CSV files use quotes around fields. Usually this is done to allow commas to be stored in the fields. A slightly more complicated parsing routine would be needed.

    Sheets are a property of excel (.xls or .xlsx files), not of CSV files. The closest you could come would be to write another CSV file.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to open .csv file

    And, you have to replace the file in each case
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)