delphi中EXECL导入数据库如何写?求源码!
发布时间:2011-06-24 19:41:44 文章来源:www.iduyao.cn 采编人员:星星草
delphi中EXECL导入数据库怎么写?求源码!!
我自己写了一个,功能可以实现,求更好的源码,我参考参考。
------解决方案--------------------
单元接口部分引用 comobj 单元(uses )
procedure TForm1.Button1Click(Sender: TObject);
var excelx,excely : string;
begin
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := ExcelApp.WorkBooks.Open(opendialog.FileName);//使用opendialog对话框指定
//excel档路径
ExcelApp.Visible := false;
ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
for i := 1 to excelrowcount + 1 do
begin
excelx := excelapp.Cells[i,1].Value;
excely := excelapp.Cells[i,2].Value;
if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,2].Value = '')) then
//指定excel档的第 i 行 ,第 1,2(看情况而定)行如果为空就退出,这样的设定,最好是你的档案力这两行//对应数据库中不能为空的数据
exit
else
with query1 do
begin
close;
sql.clear;
sql.add(insert into test(name,address) values(:name,:address));
parambyname('name').asstring := excelx;//excel档的第一列插入到test表的 name栏位;
parambyname('address').asstring := excely;//excel档的第二列插入到test表的 address 栏位;
execsql;
end;
end;
finally
WorkBook.Close;
ExcelApp.Quit;
ExcelApp := Unassigned;
WorkBook := Unassigned;
end;
end;
------解决方案--------------------
以前写的不知到能不能帮你~~
procedure Tfrmchanpin_daoru.btn2Click(Sender: TObject);
////////////////////////////////////////
// uses Comobj; //
// Excel 导入到数据库 //
// 高文杰 2009.6.22 //
////////////////////////////////////////
var
ExcelApp:variant;
n1:string;
i,j:integer;
FileName:string;
myfloat:double;
OpenDialog:TOpenDialog;
begin
OpenDialog:=TOpenDialog.Create(nil);
OpenDialog.Filter:='Excel文件|*.xls';
if not OpenDialog.Execute then begin
OpenDialog.Free;
exit;
end;
FileName := OpenDialog.FileName;
ExcelApp:=CreateOleObject('Excel.Application');
ExcelApp.visible:=False;
ExcelApp.workbooks.open(FileName);
//自适应宽度
ExcelApp.worksheets[1].Cells.EntireColumn.AutoFit; //整个表所有列
j:=2; //假定标题在第一行
try
with qry1 do
begin
n1:='insert into xcm_chanpin_info(dl_dm,xl_dm,cp_dm,cp_name,cp_jldw,cp_jj,cp_sj,cp_isZengPin)'
+'values(:dl_dm,:xl_dm,:cp_dm,:cp_name,:cp_jldw,:cp_jj,:cp_sj,:cp_isZengPin)';
close;
sql.Clear;
sql.Add(n1);
while ExcelApp.worksheets[1].cells[j,1].text<>'' do
begin
for i:=1 to 8 do begin //共8列
Parameters.Items[i-1].Value:=ExcelApp.worksheets[1].cells[j,i].text;
if i=1 then begin
//检查大类是否存在
qry2.Close;
qry2.SQL.Text:='select * from xcm_dalei_info where dl_dm='''+ExcelApp.worksheets[1].cells[j,i].text+'''';
qry2.Prepared;
qry2.Open;
if qry2.Eof then begin
mmo1.Lines.Add('导入第'+IntToStr(j)+'行时,产品大类不存在,请检查');
ExcelApp.Activeworkbook.close(false);
ExcelApp.quit;
ExcelApp:=unassigned;
OpenDialog.Free;
exit;
end;
end;
if i=2 then begin
//检查小类是否存在
qry2.Close;
qry2.SQL.Text:='select * from xcm_xiaolei_info where xl_dm='''+ExcelApp.worksheets[1].cells[j,i].text+'''';
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
delphi 绘图
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if (CheckBox1.Checked) then
begin
ADOQuery2.A...
-
键列信息不足或不正确,更新影响到过多的行
我把csv文件内的很多记录,要使用ado写入到excel中,结果报如题错误
问题该怎么解决啊
...
-
dax error : access violation at address
<OBJECT classid="clsid:A318A9AC-E75F-424C-9364-6B40A848FC6B" width=20 height=20 ...
-
c# 操作excel 去掉单元格已有的边框
如题,excel有黑色边框,怎么样才能把边框去掉?
------解决方案--------------------
SQL code
...
-
为什么现在delphi写的程序都会被杀软误报?
360杀毒,金山毒霸都误报有毒,没有写任何代码,生成都误报毒,SysConst.dcu只有12kb,自己没有...
-
关于Delphi7 调用C++ dll 传参问题。
C++ DLL h文件:C/C++ code
extern "C" __declspec(dllexport) unsigned int __stdcall W...
-
求高手封装成一个方法
C# code
private void Form1_Load(object sender, EventArgs e)
{
PrintDocument pr...
-
delphi7 dbexpress sqlserver2005
本人新手,请问delphi7可以用dbexpress中的sqlconnection组件连接sqlserver2005中的数据库吗?自己...
-
动态生成多个button并触发该事件 急!急!急!!!
在asp.net中,在指定位置动态生成了多个button,怎么样获取他所对应的ID
我的做法是:
在页面...
-
c#关于窗体间传值的问题
我在网上下了个代码,它的代码:在窗体A中能使用窗体B中定义的东西(它定义的是个委托)。
我却发现个很奇怪的事...