Delphi Excel导入 的通用程序
Delphi Excel导入 的通用程序 . 分类: delphi 2012-09-24 18:19 127人阅读 评论(0) 收藏 举报 exceldelphiintegerprocedure TForm1.btnClick(Sender: TObject); begin OpenDialog1.Title := '请选择正确的excel文件'; OpenDialog1.Filter := 'Excel(*.xls)|*.xls'; if OpenDialog1.Execute then edit1.Text := OpenDialog1.FileName; end; procedure TForm1.btninClick(Sender: TObject); const BeginRow = 2; BeginCol = 1; var Excel: OleVariant; iRow,iCol : integer; xlsFilename: string; begin if (trim(edit1.Text) = '') then begin MessageBox(GetActiveWindow(), 请选择正确的excel路径', MB_OK + MB_ICONWARNING); exit; end; xlsFilename := trim(edit1.Text); try Excel := CreateOLEObject('Excel.Application'); except Application.MessageBox('excel没有安装', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL); Exit; end; Excel.Visible := false; Excel.WorkBooks.Open(xlsFilename); try iRow := BeginRow; iCol := BeginCol; while trim(Excel.WorkSheets[1].Cells[iRow,iCol].value) <> '' do begin with ADOQuery1 do begin Append; Fields[0].AsString := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value); Fields[1].AsString := trim(Excel.WorkSheets[1].Cells[iRow,iCol+1].value); Fields[2].Asstring := trim(Excel.WorkSheets[1].Cells[iRow,iCol+2].value); iRow := iRow + 1; end; end; Excel.Quit; ADOQuery1.UpdateStatus ; except Application.MessageBox('导入数据出错', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL); Excel.Quit; end; MessageBox(GetActiveWindow(), '数据导入成功', '提示信息', MB_OK + MB_ICONWARNING); end;
Delphi Excel导入 的通用程序 . 分类: delphi 2012-09-24 18:20 257人阅读 评论(0) 收藏 举报 exceldelphiinteger数据库c步骤: 1 连excel(自己知道其格式,最好是没个字段在数据一一对应) 2 读excel数据,填入到数据库 我这里有个函数,实现把excel表格中数据导入数据库,在一条数据导入前判断数据库中是否有该数据,如果有,就不再导入该数据(避免重复) ,你可以参考下 procedure TForm_qyxxcx.BitBtn2Click(Sender: TObject); VAR I,J:INTEGER; col,row:integer; MsExcel,WBook,WSheet:OLEVARIANT; f_temp,strtemp:string; begin if Main_form.lwt.Message_Confirm('为预防不可预测情况发生(字段太长、类型不一致等)'+#10#13+'强烈建议在导入前备份原来数据,'+#10#13+'确认:终止导入'+#10#13+'取消:继续导入') then abort; cdsDJZY.Open; cdsDJZY.First; while not cdsDJZY.Eof do cdsDJZY.Delete; cdsDJZY.Close; cdsDJZY.Open; try begin MsExcel:= CreateOleObject('Excel.Application'); WBook:=MsExcel.Application; if opendialog1.Execute then //关联到文件 begin if opendialog1.FileName='' then abort; wbook.workbooks.Open(opendialog1.FileName); end; WBook.Visible:= true; WSheet:=WBook.worksheets[1]; end except begin Application.Messagebox('您取消了操作或 Excel 没有安装!','ERROR!', MB_ICONERROR + mb_Ok); Abort; end; end; row:=WSheet.UsedRange.Rows.Count; //行 col:=WSheet.UsedRange.columns.Count; //列 if (row=0) or (col = 0) then begin showmessage('该excel文件没有数据!请确认'); abort; end; proform.Show; proform.ProgressBar1.Max:=row; with qqyb do begin open; for i:=1 to row-1 do //要增加的行数 begin // 0 人员名称 2 企业名称 //判断是否存在该企业和人员,如果存在,不导入该记录 strtemp:='select * from qyb where qy_name = '''+WSheet.cells[i+1,1].Value+''''; Main_Form.lwt.DB_AdoQueryRun(qupdate,strtemp); if qupdate.RecordCount<>0 then // 存在,加入临时表 ,退出本次循环 begin cdsDJZY.Append ; cdsDJZY.FieldByName('企业名称').AsString := WSheet.cells[i+1,1].Value; cdsDJZY.Post; continue; end; Append; //不存在,继续 for j:=0 to col-1 do //列 begin proform.ProgressBar1.Position:=proform.ProgressBar1.Position+1; // if adory.Fields[j].FieldKind Fields[j].Value:= WSheet.cells[i+1,j+1].Value; end; Post; end; end; proform.ProgressBar1.Position:=0; proform.Hide; if cdsDJZY.RecordCount>1 then begin if Main_form.lwt.Message_Confirm('数据已经导入!,部分记录因为已经存在,没有导入,是否打印没有导入记录的请单?') then begin Main_form.lwt.DB_ShowReportByDataSet(cdsDJZY,'因为已经存在该企业导入失败的信息'); main_form.lwt.DB_Excel_Export(cdsdjzy,'c:\hello.xls'); end; end else begin Main_Form.lwt.Message_Show('数据已经导入!'); end; wbook.workbooks.close; end;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人