delphi - 从 Excel 导入数据

标签 delphi excel delphi-7 paradox

我想将数据从 Excel 导入到 Delphi 7 上的 Paradox 数据库中。

如何使用 Delphi 做到这一点?

最佳答案

好吧,如果您有标记为 Delphi 7 的问题,我假设您有 BDE 。这将使您能够在 Delphi 中完成整个过程。

您可以修改并使用此未经测试的代码(您将需要添加异常处理):

procedure TForm1.Button2Click(Sender: TObject);
var
  Cols: integer;
  Rows: integer;
  IntCellValue: integer;
  Excel, XLSheet: Variant;
  failure: Integer;

begin
  failure:=0;
  try
    Excel:=CreateOleObject('Excel.Application');
  except
    failure:=1;
  end;
  if failure = 0 then
  begin
    Excel.Visible:=False;
    Excel.WorkBooks.Open(<Excell_Filename>);
    XLSheet := Excel.Worksheets[1];
    Cols := XLSheet.UsedRange.Columns.Count;
    Rows := XLSheet.UsedRange.Rows.Count;

    // Value of the 1st Cell
    IntCellValue:=Excel.Cells[1, 1].Value;
    // Iterate Cals/Rows to read the data section in your worksheet
    // and you can write it in Paradox using the BDE by iterating all cells
    // somthing like this pseudo code:

      try            
        Query := TQuery.Create(nil)            
        Query .Databasename := PdxDBName; // must be configured in the BDE
        while Rows > 0
        begin
          while Cols > 0
          begin
            IntCellValue:=Excel.Cells[Cols,Rows].Value;
            Query .SQL.text := // SQLStmt including the IntCellValue
            ExecSQL;
            dec(Cols);               
          end;
          Cols := XLSheet.UsedRange.Columns.Count;
        Dec(Rows);
        end;
      finally
        Query.Free;
      end;

    Excel.Workbooks.Close;
    Excel.Quit;
    Excel:=Unassigned;
  end;
end;

关于delphi - 从 Excel 导入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5059476/

相关文章:

java - Fillo 依赖项不适用于 Maven 项目

delphi - 如何有效地检查一个字符串是否包含几个子字符串之一?

Delphi - ClientDataSet - 验证数据

WinForms float 窗口(如Delphi7 IDE)

delphi - 使用 TJpegImage 组件将 bmp 转换为 jpg 时出现问题

excel - 在vba中搜索多个字符串

excel - 根据用户窗体文本字段中的变量隐藏行

delphi - 如何更改 tcxgrid 行的颜色

delphi - 如何确保在调用 FreeLibrary 之前释放 Interface 实例

delphi - 数组的 WMI 值