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;

 

posted @ 2018-11-24 13:47  liessay  阅读(1117)  评论(0编辑  收藏  举报