DELPHI将EXCEL数据导入到数据库中源码
以下是一个使用DELPHI将EXCEL数据导入到数据库的示例源码:
```pascal
unit Unit1;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls,
Data.Win.ADODB,
Vcl.Grids,
Vcl.DBGrids,
Vcl.ExtCtrls,
Excel97,
ActiveX;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure ImportExcelData(const AFileName, ASheetName: string);
public
delphi app{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
OpenFileDialog: TOpenDialog;
begin
OpenFileDialog := TOpenDialog.Create(nil);
try
OpenFileDialog.Filter := 'Excel Files (*.xls, *.xlsx),*.xls;*.xlsx';
if OpenFileDialog.Execute then
begin
ImportExcelData(OpenFileDialog.FileName, 'Sheet1');
ShowMessage('Data imported successfully!');
end;
finally
OpenFileDialog.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DataSource1.DataSet := ADOQuery1;
end;
procedure TForm1.ImportExcelData(const AFileName, ASheetName: string);
var
ExcelApp: TExcelApplication;
Workbook: TExcelWorkbook;
Worksheet: TExcelWorksheet;
Range: Variant;
I, J, StartRow, LastRow, StartColumn, LastColumn: Integer;
FieldNames: TStringList;
FieldValues: TStringList;
begin
ExcelApp := TExcelApplication.Create(nil);
try
ExcelApp.Connect;
Workbook := ExcelApp.Workbooks.Open(AFileName);
Worksheet := Workbook.Sheets.Item[ASheetName] as TExcelWorksheet;
Range := Worksheet.UsedRange;
StartRow := Range.Row;
LastRow := Range.Row + Range.Rows.Count - 1;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论