Reading from Excel File Visual C++ 2010 Express Edition
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Reading from Excel File Visual C++ 2010 Express Edition

  1. #1
    Join Date
    Jul 2012
    Posts
    6

    Reading from Excel File Visual C++ 2010 Express Edition

    Hello all,
    I am wanting to make a program that reads from an excel file and then writes that data to a csv file. I am aware I can do this through Excel and VBA, but this is not the best approach for my application. I need a simple console application that does this in the background without bringing up Excel and then I need to delete the file I made shortly afterwards.
    I understand basics of C++, but every example I see is related to a actual windowed build with Visual C++ and nothing works for a console build. There are several tutorials that take Excel to HTML from C++, but I can't seem to get their code to compile, or have enough experience using any of their classes to modify them for my needs.
    If someone could give me a simple snipet of code that will open up an excel file, read from a specific cell on a specific sheet and print that value to the screen, I would be forever grateful. After I get that information, I will be able to do the rest of what I am wanting to do.
    Thanks in advance for any help!

    Nathan

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,544

    Re: Reading from Excel File Visual C++ 2010 Express Edition

    You could use ADO to read data from excel sheet and then write them to text file.
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2012
    Posts
    6

    Compile Error

    Thanks for the point to ADO. I am trying to compile a sample that I found online, but I am missing something to include/import/namespace, something. I just don't know exactly what I am missing?
    I have been looking at everything I can find on ADO, and I found some example code, but unfortunately all written by Japanese people, so I can't read the comments at all, and they seem pretty advanced.
    Any help with what I am missing would be awesome
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <vector>
    
    #import "msado15.dll" \
    	no_namespace rename("EOF", "EndOfFile")
    	
    int _tmain(int argc, _TCHAR* argv[])
    {
    		//Connection string
    		std::string con_string = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents and Settings\\nathan.sizemore\\My Documents\\Purchase_Req_Form.xlsm;Extended Properties='Excel 12.0 Macro;HDR=NO'";
    	
    		//Create and open a Recordset
    		TESTHR(pRec.CreateInstance(__uuidof(Recordset)));       
    		TESTHR(pRec->Open("SELECT * FROM [Plain_VDR$]", con_string, adOpenStatic, adLockOptimistic, adCmdText));
    	
    		//Extract values of cells
    		while (!pRec->adoEOF)
    		{
    			for (long i = 0; i < pRec->Fields->GetCount(); ++i)
    			{
    				if (i > 0) 
    				{
    					stream<<";";
    				}
    				
    				_variant_t v = pRec->Fields->GetItem(i)->Value;
    				
    				if(v.vt == VT_R8)                                           
    				{
    					stream<<v.dblVal;
    				}
    				if(v.vt == VT_BSTR)
    				{
    					stream<<(char*)(_bstr_t)<<v.bstrVal;
    				}
    			}
    			
    			stream<<std::endl;
    			pRec->MoveNext();
    		}
    }
    This is the error list I am receiving
    Code:
    Error	1	error C2065: 'pRec' : undeclared identifier
    Error	2	error C2228: left of '.CreateInstance' must have class/struct/union
    Error	3	error C3861: 'TESTHR': identifier not found
    Error	4	error C2065: 'pRec' : undeclared identifier
    Error	5	error C2227: left of '->Open' must point to
    Error	6	error C3861: 'TESTHR': identifier not found
    Error	7	error C2065: 'pRec' : undeclared identifier
    Error	8	error C2227: left of '->adoEOF' must point to class/struct/union/generic type
    Error	9	error C1903: unable to recover from previous error(s); stopping compilation
    	10	IntelliSense: identifier "TESTHR" is undefined
    	11	IntelliSense: identifier "pRec" is undefined
    	12	IntelliSense: identifier "stream" is undefined
    	13	IntelliSense: identifier "stream" is undefined
    	14	IntelliSense: identifier "stream" is undefined
    	15	IntelliSense: type name is not allowed
    	16	IntelliSense: identifier "stream" is undefined
    Thanks again for any help on these issues...

    Nathan

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,205

    Re: Reading from Excel File Visual C++ 2010 Express Edition

    You can do it using ODBC and CRecordset also.

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,544

    Re: Reading from Excel File Visual C++ 2010 Express Edition

    Quote Originally Posted by GCDEF View Post
    You can do it using ODBC and CRecordset also.
    AFAIK VS 2010 Express does not contain MFC.
    Victor Nijegorodov

  6. #6
    Join Date
    Jul 2012
    Posts
    6

    Re: Reading from Excel File Visual C++ 2010 Express Edition

    ADO seems to have what I need. Any idea on what I am missing to include or namespace I should be using to resolve these erros?

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,051

    Re: Reading from Excel File Visual C++ 2010 Express Edition

    You have missed to define a pRec smartpointer (seems should be _RecordsetPtr, see in generated .thi). The same to TESTHR macro. It looks like you copied the snipped from somewhere, and the copy appears incomplete.
    Best regards,
    Igor

  8. #8
    Join Date
    Jul 2012
    Posts
    6

    Re: Reading from Excel File Visual C++ 2010 Express Edition

    Quote Originally Posted by Igor Vartanov View Post
    You have missed to define a pRec smartpointer (seems should be _RecordsetPtr, see in generated .thi). The same to TESTHR macro. It looks like you copied the snipped from somewhere, and the copy appears incomplete.
    Yeah, I noticed that after I downloaded the working source code and compiled. I've never gotten this detailed with C++ before (Assigning variant classes, include SQL DB stuff) and am trying to hack my way through it, but it is very cumbersome to figure out errors when you have no idea what they mean or enough of the language to know where to start looking.
    After I get this thing actually working and know what each thing does, I will throw up my code because it seems to be a very needed topic and not that many step by step tutorials. Seems to be more of one decent C++ developer telling another C++ developer what Libraries to use. Nothing seems to capture the fact that maybe a developer not that familiar with the C++ language needs to do this task, but doesn't know enough about the language, yet, to get there.

    Nathan

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,051

    Re: Reading from Excel File Visual C++ 2010 Express Edition

    In fact, you hit a very specific topic of importing COM server in your C++ program. Very little it has to do with C++ real complexities, but requires quite a good knowledge of COM technology.

    I don't know the background, but say in VB, VBScript or JScript it would look much and much simpler, and maybe you should try with some language other than C++.
    Best regards,
    Igor

  10. #10
    Join Date
    Jul 2012
    Posts
    6

    Re: Reading from Excel File Visual C++ 2010 Express Edition

    I'm too close to turn back now Only have to figure out how to select a certain 'Sheet' and everything else should work fine.

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,051

    Re: Reading from Excel File Visual C++ 2010 Express Edition

    Try this: Office Space: Tips and Tricks for Scripting Microsoft Office Applications

    What you to focus on is ADO connection string and SQL query.
    Last edited by Igor Vartanov; July 6th, 2012 at 05:21 PM.
    Best regards,
    Igor

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,051

    Re: Reading from Excel File Visual C++ 2010 Express Edition

    Best regards,
    Igor

  13. #13
    Join Date
    Jul 2012
    Posts
    6

    Re: Reading from Excel File Visual C++ 2010 Express Edition

    Thanks for the replies Igor, very helpful.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center