Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
4.29/5 (4 votes)
See more:
What is the best and the simplest way for Reading from and writing to Excel files in C++?I'll appreciate if anybody can help me.
Posted

You could use a .csv file - which is comma seperated values.

Each cell is seperated with a comma, and each row is ended with a CR ( endl).
Here is a simple example

#include <iostream>
#include <fstream>


using namespace std;


int main(int args, char * argv[])
{
ofstream MyExcelFile;
MyExcelFile.open("C:\\test.csv");

MyExcelFile << "First Name, Last Name, Middle Initial" << endl;
MyExcelFile << "Michael, Jackson, B." << endl;
MyExcelFile.close();
return 0;
} 


Or you can read following articles, I hope it will help you:

CSpreadSheet - A Class to Read and Write to Excel and Text Delimited Spreadsheet[^]

http://www.maths.manchester.ac.uk/~ahazel/EXCEL_C++.pdf[^]

and this:
http://support.microsoft.com/default.aspx?scid=kb;en-us;178782[^]
 
Share this answer
 
Comments
Sandeep Mewara 7-Jul-12 14:18pm    
My 5!
Volynsky Alex 8-Jul-12 15:01pm    
Thanks!!!
mk4you7 8-Jul-12 3:32am    
Very good answer! 5!!!
Volynsky Alex 8-Jul-12 15:01pm    
Thanks!!!
Excel has a lot of formats to save out a table. If you can choose one of these formats then I can give you some help. (If you have to work old excel formats then you are in trouble!) You should save your excel tables to Excel 2002 XML or Excel 2003 XML format. Then you can read/write it with a simple xml parser. (http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats[^])

Here is a refernce to the xml tags: http://msdn.microsoft.com/en-us/library/aa140066%28office.10%29.aspx[^]
If all you want is just a table that has text inside its cells, then dont read the above reference and here is my advice: create a small table (maybe 4x5) and save it as Excel 2002 or 2003 xml. Leave some random cells empty inside your table. Then open the xml file with a text editor/viewer and check out its contents. Most of the tags are just for excel so you can ignore them, you will find out which tags/attributes are important at all. (You can start delete the tags you find unimportant and check if excel can still read the table until you simplified the table to minimal - thats what you will have to save out from your code!) There is only one big mistake you can make, sometimes excel skips some empty columns when it finds some empty cells (this is why you should leave some random cells empty) end then it gives an index to the next nonempty cell in a row without saving the empty cells on the left of it. Thats it!

We use this method to give designers a tool (excel) to tweak the parameters of our program. This solution is so primitive that it can make totally platform independent! Note that you can put in some column/row headers and some coloring for your ppl and your program can ignore those. Later if you have to save space you can trim down the excel file or can convert it to a size optimized custom format.
 
Share this answer
 
v2
You will find many projects of C++ / Excel interfaces on this website. For example Saving Excel 2.1 Workbook.

As for a commercial link: Libxl seems to do well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900