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!
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
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.
Re: Reading from Excel File Visual C++ 2010 Express Edition
Originally Posted by Igor Vartanov
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.
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++.
Bookmarks