//Evan: 从excel文件导入数据
...
type
...
edFileName: TEdit;
con1: TADOConnection;
ads1: TADODataSet;
gdData: TAdvStringGrid;
lst1: TListBox;
gd1: TDBGrid;
ds1: TDataSource;
...
function DoFinish: Boolean;
var
i, j, recordnum: Integer;
mCard1, mCard2: TStringList;
mPointNumber: string;
begin
recordnum := 0;
if ads1.Active then
begin
mCard1 := TStringList.Create;
mCard2 := TStringList.Create;
for i := 1 to gdData.RowCount - 1 do
begin
mCard1.Clear;
mCard2.Clear;
Split(gdData.Cells[1, i], ',', mCard1);
Split(gdData.Cells[2, i], ',', mCard2);
for j := 0 to mCard1.Count - 1 do
begin
if j < mCard2.Count then mPointNumber := mCard2.Strings[j]
else mPointNumber := '';
加入Sql命令写表
...
Inc(recordnum);
end;
end;
mCard1.Free;
mCard2.Free;
end;
end;
procedure LoadTables;
begin
if con1.Connected then con1.Close;
con1.ConnectionString :=
Format(
'Provider=Microsoft.Jet.OLEDB.4.0;' + #13#10 +
'Data Source=%s;' + #13#10 +
'Extended Properties=Excel 8.0;' + #13#10 +
'Persist Security Info=False', [edFileName.Text]);
con1.Connected := True;
con1.GetTableNames(lst1.Items);
end;
procedure lst1Click(Sender: TObject);
var
i: integer;
mTableName: string;
mCol: TColumn;
begin
inherited;
for i := 0 to lst1.Count - 1 do
begin
if lst1.Selected then
begin
mTableName := lst1.Items.Strings;
Break;
end;
end;
//打开数据集
if mTableName <> '' then
begin
if ads1.Active then
ads1.Close;
gd1.DataSource := nil;
ads1.CommandText := ' select * from [' + mTableName + '] ';
try
ads1.Open;
ads1.Last;
ads1.First;
except
ErrMsg('打开出错,可能是不支持此表。');
Exit;
end;
gd1.Columns.Clear;
for i := 0 to ads1.Fields.Count - 1 do
begin
mCol := gd1.Columns.Add;
mCol.Title.Caption := ads1.Fields.Fields.FieldName;
mCol.Width := 100;
mCol.FieldName := ads1.Fields.Fields.FieldName;
end;
gd1.DataSource := ds1;
end;
end;
你改改就可以了。如果要加快速度,建议采用以下方法试试:
1、可以把数据读到内存中再插表而不要加载到Grid中;
2、把Excel文件另存为如下文本文件格式后再导入,这样速度会快很多:
张三,男,23,工程师,本科,2000
张三2,男,25,工程师,本科,2500
...