Delphi StringGrid1读取加载excel文件


本文整理自网络,侵删。

 
   const
    xlCellTypeLastCell = $0000000B;
  var
    ExlApp, Sheet: OLEVariant;
    i, j, r, c: integer;
    namestr: string;
    checkdata: string;
    tempstr: string;
  begin
      ExlApp := CreateOleObject('Excel.Application');
      ExlApp.Visible := false;
      ExlApp.Workbooks.Open(path);
      Sheet := ExlApp.Workbooks[ExtractFileName(path)].WorkSheets[1];
      Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
      r := ExlApp.ActiveCell.Row;
      c := ExlApp.ActiveCell.Column;

      //Grid.RowCount:=r;
     // c :=Grid.ColumnCount;
     // Form2.Caption:='加载进度 0%';

      Application.ProcessMessages;
      for j:= 1 to r do
      begin
          try
              if (j mod 1000 = 0)
              then begin
                  Caption:='加载进度'+FormatFloat('0.00%',(j/r*100));
                //  ProgressBar1.Position :=(j/r*100);

                  Application.ProcessMessages;
              end;

              for i:= 1 to c do
              begin
                  StringGrid1.Cells[i-1,j-1]:=sheet.cells[j,i];

              end;
          except on e:exception do
              //ShowMessage(inttostr(j)+' '+e.Message);
          end;
      end;
      Caption:='加载进度 100%';
      //ProgressBar1.Position:=0;

      ExlApp.Quit;
      ExlApp := Unassigned;
      Sheet := Unassigned;



补充:
StringGrid 实例1:初始化StirngGrid的首行和首列
实例1:初始化StirngGrid的首行和首列

// 主要代码部分:

procedure TForm23.SetSGridTitle(Sender: TObject);

var

 ColIndex, RowIndex: integer;

begin

 //画第一行(标题栏)

 for colIndex := 1 to StringGrid1.ColumnCount do

 begin

    StringGrid1.Cells[colIndex, 0] := '列名' + Chr(ord('A') - 1 + colIndex);

 end;

 //画第一列(数字栏)

 StringGrid1.Columns[0].Width := 30;

 StringGrid1.Cells[0, 0] := '序列';

 for RowIndex := 1 to StringGrid1.RowCount - 1 do

 begin

    StringGrid1.Cells[0, RowIndex] := IntToStr(RowIndex);

 end;

end;
来源:https://www.cnblogs.com/lingzhiwen/p/3321947.html

相关阅读 >>

Delphi 将label的caption内容竖向显示

Delphi firedac 下的 sqlite [2] - 第一个例子

Delphi在memorystream查找字符串

Delphi 获取超gb大文件大小

Delphi 提高进程自身权限

Delphi 如何解析网址?

Delphi topendialog设置多个过滤条件

Delphi streamtohexstr

webbrowser自动填表模板

Delphi sqlite实现加密

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...