VC6.0 to read Excel data method

  • 2020-04-02 02:28:40
  • OfStack

Excel is one of the commonly used office software, this paper takes Excel2003 as an example to illustrate the VC program to read Excel file method. The specific steps are as follows:
 
1. First, add the excel class to the project.

In the ClassWizard, [Add Class], find Excel. Exe in the Excel installation directory (Microsoft2003 is Excel. Exe; 2007 should be independent of the lib library, this is not verified), add the necessary several classes:


//Excel application object
_Application  m_oExcelApp;   //Excel program
_Worksheet  m_oWorkSheet;   //The worksheet
_Workbook  m_oWorkBook;   //workbook
Workbooks  m_oWorkBooks;  //workbook A collection of 
Worksheets  m_oWorkSheets;  //The worksheet A collection of 
Range  m_oCurrRange;   //Use area

The files added to the project are excel. H and excel. CPP.

2. Initialize the Com library


if (CoInitialize(NULL)!=0)
{
    AfxMessageBox(" Initialize the COM Support library failed !");
    exit(1);
}

3. Read the data in the file


if (!m_oExcelApp.CreateDispatch( _T( "Excel.Application" ), NULL ) )
{
   ::MessageBox( NULL, _T( " create Excel Service failed! " ), _T( " Error! " ), MB_OK | MB_ICONERROR);
   exit(1);
}

//Set to display
m_oExcelApp.SetVisible(FALSE);
m_oWorkBooks.AttachDispatch( m_oExcelApp.GetWorkbooks(), TRUE ); //Without this statement, open the file next and return a failure.

LPDISPATCH lpDisp = NULL;
COleVariant covTrue((short)TRUE);
COleVariant covFalse((short)FALSE);
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); 
Range  oCurCell;

//Open the file
lpDisp = m_oWorkBooks.Open( strFilePath,
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing),
    _variant_t(vtMissing) );
//Get the WorkBook for the activity
m_oWorkBook.AttachDispatch( lpDisp, TRUE );
//Get the WorkSheet for the activity
m_oWorkSheet.AttachDispatch( m_oWorkBook.GetActiveSheet(), TRUE );
//Get the Range of areas to use
m_oCurrRange.AttachDispatch( m_oWorkSheet.GetUsedRange(), TRUE );

//Gets the number of rows used
long lgUsedRowNum = 0;
m_oCurrRange.AttachDispatch( m_oCurrRange.GetRows(), TRUE );
lgUsedRowNum = m_oCurrRange.GetCount();
//Gets the number of columns used
long lgUsedColumnNum = 0;
m_oCurrRange.AttachDispatch( m_oCurrRange.GetColumns(), TRUE );
lgUsedColumnNum = m_oCurrRange.GetCount();
//Read the name of the Sheet
CString strSheetName = m_oWorkSheet.GetName();

//I get all of the Cells, and now CurrRange is the set of Cells
m_oCurrRange.AttachDispatch( m_oWorkSheet.GetCells(), TRUE );

//Go through the entire Excel spreadsheet
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 );
   //Determines if the cell is merged
   VARIANT varMerge = oCurCell.GetMergeCells();
   if ( varMerge.boolVal == -1 )
   {
//AfxMessageBox(_T) (" is the merged cell!" ));
   }
   else if ( varMerge.boolVal == 0 )
   {
//AfxMessageBox(_T(" not a merged cell!" ));
   }

   arrayStr[i].Add( strItemName );
   j++;
   }
   i++;
}

//Update list control data
m_pExcelOperDlg->initListCtrlColumn( lgUsedColumnNum );
m_pExcelOperDlg->updateListCtrlData( arrayStr, lgUsedRowNum );

//Release 2d array
delete[] arrayStr;

//Shut down
m_oWorkBook.Close( covOptional, COleVariant( strFilePath ), covOptional );
m_oWorkBooks.Close();
//The release of
m_oCurrRange.ReleaseDispatch();
m_oWorkSheet.ReleaseDispatch();
m_oWorkSheets.ReleaseDispatch();
m_oWorkBook.ReleaseDispatch();
m_oWorkBooks.ReleaseDispatch();
m_oExcelApp.ReleaseDispatch();
m_oExcelApp.Quit();  //This statement launches an Excel program, and the Excel process in the task manager will automatically end.

Supplement: click here to complete source code described in this article application instance (link: http://xiazai.jb51.net/201407/yuanma/ExcelOper (jb51.net). Rar)


Related articles: