Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Read from xls file

Read from xls file

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Girish601
    wrote on last edited by
    #1

    Hi guys, I am trying to write into xls file. This part is ok. problem occur while reading from xls file. error shows as follows : syntax error in query expression field_2 by demo code I use for writing into xls file is as follows. void CWrite_read_xlsDlg::OnButton1() { CDatabase database; CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; CString sExcelFile = "c:\\kdr.xls"; CString sSql; TRY { sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s", sDriver, sExcelFile, sExcelFile); // create database i.e excel sheet if( database.OpenEx(sSql,CDatabase::noOdbcDialog) ) { // create table structure sSql = "CREATE TABLE demo (Name TEXT,Address TEXT)"; database.ExecuteSQL(sSql); // insert data into table i.e excel sheet sSql = "INSERT INTO demo (Name,Address) VALUES ('Nilesh','Pune')"; database.ExecuteSQL(sSql); sSql = "INSERT INTO demo (Name,Address) VALUES ('akash','Mumbai')"; database.ExecuteSQL(sSql); sSql = "INSERT INTO demo (Name,Address) VALUES ('ajit','thane')"; database.ExecuteSQL(sSql); sSql = "INSERT INTO demo (Name,Address) VALUES ('aaniket','Nashik')"; database.ExecuteSQL(sSql); } // close database database.Close(); } CATCH_ALL(e) { TRACE1("Driver not installed: %s",sDriver); } END_CATCH_ALL; } code I use for reading data from xls file void CWrite_read_xlsDlg::OnButton2() { CDatabase database; CString sSql; CString Name, Address; CString sDriver; CString sDsn; // register datasource in ODBC database manager CString sExcelFile = "c:\\kdr.xls"; m_cList.ResetContent(); sDriver = GetExcelDriver(); if(sDriver.IsEmpty()) { AfxMessageBox(" No Excel ODBC driver found "); } sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sExcelFile); TRY { database.Open(NULL,false,false,sDsn); //A CRecordset object represents a set of records selected from a data source. //Known as recordsets CRecordset recset(&database); sSql = "SELECT field_1,field_2" "FROM demo " "ORDER BY field_1"; // sSql = "SELECT * FROM demo"; // sSql = "INSERT INTO demo (Name,Address) VALUES ('Nilesh','Pune')"; //"ORDER BY Name"; recset.Open(CRecordset::forwardOnly,sSql,CRecordset

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups