Control excel under VC (excellent tutorial)

Source: Internet
Author: User
Original post address: publish (Here we use 2003 as an example ). 1.

First, add the Excel class to the project. In classwizard, [add slave; 2007 should have an independent lib library, which is not verified), add several necessary classes: // Excel application object _ application m_oexcelapp; // Excel
Project_worksheet m_oworksheet ;//
Worksheet _ workbook m_oworkbook ;//
Workbooks m_oworkbooks ;//
Worksheets m_oworksheets ;//
Worksheet set range m_ocurrrange ;//
The files added to the project by region are excel. h and Excel. cpp. 2.

Initialize the com library if (coinitialize (null )! = 0) {afxmessagebox ("
Failed to initialize com support library! "); Exit (1);} 3.

Read the data in the file if (! M_oexcelapp.createdispatch (_ T ("Excel. application"), null) {: MessageBox (null, _ T ("
An error occurred while creating the Excel service! "), _ T (" error prompt! "), Mb_ OK | mb_iconerror); exit (1 );}//
Set to display m_oexcelapp.setvisible (false); m_oworkbooks.attachdispatch (m_oexcelapp.getworkbooks (), true );//
Without this statement, opening the file below will fail. Lpdispatch lpdisp = NULL; colevariant covtrue (short) True); colevariant covfalse (short) False); colevariant covoptional (long) handle, vt_error); range ocurcell ;//
Open the file lpdisp = require (strfilepath, _ variant_t (vtmissing), _ variant_t (vtsing mis), _ variant_t (vtmissing ), _ variant_t (vtmissing ), _ variant_t (vtmissing), _ variant_t (vtmissing), _ variant_t (vtmissing ));//
Obtain the active workbook (workbook) m_oworkbook.attachdispatch (lpdisp, true );//
Get the active Worksheet (worksheet) m_oworksheet.attachdispatch (m_oworkbook.getactivesheet (), true );//
Obtain the range (region) m_ocurrrange.attachdispatch (m_oworksheet.getusedrange (), true) of the region in use );//
Long lgusedrownum = 0; m_ocurrrange.attachdispatch (m_ocurrrange.getrows (), true); lgusedrownum = m_ocurrrange.getcount ();//
Obtain the number of columns used: Long lgusedcolumnnum = 0; m_ocurrrange.attachdispatch (m_ocurrrange.getcolumns (), true); lgusedcolumnnum = m_ocurrrange.getcount ();//
Read the sheet name cstring strsheetname = m_oworksheet.getname ();//
Obtain all cells. In this case, currrange is the set of cells m_ocurrrange.attachdispatch (m_oworksheet.getcells (), true );//
Traverse the entire Excel table cstringarray * arraystr; arraystr = new cstringarray [lgusedrownum]; for (INT I = 0; I <lgusedrownum;) {for (Int J = 1; j <= lgusedcolumnnum ;){
Ocurcell. attachdispatch (m_ocurrrange.getitem (colevariant (long) (I +
1), colevariant (long) J )). pdispval, true); variant varitemname = ocurcell. gettext (); cstring stritemname; stritemname = varitemname. bstrval; // afxmessagebox (stritemname );//
Determine whether the merged cell variant varmerge = ocurcell. getmergecells (); If (varmerge. boolval =-1) {// afxmessagebox (_ T ("
Is the merged cell! ");} Else if (varmerge. boolval = 0) {// afxmessagebox (_ T ("
It is not a merged cell! ");} Arraystr [I]. Add (stritemname); j ++;} I ++ ;}//
Update list control data m_pexceloperdlg-> initlistctrlcolumn (lgusedcolumnnum); m_pexceloperdlg-> updatelistctrldata (arraystr, lgusedrownum );//
Release a two-dimensional array Delete [] arraystr ;//
Close m_oworkbook.close (covoptional, colevariant (strfilepath), covoptional); m_oworkbooks.close ();//
Release release (); m_oworksheets.releasedispatch (); m_oworkbook.releasedispatch (); m_oworkbooks.releasedispatch (); release (); m_oexcelapp.quit ();//
This statement is an excel program, and the Excel process in the task manager will automatically end.

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.