https://blog.csdn.net/baidu_37503452/article/details/78316671
本地项目 VCExcelAccess
DWORD dwStart = GetTickCount(); CApplication app; CRange range; CWorkbook book; CWorkbooks books; CWorksheet sheet; CWorksheets sheets; LPDISPATCH lpdisp; COleVariant vresult; COleVariant covtrue((short)TRUE); COleVariant covfalse((short)FALSE); COleVariant covoptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //create the server if (!app.CreateDispatch("Excel.Application")) { AfxMessageBox("Cannot start Excel server"); return; } app.put_DisplayAlerts(false); app.put_Visible(FALSE); books.AttachDispatch(app.get_Workbooks()); CString ExcelFile = "G:\colibri\ISL3.5\MSA\7-4\MSA\1.xlsx"; CString saveFile = "G:\colibri\ISL3.5\MSA\7-4\MSA\2.xlsx"; //open file lpdisp = books.Open( ExcelFile, covoptional, covfalse, covoptional, covoptional, covoptional, covoptional, covoptional, covoptional, covoptional, covoptional, covoptional, covoptional, covoptional, covoptional); //get workbook book.AttachDispatch(lpdisp); //get worksheets sheets.AttachDispatch(book.get_Worksheets()); /*lpdisp = book.get_ActiveSheet(); sheet.AttachDispatch(lpdisp);*/ CString str; sheet = sheets.get_Item(COleVariant((short)2)); //访问第二个sheet range = sheet.get_Range(COleVariant("A1"), COleVariant("Z1000")); VARIANT valArray = range.get_Value2(); for (int row = 1; row <= 1000; row++) { for (int col = 1; col <= 26; col++) { str = GetCellVal(valArray, row, col); SetCellVal(valArray, row, col,"test"); } } range.put_Value2(valArray); VariantClear(&valArray); book.SaveAs(COleVariant(saveFile), covoptional, covoptional, covoptional, covoptional, covoptional, (long)0, covoptional, covoptional, covoptional, covoptional, covoptional); book.Close(covoptional, COleVariant(ExcelFile), covoptional); books.Close(); app.Quit(); CString CVCExcelAccessDlg::GetCellVal(VARIANT data , int row, int col) { VARIANT val; long index[2]; index[0] = row; index[1] = col; SafeArrayGetElement(data.parray, index, &val); //获取值 return Var2CString(val); } int CVCExcelAccessDlg::SetCellVal(VARIANT data, int row, int col,CString strData) { VARIANT val; long index[2]; index[0] = row; index[1] = col; variant_t var_Datacfg = (LPSTR)(LPCTSTR)strData; HRESULT hRet = ::SafeArrayPutElement(data.parray, index, &var_Datacfg); return hRet; }