USEGEAR

导航

ChatGTP获取的d读取excel通用程序。

procedure  ReadExcelFile(const FileName: string; AMemtable: TFDmemtable);
var
  ExcelApp: OleVariant;
  Sheet: OleVariant;
  Range: OleVariant;
  RowCount, ColCount, i, j: Integer;
  Data: Variant;
begin
  ExcelApp := CreateOleObject('Excel.Application');
  try
    ExcelApp.Workbooks.Open(FileName);
    Sheet := ExcelApp.ActiveSheet;
    Range := Sheet.UsedRange;
    RowCount := Range.Rows.Count;
    ColCount := Range.Columns.Count;
    AMemtable.FieldDefs.Clear;
    for j := 1 to ColCount do
      AMemtable.FieldDefs.Add(Range.Cells[1, j].Value, ftString, 255);
    AMemtable.CreateDataSet;
    for i := 2 to RowCount do
    begin
      AMemtable.Append;
      for j := 1 to ColCount do
      begin
        Data := Range.Cells[i, j].Value;
        AMemtable.Fields[j - 1].AsString := VarToStr(Data);
      end;
      AMemtable.Post;
    end;
  finally
     ExcelApp.Quit;
     Sheet := Unassigned;
     ExcelApp := Unassigned;
  end;
end  ;

 

posted on 2023-04-17 15:56  USEGEAR  阅读(80)  评论(0编辑  收藏  举报