VC ++ read and write open Excel document (Code backup)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.