Link to home
Start Free TrialLog in
Avatar of ommanar
ommanar

asked on

how to read an excel file in vc++?

I need to read an excel file , then sen email with the information in the sheet
Avatar of jkr
jkr
Flag of Germany image

Well, that's not really a trivial task, but fortunately you're not alone - see

http://www.codeproject.com/KB/office/BasicExcel.aspx ("BasicExcel - A Class to Read and Write to Microsoft Excel")
http://www.codeproject.com/KB/IP/zsmtp.aspx ("CFastSmtp - Fast and easy SMTP class")

Both articles come wit hfull source code and should help you to achieve your goal.
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ommanar
ommanar

ASKER

how I can go with CSV
Avatar of ommanar

ASKER

how can I do it with csv
Err - your question was "how to read an excel file in VC++", wasn't it?
Avatar of ommanar

ASKER

yes I tried the code you gave me , it does not work
Of course it works. Any error messages?
Avatar of ommanar

ASKER

the error is No such file or directory
is it work for VC++2008?
>>>> how I can go with CSV

Read it in like the following:

     string line;
     vector<vector<string> > table;
     int nrows = 0;
     ifstream ifs("myxls.csv");
     while (getline(ifs, line))
     {
           int pos1 = 0;
           int pos2 = 0;
           table.push_back(vector<string>());
           line += ';';   // add a separator (assuming it is ;)
           while ((pos2 = (int)line.find(';', pos1)) != string::npos)
           {
                  table[nrows].push_back(line.substr(pos1, pos2-pos1));
                  pos1 = pos2+1;
           }
           nrows++;
     }

You left out the most important part:


MessageBox(NULL,"Please use Excel to export the data to CSV","Attention!",MB_OK);

Open in new window

>>>> You left out the most important part:
You can automate that with excel. Whenever the .xls was stored, the .csv was stored as well. A MessageBox is not needed. On the contrary, unwished message boxes even in silent mode were the most experienced reasons for the automation with excel not to work.

Note, I did both ways in various projects. The automation using excel is the far less stabile way cause it often fails if users have an active excel open and stay there in a modal dialog. The excel solution also is of less benefit for a server which needs to access excel files in a silent mode. Here we often experienced breaks because of automatic installations, driver errors and other 'hell' scenarios where for sudden a mostly working automation interactively posts a message box what stopped the whole process....  No, your comment shows how less experience you have with that ...

I know that you can automate that, but then this is no longer less complicated than directly accessing an Excel file...
>>>> I know that you can automate that, but then this is no longer less complicated than directly accessing an Excel file...

I don't want to quarrel. But storing an excel file as a .csv is a one-time automation. Once done, there is no issue anymore. Keeping up an excel automation on an .xls is a complex thing *any* time you were doing it. You actually can't compare these complexities.

Note, my solution is a valid and proofed way. I posted it cause I knew it worked and that it could reduce the efforts for implementing it to a fraction of that doing real automation. But I also knew that it wasn't *the* solution for all cases of automation with excel. That's why I didn't post code with my first comment as I often experienced that easy and pragmatic solutions have a bad reputation among developers. Here I had been lucky. The questioner realized that my suggestion was practicable and simple. Thanks.