procedure TfrmImportCsv.ImportExcel_btnClick(Sender: TObject);
var
csv_file_name: string;
csv_list: TStrings;
I: Integer;
top_seller_nick: string;
begin
//==============================================================================
//1.获取选择的店铺
top_seller_nick := SellerNickList_cbb.Text;
if top_seller_nick = '' then
begin
ShowMessage('请选择从哪个店铺导出的订单');
Exit;
end;
//==============================================================================
//==============================================================================
//1.判断窗口有没有打开成功
if not OpenDialog1.Execute then
begin
ShowMessage('打开窗口失败,请联系开发者');
Exit;
end;
CsvPath_edit.Text := OpenDialog1.FileName;
//==============================================================================
//==============================================================================
//2.判断连接数据库的连接是否打开
if not frmDataDm.Main_adocon.Connected then
begin
frmDataDm.Main_adocon.Connected := True;
end;
//==============================================================================
//==============================================================================
//3.开始导入数据到StringGrid中
try
sFileName := OpenDialog1.FileName;
frmDataDm.Excel_adocon.Connected := False; //注意这里 连接 EXCEL 的参数 //关键是这里
frmDataDm.Excel_adocon.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source=' + sFileName + ';Extended Properties="Excel 8.0;IMEX=1";Persist Security Info=False';
frmDataDm.Excel_adocon.Connected := True;
//
sl := TStringList.Create;
frmDataDm.Excel_adocon.GetTableNames(sl); //记住 Excel 文件的 底部 必须是 sheet1 否则不能导入
sTableName := sl[0];
if Pos('$', sTableName) > 0 then
begin
sTableName := '[' + sTableName + ']';
end;
frmDataDm.Excel_adoq.Close;
frmDataDm.Excel_adoq.SQL.Clear;
frmDataDm.Excel_adoq.SQL.Text := 'SELECT * FROM ' + sTableName;
frmDataDm.Excel_adoq.Open;
//释放资源
sl.Free;
if frmDataDm.Excel_adoq.RecordCount > 0 then
begin
//把连接access的数据库的连接打开
frmDataDm.ExcelAccess_adoq.Close;
frmDataDm.ExcelAccess_adoq.Sql.Text := 'SELECT top 1 * FROM top_trade';//打开 空记录表....这里可以用 insert,不用append
frmDataDm.ExcelAccess_adoq.Open;
//循环插入数据
frmDataDm.Excel_adoq.First;
for i := 1 to frmDataDm.Excel_adoq.RecordCount do
begin
StringGrid1.Cells[0,I] := top_seller_nick;//frmDataDm.Excel_adoq.FieldByName('编号').AsString;
StringGrid1.Cells[1,I] := frmDataDm.Excel_adoq.FieldByName('订单编号').AsString;
StringGrid1.Cells[2,I] := frmDataDm.Excel_adoq.FieldByName('订单状态').AsString;
StringGrid1.Cells[3,I] := frmDataDm.Excel_adoq.FieldByName('买家会员名').AsString;
StringGrid1.Cells[4,I] := frmDataDm.Excel_adoq.FieldByName('收货人姓名').AsString;
StringGrid1.Cells[5,I] := frmDataDm.Excel_adoq.FieldByName('联系手机').AsString;
StringGrid1.Cells[6,I] := frmDataDm.Excel_adoq.FieldByName('订单创建时间').AsString;
StringGrid1.RowCount := StringGrid1.RowCount +1;
//下一行
frmDataDm.Excel_adoq.Next;
end;
end;
except
MessageBox(Handle,'导入失败!','信息!',MB_OK+MB_ICONINFORMATION);
Exit;
end;
//==============================================================================
end;
本文来自博客园,作者:del88,转载请注明原文链接:https://www.cnblogs.com/del88/archive/2011/12/31/2308911.html