Thread: excel/c++

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    excel/c++

    Can someone advise an easy reference for importing data from an excel spreadsheet into a visual c++ console program to do calculations.
    Thanks
    Michael

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Save the Excel data as a tab-delimited text file and read it in your program with std::ifstream.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    excel/c++

    Can you indicate the precise wording of the commands please.
    Where can I find some written material on the subject.
    Thanks and regards
    Michael

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    *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.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    What if I want to import directly from the excel file without converting it to text?
    Thanks
    Michael

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    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;
    }

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Do you see my above response? If not, please tell me and I'll repost.
    Last edited by Speedy5; 03-02-2003 at 07:51 PM.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    OK I saw your response thanks.

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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:
    Yeah....I thought it looked familiar

    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

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    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

Popular pages Recent additions subscribe to a feed