Excel in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Excel in c++

Hello one way to read excel file is using fstream objects... I am not looking for same.... is there a good approach to read excel data using c++ ? I am looking for some good site or document which talks about excel reading using c++.

4th Jul 2019, 3:07 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
9 Answers
+ 2
Most Excel data can be, or is delimited with commas. This is called csv data or, Comma Separated Values. Try finding ways to parse csv files, and ways to export your Excel files into csv format. Good luck!
4th Jul 2019, 4:38 AM
Zeke Williams
Zeke Williams - avatar
+ 2
Are you using a string buf object when reading? Because I actually had a similar issue recently with reading special characters as well. My fix was to use getline(istream, string), and that worked because the file I was reading was just one long line.
10th Jul 2019, 4:23 PM
Zeke Williams
Zeke Williams - avatar
+ 1
Zeke Williams this is okay... we can obviously do the same... but as we can read XML file using msxml, do we have something for Excel or not ?
5th Jul 2019, 6:42 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
that parsing code becomes difficult or cumbersome... it also not that much readable...
5th Jul 2019, 6:43 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
yes I am using string only.... below is sample code : ifstreamĀ fs; fs.open(lFilePath.c_str(), std::ios::binary ||ios::binary); stringĀ line; getline(fs, line); but I think I have mislead you while asking question... I am not complaining about unable to read special characters... below is my issue : sample excle file test.xlsx first row has a b c and d in to column 1 to 4...then in second row , data in column 1 to 8 is e f g and h... now I am saving this file as test.csv... so , ideally CSV reading should have below two lines : a,b,c,d e,f,g,h but it gives me as below few unknown characters a,b,c,d e,f,g,h it means some extra special characters are added unnecessary while reading through code for first line only... why this happens nd how to avoid this problem? I am using visual studio 12 for c++ nd Excel version is 2016... code works fine if I manually create text file , write two lines with comma seperated values , save it and change extension from .TXT to .CSV..
10th Jul 2019, 4:39 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
I'm not sure. One of my solutions was to read the whole line into the string and then get the position of the first valid character. std::getline(in, str); int pos = str.find_first_of("abcdefghi..."); Then I take a substring of that to cut off the weird characters. str = str.substr(pos, str.length() - pos - 1); This works fine, no matter what the first line starts with. I also didn't open my file in binary mode.
10th Jul 2019, 4:52 PM
Zeke Williams
Zeke Williams - avatar
+ 1
thanks...this should solve the purpose...
10th Jul 2019, 5:32 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
okay..finally I decided to go with CSV... when I manually create XLSX nd save it as CSV, there is some special characters being recognised along with contents (for first line only) while reading CSV file through code... if I directly create a file in notepad editor, save it and changes extension from .TXT to .CSV; it works fine...how to avoid special characters while reading...
10th Jul 2019, 4:15 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I came across a situation nd got confused about what to do...my excel cell itself has value which contains comma... now how to differentiate between CSV comma nd cell value comma during delimiting
12th Jul 2019, 2:57 AM
Ketan Lalcheta
Ketan Lalcheta - avatar