MFC lesson 4 manipulating EXCEL tables using ODBC
Option 1: make use of ready-made third-party library: CSpreadSheet
The limitation of using CSpreadSheet to operate EXCEL: you can only create a table in EXCEL, and then you can manipulate the EXCEL table in the way of ODBC, otherwise the table name after FROM in the query statement cannot be filled in.
Limit
This control needs MFC (Microsoft Foundation Class Library) support. Support for Unicode encoding was not tested. Control to read and write with ODBC
Excel file, ODBC driver is required. Excel file must be marked with columns. And the first row mark is unique (field). Prohibited
Delete the workbook, only the contents of the workbook are allowed 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
Plan 2: write and access the database by yourself
The 12345678910111213141516171819202122CDatabase db;// database library needs to contain the header file # include CString sDriver = _ T ("MICROSOFTEXCEL DRIVER (* .XLS)"); / / Excel driver CString sSql; charszRecord [6] [128] = {0}; sSql.Format (_ T ("DRIVER= {% s}; DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"% s\ "; DBQ=%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 () / / previous field pset.MoveNext ();} db.Close ()
Note:
1) obtain the corresponding attribute values in the current row through GetFieldValue
2) the sSql statement is a query statement, where Sheet1 is the table name, indicating that the table must be created in the exported file before it can be imported.