leer ficheros .xls con c++
Publicado por Pablo (5 intervenciones) el 11/07/2006 12:44:55
Hola a todos,
necesito hacer un programa en c++ para leer ficheros con formato .xls. Soy nuevo en c++, así que he buscado códigos hechos con google y he encontrado algo. Con la información que he encontrado he escrito mi código, pero al compilar (con visual c 2005 express edition), me sale un error diciendo que me falta sqltypes.h. He buscado el fichero con google y lo he copiado en la carpeta include de visual c++, pero haciendo eso y volviendo a compilar aparecen más de 150 errores (todos localizados en sqltypes.h. He probado con varios ficheros sqltypes.h y con todos pasa lo mismo). Pongo aquí el código que he desarrollado por si alguien me puede echar una mano.
// excel.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//#include "ReadExcel.h"
//#include "ReadExcelDlg.h"
#include "odbcinst.h"
int _tmain(int argc, _TCHAR* argv[])
{
CDatabase database;
CString sSql;
CString sItem1, sItem2;
CString sDriver;
CString sDsn;
CString sFile = "prueba.xls";
sDriver = GetExcelDriver();
if(sDriver.IsEmpty()){
// Blast! We didn´t find that driver!
AfxMessageBox("No Excel ODBC driver found");
return;
}
// Create a pseudo DSN including the name of the Driver and the Excel file
// so we don´t have to have an explicit DSN installed in our ODBC admin
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY{
// Open the database using the former created pseudo DSN
database.Open(NULL,false,false,sDsn);
// Allocate the recordset
CRecordset recset(&database);
// Build the SQL string
// Remember to name a section of data in the Excel sheet using "Insert->Names" to be
// able to work with the data like you would with a table in a "real" database. There
// may be more than one table contained in a worksheet.
sSql = "SELECT field_1, field_2 "
"FROM demo_table "
"ORDER BY field_1";
// Execute that query (implicitly by opening the recordset)
recset.Open(CRecordset::forwardOnly,sSql,CRecordset::readOnly);
// Browse the result
while( !recset.IsEOF() )
{
// Read the result line
recset.GetFieldValue("field_1",sItem1);
recset.GetFieldValue("field_2",sItem2);
// Insert result into the list
m_ctrlList.AddString( sItem1 + " --> "+sItem2 );
// Skip to the next resultline
recset.MoveNext();
}
// Close the database
database.Close();
}
CATCH(CDBException, e)
{
// A database exception occured. Pop out the details...
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
return 0;
}
No se si tengo que instalar algo que me falta...
Por favor, que alguien me ayude.
MUCHAS GRACIAS.
necesito hacer un programa en c++ para leer ficheros con formato .xls. Soy nuevo en c++, así que he buscado códigos hechos con google y he encontrado algo. Con la información que he encontrado he escrito mi código, pero al compilar (con visual c 2005 express edition), me sale un error diciendo que me falta sqltypes.h. He buscado el fichero con google y lo he copiado en la carpeta include de visual c++, pero haciendo eso y volviendo a compilar aparecen más de 150 errores (todos localizados en sqltypes.h. He probado con varios ficheros sqltypes.h y con todos pasa lo mismo). Pongo aquí el código que he desarrollado por si alguien me puede echar una mano.
// excel.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//#include "ReadExcel.h"
//#include "ReadExcelDlg.h"
#include "odbcinst.h"
int _tmain(int argc, _TCHAR* argv[])
{
CDatabase database;
CString sSql;
CString sItem1, sItem2;
CString sDriver;
CString sDsn;
CString sFile = "prueba.xls";
sDriver = GetExcelDriver();
if(sDriver.IsEmpty()){
// Blast! We didn´t find that driver!
AfxMessageBox("No Excel ODBC driver found");
return;
}
// Create a pseudo DSN including the name of the Driver and the Excel file
// so we don´t have to have an explicit DSN installed in our ODBC admin
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY{
// Open the database using the former created pseudo DSN
database.Open(NULL,false,false,sDsn);
// Allocate the recordset
CRecordset recset(&database);
// Build the SQL string
// Remember to name a section of data in the Excel sheet using "Insert->Names" to be
// able to work with the data like you would with a table in a "real" database. There
// may be more than one table contained in a worksheet.
sSql = "SELECT field_1, field_2 "
"FROM demo_table "
"ORDER BY field_1";
// Execute that query (implicitly by opening the recordset)
recset.Open(CRecordset::forwardOnly,sSql,CRecordset::readOnly);
// Browse the result
while( !recset.IsEOF() )
{
// Read the result line
recset.GetFieldValue("field_1",sItem1);
recset.GetFieldValue("field_2",sItem2);
// Insert result into the list
m_ctrlList.AddString( sItem1 + " --> "+sItem2 );
// Skip to the next resultline
recset.MoveNext();
}
// Close the database
database.Close();
}
CATCH(CDBException, e)
{
// A database exception occured. Pop out the details...
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
return 0;
}
No se si tengo que instalar algo que me falta...
Por favor, que alguien me ayude.
MUCHAS GRACIAS.
Valora esta pregunta


0