Thread: reading an excel file into c programme.

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    5

    reading an excel file into c programme.

    hello friends.

    I want my c programme to read an excel file.There are numbers in each cell of excel sheet.And this data consists of thousand rows and column each.This is a heavy data.Please tell me the code for reading such and an excel file.If there is other way to do this,please suggest.

    VIVEK

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Do you have to do it in C?

    There are APIs in several other languages....

    libjexcelapi-java -> Java API to read, write and modify Excel spreadsheets

    python-excelerator -> module for reading/writing Excel spreadsheet files

    libspreadsheet-parseexcel-perl -> Perl module to access information from Excel Spreadsheets

    ruby-spreadsheet -> Ruby library for manipulating MS Excel (.xls) spreadsheets


    Or perhaps use an intermediate tool such as
    xlhtml -> A program for converting Microsoft Excel Files .xls

    But I suppose this is where you tell us you're not using an open source OS like Ubuntu, but instead you're a windows user and your compiler is TurboC
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Would it be feasible to output the Excel data as a CSV file or some-such? This would make it a lot easier to read from a 'C' program (though you probably won't find anyone here willing to "tell you the code" for such a program).

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    5
    Thanks Matticus for replying.Actually I want my output in .txt format only because i want this file to be read in ENVI software.ENVI can read only text file.Finally i want that my c programme should read in excel format and output in text.Is it possible?please help me.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So why don't you just do in excel "Save as...." and choose one of the text file formats such as CSV as the output format?

    And nice job of NOT answering the questions about what OS and compiler you're using.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    I think the Excel(and other Microsoft products) .xls format is proprietary, so you just can't open up a spreadsheet and read from it.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    I think you should just follow Salem's advice - you perhaps are being blinded by the fact that you are using excel to view your data at present - What you need is a flatfile - just save your sheet as .csv - this is text but with a comma seperating the values, you can use this seperator then when you read the file with C

    If your excel workbook has several sheets of data then each of them will have to be saved as seperate .csv files

    If on the other hand you actually want to work with true excel workbooks then you can't do it with simple C you would be better considering Java or somesuch as already posted.
    Last edited by rogster001; 07-16-2012 at 02:20 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    5
    thanks I agree with your advice.I will convert my data into .csv format.But tell me one thing that the code for reading a .csv file is same as that for reading a text file?If not then can u tell me some lines of that code.it would be helpful.

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    5
    I am using Microsoft and compiler is turboc++.

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    5
    yeah Matticus that would be right to get the output in .csv but i will have to convert that file again into .txt format as the software i am working on can read only text files.Is it possible to convert .csv into .txt???Thanks

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by vivek472 View Post
    yeah Matticus that would be right to get the output in .csv but i will have to convert that file again into .txt format as the software i am working on can read only text files.Is it possible to convert .csv into .txt???Thanks

    How To Ask Questions The Smart Way

    You have been told twice in this thread that an csv file is a text file.

    Read the link then post a smart question with the details needed; so, someone can help you!

    Or, be considered a troll and/or a an idiot.

    Tim S.
    Last edited by stahta01; 07-16-2012 at 10:07 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by cfanatic View Post
    I think the Excel(and other Microsoft products) .xls format is proprietary, so you just can't open up a spreadsheet and read from it.
    Not true any more - https://www.microsoft.com/interop/do...ryFormats.mspx

    You will need a lot of reading though... and a separate knowledge of the MS OLE Document format (kind of like a fat file system in a single file)

    I wrote a python class that read the text from a word document a few months ago. A fair bit of reading and a lot of time staring at a hex editor, but it can certainly be done. Even with a POS compiler like Turbo C++

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Is it possible to convert .csv into .txt???
    stahta01 has pointed out that this has already been covered in this thread. However, even if you somehow missed it, try this:

    Step 1: Search the internet for an answer to your question
    Step 2: Find a link that might answer your question (the very first one, in this case)
    Step 3: Scroll down until you see the heading, "Export data to a text file by saving it"
    Step 4: Read the part under the heading - it will tell you exactly what you want to know
    Step 5: Do what it says.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I am using Microsoft and compiler is turboc++.
    On the off-chance that you're open to being weaned off the fossil compiler you were taught (badly) with, try some of these.
    Free Developer Tools - Visual Studio 2010 Express | Microsoft Visual Studio
    smorgasbordet - Pelles C
    Code::Blocks
    Orwell Dev-C++ | Free Development software downloads at SourceForge.net
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Some IT advice in general: Giving a file an extension means nothing - It is only a flag in windows that allows for the OS to know things about it - like what programs may be used by default to open it.
    You could call your file myData.commaSeperatedValues if you wanted - windows would just ask what program to open it with - if you choose a text editor then lo it will appear as normal text.
    You could invent your own file extension and use it in your programs, despite the fact they may be only writing text - you are not obliged to output text files as .txt
    You could call then .ppp and then register them with your installer script so that double clicking your icons launches the program etc etc. By the way there are lists of file extensions registered to products etc

    Equally you can ouput to .xls if you like - This would generate an excel spreadsheet icon - double click it - super it opens with excel - but its just text to start with. You have not written a workbook object.
    There are other operating systems than windows. UNIX for example could not care less if your file has an extension or not.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Excel Row Cells A through J
    By CheyenneWay in forum C++ Programming
    Replies: 37
    Last Post: 03-17-2011, 08:00 AM
  2. reading excel files...
    By gaah in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2005, 09:55 PM
  3. Reading Excel Files?
    By Daniel in forum C++ Programming
    Replies: 2
    Last Post: 08-13-2003, 06:03 PM
  4. Reading Excel
    By meena in forum C Programming
    Replies: 7
    Last Post: 08-05-2002, 01:03 AM