Thread: Write/Read Excel Cells

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    Write/Read Excel Cells

    I wonder if anybody has advice on file control where i want to actually read in the data from an excel spreadsheet, to my program for analysis. I have written data out to .xls no worries but it always writes the data just down the first column on the sheet, what if i wanted to fill the sheet across five cells then down ten for example.
    My main query is with reading the data though, i will be copying and pasting figures into the sheet, nothing fancy, just numbers,
    they will be divided into 10 blocks, or maybe ten sheets/files if thats easier.
    thanks for any ideas!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are MUCH better off reading the excel data in a format of CSV or other text-based format. The internal excel format is very complex, and unless you want really complex code and lots of hard reverse-engineering efforts, you are better off saving your file in a format that is reasonably easy to read [such as "Comma Separated Vector", CSV - just read the parts between the commas, and stuff it into a vector].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unless you use ODBC?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Unless you use ODBC?
    Yes, that would work too - and make it easy on a transisition to using for instance MS SQL Server or mySQL to store a large(r) database in the future. Added complication is that you have to have a SQL server of some sort [MS Access will do fine tho'].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    There is a very basic example of how to create, write and read an Excel spreadsheet at this link

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I have written data out to .xls no worries but it always writes the data just down the first column on the sheet
    How are you doing that?

    gg

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    thanks for the info folks, i am back from hols :-) so have not posted any replies.. in answer to Codeplug i just specify the output file as filename.xls and heypresto, there it is, i open excel and as i say, the output from the program is in the A column cells, sent by the loop.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rogster001 View Post
    thanks for the info folks, i am back from hols :-) so have not posted any replies.. in answer to Codeplug i just specify the output file as filename.xls and heypresto, there it is, i open excel and as i say, the output from the program is in the A column cells, sent by the loop.
    How are you creating this file? It sounds like you are getting a number of lines, instead of columns [and it's most likely a text file called .xls?]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    no, really, if i go to my c drive or location i specified for the file to be created it will show as an Excel icon, and open as regular spreadsheet, i see wot you saying though, maybe Excel just obliges and opens this txt file for me with the data organised as i described. i havenot used this for a while, its from some old work and i was thinking if i could use it in new plans.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The icon in Explorer is strictly based on the filename itself, not on the content.

    Say I do:
    Code:
    c:\>touch foo.xls
    that creates an empty file called foo.xls - and it shows an Excel logo for that file - but it's certainly not a valid Excels Spreadsheet.

    If you open the file with Notepad, does the file show as text then?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    not tried it, files not here, i will check later though, so you think excel is just being obliging and opening it anyway?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rogster001 View Post
    not tried it, files not here, i will check later though, so you think excel is just being obliging and opening it anyway?
    I would think so. Excel certainly happily opens my "foo.xls" that is the empty file from above.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    hehe , ok, hey whats this 'foo' lark i keep seeing when people are just quoting file examples...acronym for something i suppose?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    hehe, the files open fine with notepad, simple text.

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. Dissecting an Excel XML spreadsheet
    By desmond5 in forum C++ Programming
    Replies: 1
    Last Post: 05-22-2008, 04:32 PM
  3. Merging the cells of excel from vc++
    By yongzai in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2006, 03:42 AM
  4. excel spreadsheets / blank cells
    By xddxogm3 in forum Tech Board
    Replies: 3
    Last Post: 06-14-2005, 12:10 PM
  5. C++ Builder and Excel files
    By mfdemi in forum Windows Programming
    Replies: 3
    Last Post: 09-24-2004, 01:51 AM