• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 648
  • Last Modified:

how to read an excel file in vc++?

I need to read an excel file , then sen email with the information in the sheet
0
ommanar
Asked:
ommanar
  • 5
  • 4
  • 4
  • +1
1 Solution
 
jkrCommented:
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.
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
0
 
itsmeandnobodyelseCommented:
If you would/could store the excel into a .csv file, the task is much easier.

Tell me if you consider to go that way.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ommanarAuthor Commented:
how I can go with CSV
0
 
ommanarAuthor Commented:
how can I do it with csv
0
 
jkrCommented:
Err - your question was "how to read an excel file in VC++", wasn't it?
0
 
ommanarAuthor Commented:
yes I tried the code you gave me , it does not work
0
 
jkrCommented:
Of course it works. Any error messages?
0
 
ommanarAuthor Commented:
the error is No such file or directory
is it work for VC++2008?
0
 
itsmeandnobodyelseCommented:
>>>> 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++;
     }

0
 
jkrCommented:
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

0
 
itsmeandnobodyelseCommented:
>>>> 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 ...

0
 
jkrCommented:
I know that you can automate that, but then this is no longer less complicated than directly accessing an Excel file...
0
 
itsmeandnobodyelseCommented:
>>>> 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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now