Re: have to read data from Excel
 
Gaurav wrote:
Hi,
I want to read and write the data from the excel file.
I dont know how to do this. Any pointer will be helpful
This works:
http://www.codeproject.com/database/excel_odbc.asp
I didn't find a project download but I pasted some stuff into a test
project and read the data.
LPCSTR testStr= _T("DSN=Excel
Files;DBQ=C:\\cpp\\Highlands\\Database\\Modifier.xls;DefaultDir=C:\\cpp\\Highlands\\Database;DriverId=22;FILEDSN=C:\\Program
Files\\Common Files\\ODBC\\Data Sources\\Excel Files (not
sharable).dsn;MaxBufferSize 48;PageTimeout=5;");
BOOL CDatabaseDoc::OnNewDocument()
{
    if (!CDocument::OnNewDocument())
        return FALSE;
    SQLRETURN rc;
    CDatabase db;
    db.OpenEx( testStr );
    db.Dump( afxDump );
    SQLHANDLE hstmt;
    SQLSMALLINT sint= 0;
    if( SQL_SUCCESS == ( rc = ::SQLAllocHandle( SQL_HANDLE_STMT, db.m_hdbc,
&hstmt ) ) )
    {
        if( SQL_SUCCESS == ( rc = ::SQLGetTypeInfo( hstmt, sint ) ) )
        {
            //::SQLColumns(
            int test= 0;
        }
    }
    CRecordset rs( &db );
    CString strT( "SELECT field_1, field_2 FROM demo_table ORDER BY field_1" );
    rs.Open( CRecordset::forwardOnly, strT, CRecordset::readOnly );
         while (!rs.IsEOF())
         {
            CString sItem1, sItem2;
             rs.GetFieldValue("field_1", sItem1);
             rs.GetFieldValue("field_2", sItem2);
(* I put a breakpoint here to see the data *)
             rs.MoveNext();
        break;
         }
    return TRUE;
}