Can someone advise an easy reference for importing data from an excel spreadsheet into a visual c++ console program to do calculations.
Thanks
Michael
Printable View
Can someone advise an easy reference for importing data from an excel spreadsheet into a visual c++ console program to do calculations.
Thanks
Michael
Save the Excel data as a tab-delimited text file and read it in your program with std::ifstream.
Can you indicate the precise wording of the commands please.
Where can I find some written material on the subject.
Thanks and regards
Michael
*Open the spreadsheet
*File->Save as...
*Choose tab-delimited file as 'file format'
*Press OK
I have a Swedish version of Excel, so I'm not 100% about the exact wording of the commands.
Then open the .txt file using std::ifstream in your C++ program. There are millions of tutorials about using ifstream on the Internet.
What if I want to import directly from the excel file without converting it to text?
Thanks
Michael
This isn't my code, its just something I saved from this very board! I thought it might come in handy one day so:
:)
Code:#pragma warning (disable:4146)
#import "c:\Program Files\Common Files\Microsoft Shared\Office10\mso.dll"
#import "c:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.olb"
#import "C:\Program Files\Microsoft Office\Office10\excel.exe" \
rename("DialogBox", "ExDialogBox") rename("RGB", "ExRGB")
#include <windows.h>
#include <iostream>
int main(){
CoInitialize(0);
try{
Excel::_ApplicationPtr lpApp("Excel.Application");
Excel::_WorksheetPtr lpWkst = 0;
lpApp->PutVisible(0,TRUE);
lpApp->Workbooks->Add();
lpWkst = lpApp->ActiveSheet;
lpWkst->Range["A1"]->Value2 = "Forename";
lpWkst->Range["B1"]->Value2 = "Score";
lpWkst->Range["A2"]->Value2 = "Dave";
lpWkst->Range["B2"]->Value2 = 40L;
lpWkst->Range["A3"]->Value2 = "Andrew";
lpWkst->Range["B3"]->Value2 = 35L;
lpWkst->Range["A4"]->Value2 = "Simon";
lpWkst->Range["B4"]->Value2 = 29L;
lpWkst = 0;
lpApp = 0;
}
catch(_com_error& e){
std::cout << "Error ";
std::cout << static_cast<char*>
((e.Description().length() ? e.Description() : ""));
return 1;
}
CoUninitialize();
return 0;
}
Do you see my above response? If not, please tell me and I'll repost.
OK I saw your response thanks.
Yeah....I thought it looked familiar ;)Quote:
Originally posted by Speedy5
This isn't my code, its just something I saved from this very board! I thought it might come in handy one day so:
If you want to work with Excel data properly, you need to learn COM.......then the data in each cell is available in the form of a VARIANT (Could be a BSTR, an in, a bool...etc), and with the wrapper classes in tools like VC++ its very easy to work with them
OK thanks where can I read some easy to understand material on this COM stuff: variants wrapper classes etc.
I often have excel worksheets that I would like to work on and write the results in another worksheet. As I am learning C++I would like to use c++ code for these programs.
Thanks
Michael