Workbooks wbsMyBooks; 
        _Workbook wbMyBook;
        Worksheets wssMysheets; 
        _Worksheet wssMysheet;
        Range range; 
        Range iCell;
        LPDISPATCH lpDisp;
        COleVariant vResult;
        COleVariant covTrue((short)TRUE), covFalse((short)FALSE),
       covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
Copy the code
// Create Excel 2000 server (start Excel), try to create the EXCel_app object in initialization // to ensure that only one Excel server is opened at a timeCopy the code
// if(! M_excelapp.createdispatch ("Excel.Application")) // {// AfxMessageBox(" Unable to start Excel server!" ); // return; / /}Copy the code
//app.SetVisible(TRUE); / / make visible/Excel/ExcelApp SetUserControl (TRUE); // Allow other users to control ExcelCopy the code
/ / open c: \ \ *. XLS wbsMyBooks. AttachDispatch (m_ExcelApp. GetWorkbooks ()); LpDisp = wbsmyBooks.open (m_strPath,// There is no problem that the document cannot be opened hereCopy the code
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional); wbMyBook.AttachDispatch(lpDisp); / / get the Workbook wssMysheets. AttachDispatch (wbMyBook GetWorksheets ()); / / get the WorksheetsCopy the code
LpDisp = wbMyBook.getActivesheet (); // Get the current active sheet. wssMysheet.AttachDispatch(lpDisp);Copy the code
Range usedRange; usedRange; usedRange; usedRange.AttachDispatch(wssMysheet.GetUsedRange()); range.AttachDispatch(usedRange.GetRows()); long iRowNum = range.GetCount(); Range.attachdispatch (usedrange.getColumns ()); long iColNum = range.GetCount(); Long iStartRow = usedrange.getrow (); Long iStartCol = usedrange.getcolumn (); // Reads the value of the iStartRow row, the iStartCol column cellCopy the code
range.AttachDispatch(wssMysheet.GetCells()); range.AttachDispatch(range.GetItem(COleVariant(iStartRow),COleVariant(iStartCol)).pdispVal); vResult = range.GetValue(); CString str; If (vresult. vt == VT_BSTR) // String STR = vresult. bstrVal; Else if (vresult.vt == VT_R8) //8 bytes STR.Format("%f", vresult.dblval); Vt == VT_DATE; else if(vresult. vt == VT_DATE) // SYSTEMTIME st; VariantTimeToSystemTime(vResult.date, &st); } else if(vresult. vt == VT_EMPTY) // empty STR = ""; // Read the alignment of the first cell, data type :VT_I4, read the horizontal alignment range.attachDispatch (wssmysheet.getCells ()); iCell.AttachDispatch((range.GetItem (COleVariant(long(1)), COleVariant(long(1)))).pdispVal); vResult.lVal = 0; vResult = iCell.GetHorizontalAlignment(); if(vResult.lVal ! = 0) {switch (vresult. lVal) {case 1: break; Case-4108: // center break; Case-4131: // break to the left; Case-4152: // break; Icell. AttachDispatch((range.getitem (COleVariant(long(1))), COleVariant(long(1)))). PdispVal); vResult.lVal = 0; vResult = iCell.GetVerticalAlignment(); if(vResult.lVal! =0) {switch (vresult. lVal) {case-4160: // break; Case-4108: // center break; Case-4107: // break; }} // Set the first cell value to "HI,EXCEL!" range.SetItem(COleVariant(long(1)),COleVariant(long(1)),COleVariant("HI,EXCEL!" )); // set the first cell font color: red // CFont font; // range.AttachDispatch(wssMysheet.GetCells()); // range.AttachDispatch((range.GetItem (COleVariant(long(1)), COleVariant(long(1)))).pdispVal); // font(COleVariant((long)0xFF0000)); // Merge cell processing, including determining whether the first cell is merge cell, and merge the first cell Range unionRange; range.AttachDispatch(wssMysheet.GetCells()); unionRange.AttachDispatch(range.GetItem (COleVariant((long)1),COleVariant((long)1)).pdispVal ); vResult = unionRange.GetMergeCells(); If (vresult.boolVal == -1) // Yes merged cells {// Number of merged cells range.attachDispatch (unionrange.getrows ()); long iUnionRowNum=range.GetCount(); AttachDispatch(unionrange.getColumns ()); AttachDispatch(unionrange.getColumns ()); long iUnionColumnNum=range.GetCount(); Long iUnionStartRow= unionrange.getrow (); Long iUnionStartCol= unionrange.getColumn (); Else if(vresult.boolVal == 0) {// Not merge the first cell into 2 rows, 3 columns range.attachDispatch (wssmysheet.getCells ()); unionRange.AttachDispatch(range.GetItem(COleVariant((long)1),COleVariant((long)1)).pdispVal ); unionRange.AttachDispatch(unionRange.GetResize(COleVariant((long)2),COleVariant((long)3))); unionRange.Merge(COleVariant((long)0)); // Merge cells} // Save the file as *.xls m_excelApp.setVisible (TRUE); CString strtmp = m_strPath.Left(m_strPath.ReverseFind('.')) + ".xls"; // We should make sure that STRTMP is a valid path, otherwise the function will handle the exception itself, resulting in no future statement execution. Not closing the open document wbMyBook. SaveAs (COleVariant (STRTMP), covOptional, covOptional, covOptional, covOptional, covOptional, 0, covOptional,covOptional,covOptional,covOptional); Close(covOptional, COleVariant(""), covOptional); wbsMyBooks.Close(); //m_ExcelApp.Quit();Copy the code