1,178
社区成员
发帖
与我相关
我的任务
分享
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;
}
}