I. Write an Excel document.
// Obtain the program directory GetCurrentDirectory (MAX_PATH, g_csPathBuff); // create and write the Excel file CDatabase database; char * sDriver = "microsoft excel driver (*. XLS) "; // Excel installation driver char sExcelFile [MAX_PATH]; sprintf (sExcelFile," % s \ data.xls ", g_csPathBuff ); // The Excel file char sSql [500]; DeleteFile (sExcelFile); TRY {// create a sprintf (sSql, "DRIVER = {% s}) for access }; DSN = '''; FIRSTROWHASNAMES = 1; READONLY = FALSE; CREATE_DB = \ "% s \"; DBQ = % s ", sDriver, sExcelFile, sExcelFile ); // create a database (an Excel file. openEx (sSql, CDatabase: noOdbcDialog); // create a table structure (number of detonators, blasting coordinates, blasting date,) sprintf (sSql, "create table batch % d (detonator quantity TEXT, blasting date TEXT, longitude TEXT, latitude TEXT, blasting personnel number TEXT, blasting device number TEXT, Information Status TEXT, detonator number TEXT, detonator id text, delay time TEXT) ", I); // execute the SQL statement database. executeSQL (sSql); sprintf (sSql, "insert into batch % d (quantity of detonator, blasting date, longitude, latitude, blasting personnel number, blasting device number, Information Status) VALUES (% s, % d, % s) ", I, g_DBDArray [I]. ucDetonatorLength, g_DBDArray [I]. ucBlastingDate, csJingdu, csWeidu, g_DBDArray [I]. ucOperatorID, g_nOperatorDev, g_DBDArray [I]. ucIsRead); // execute the SQL statement database. executeSQL (sSql);} CATCH (CMemoryException, e) {AfxMessageBox ("An error occurred while writing data to Excel"); database. close ();} END_CATCH
Ii. Read the Excel document.
1. First, enter the following support classes.
2. How to add (that is, add an excel header file)
3. Remember to add the AfxOleInit () function initialization component at the beginning of the InitInstance function in the App class.
After completing step 2 in Step 4, you can write the code for opening the. xls document.
// TODO: Add your control notification handler code here _ Application objApp; // determines whether the EXCEL Application Workbooks objBooks; _ Workbook objBook; Worksheets objSheets; _ Worksheet objSheet; Range objRange, usedRange; // cell to get Font font; objApp. createDispatch ("Excel. application "); // determine whether the EXCEL Application COleVariant VOptional (long) DISP_E_PARAMNOTFOUND, VT_ERROR); objApp. setVisible (TRUE); objApp. setUserControl (TRUE); LPDISPATCH pwkbooks = objApp. getWorkbooks (); objBooks. attachDispatch (pwkbooks, TRUE); objBooks = objApp. getWorkbooks (); // objBook = objBooks. add (VOptional); objSheets = objBook. getSheets (); char sExcelFile [MAX_PATH]; sprintf (sExcelFile, "% s \ data.xls", g_csPathBuff); // the Excel file objBooks to be created. open (sExcelFile, VOptional, kernel, VOptional, kernel, kernel, VOptional, VOptional );
5. The effect is as follows.