Scenario One: Leverage ready-made third-party libraries: Cspreadsheet
Use Cspreadsheet the limitations of working with Excel : You can only create tables in Excel before you can manipulate Excel tables in an ODBC way , otherwise the query statement The table name after the from is not filled in .
Limit
the control requires MFC ( Microsoft Foundation Class Library ) support . not tested for support Unicode encoding . Control to ODBC Read-write
An Excel file that requires an ODBC driver . The Excel file must have a column tag . and the first row marks the unique ( field ). prohibited
Delete the workbook, allowing only the workbook contents to be deleted . The column value type refers to the program data type . Do not use Excel format .
Detailed reference:http://blog.csdn.net/andy205214/article/details/5918648
Scenario Two: Write your own Access database
cdatabase db;//Database library needs to contain header files #include <afxdb.h> cstring sdriver = _t ("microsoftexcel driver (*. XLS);// excel Drive CString sSql; charszRecord[6][128] = {0}; ssql.format (_t ("driver={%s};D sn="; Firstrowhasnames=1; Readonly=false; Create_db=\ "%s\";D bq=%s "), Sdriver,strfilepath, strfilepath); if (!db. OpenEx (Ssql,cdatabase::noodbcdialog))//Connect data source DJB. Xls { messagebox (_t ("Failed to open Excel file!"), _t ("error"); return ; } //Open Excel Table crecordset pset (&DB); ssql.format (_T (" Selectvalues1,values2 from sheet1 ")); pset. Open (crecordset::forwardonly,ssql,crecordset::readonly); while (!pset. IsEOF ()) { pset. GetFieldValue ();//Front field pset. MoveNext (); } db. Close ();
Attention:
1 gets the corresponding property value in the current row by GetFieldValue
2 The sSQL statement is a query statement, where Sheet1 is the table name, stating that the table must be established in the exported file before it can be imported
MFC: Using ODBC to manipulate Excel tables