OLE方法打开Excel的问题
我想用OLE的方法打开一个Excel文件,并读到StringGrid里面,如果Excel数据量小的话没什么问题,数据量一大就会报错,大家先看下我的代码对不对……
void __fastcall TReadExcelForm::ExcelOpenClick(TObject *Sender){ Variant ex,wk,sht; //init try{ ex=CreateOleObject("Excel.Application"); }catch(...){ ShowMessage("无法启动Excel,可能尚未安装或文件已经损坏!"); return; } //open Excel File if(OpenExcelDialog->Execute()){ ex.OlePropertyGet("WorkBooks").OleFunction("Open",WideString(OpenExcelDialog->FileName)); wk=ex.OlePropertyGet("ActiveWorkBook"); sht=ex.OlePropertyGet("ActiveSheet"); iCol = sht.OlePropertyGet("UsedRange").OlePropertyGet("Columns").OlePropertyGet("Count"); iRow = sht.OlePropertyGet("UsedRange").OlePropertyGet("Rows").OlePropertyGet("Count"); StringGrid->ColCount = iCol+2; StringGrid->RowCount = iRow+2; for(int i=1;i<iRow;i++){ StringGrid->Cells[0][i-1] = i-1; for(int j=1;j<iCol;j++){ StringGrid->Cells[j][i-1] = sht.OlePropertyGet("Cells",i,j).OlePropertyGet("Value"); } } //Quit wk.OleProcedure("Save"); wk.OleProcedure("Close"); ex.OleFunction("Quit"); ex = Unassigned; wk = Unassigned; sht = Unassigned; }}