1. 程式人生 > >XLSReadWriteII5匯入excel資料

XLSReadWriteII5匯入excel資料

procedure TForm1.Button1Click(Sender: TObject);
var
  xls: TXLSReadWriteII5;
  openFile: TOpenDialog;
  Rows, Cols: Integer;                 //rows行數,cols列數
begin
  xls := TXLSReadWriteII5.Create(Self);     //建立例項
  openFile := TOpenDialog.Create(Self);

  openFile.Filter := 'Excel|*.xlsx';
  openFile.DefaultExt :
= 'xlsx'; try if openFile.Execute() then begin xls.Filename := openFile.FileName; //讀取檔名 xls.Clear; xls.Read; StringGrid1.RowCount := xls.Sheets[0].LastRow + 1; //設定stringgrid總行數 StringGrid1.ColCount := xls.Sheets[0].LastCol + 1; //
設定stringgrid總列數 for Rows := 0 to xls.Sheets[0].LastRow do begin for Cols := 0 to xls.Sheets[0].LastCol do begin StringGrid1.Cells[Cols, Rows] := xls.Sheets[0].AsString[Cols, Rows]; end; end; end; finally xls.Free; openFile.Free; end; end
;