乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>Delphi</em>中如何将<em>excel</em>中的数

<em>Delphi</em>中如何将<em>excel</em>中的数

作者:乔山办公网日期:

返回目录:excel表格制作


a和b的字段类型是不是有Double类型?
请检查Excel中是不是出现了除数字外的其他字符
所以导致你插入数据库时会出现错误提示(string类型不能转换成double类型)

可以将excel中的一个页签视为一个表,excel文件视为一个数据库,使用ADO连接这个数据库,连接串中加入Extended Properties=Excel 8.0,假如excel中的页签名是“测试”,在delphi中用sql操作时,类似语句如下:select * from [测试$],虽然可以支持中文,不过不建议使用中文的页签名称
元接口部分引用 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(看情况而7a686964616fe78988e69d83361定)行如果为空就退出,这样的设定,最好是你的档案力这两行//对应数据库中不能为空的数据

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;

上面的是导到sql 中的。oracle应该也差不多的吧。。

用这个方法吧,好理解且不容易出错。
var
nu,barcode1,da,na,ena,com,spec:string;
ExcelApp,WorkBook: Olevariant;
ExcelRowCount,i :Integer;
begin
dbgrid2.Enabled:=false;
try
if SaveDialog1.Execute then
begin

ExcelApp := CreateOleObject('Excel.Application');
WorkBook := ExcelApp.WorkBooks.Open(savedialog1.FileName);
ExcelApp.Visible := false;
ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;

for i := 2 to excelrowcount + 1 do
begin
nu := excelapp.Cells[i,1].Value;
if trim(excelapp.cells[i,2].value)='' then
na:=' '
else begin
na:=excelapp.Cells[i,2].Value;
end;
if trim(excelapp.cells[i,3].value)='' then
ena:=' '
else
ena:= excelapp.Cells[i,3].Value;
if trim(excelapp.cells[i,4].value)='' then
com:=' '
else
com:= trim(excelapp.Cells[i,4].Value);
if trim(excelapp.cells[i,5].value)='' then
spec:=' '
else
spec:= excelapp.Cells[i,5].Value;

if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,3].Value = '')) then //指定excel档的第 i 行 ,第 1,2(看情况而定)行如果为空就退出,这样的设定,最好是你的档案力这两行//对应数据库中不能为空的数据
exit
else
with data.product do
begin
close;
sql.clear;
sql.Add('select * from twoven where number='''+nu+'''');
open;
if recordcount>=1 then begin
if MessageBox(handle,'此记录已存在e79fa5e98193e59b9ee7ad94336,是否覆盖原记录?','提示',MB_IconQuestion+MB_YesNo)=IDYES then
BEGIN
Delete;
sql.Clear;
sql.add('insert into twoven(number,name,ename,wf_comp,wf_spec)');
sql.add(' values(:number,:name,:ename,:wf_comp,:wf_spec)');
Parameters.ParamByName('number').Value :=trim(nu);//excel档的第一列插入到aa表的 a 栏位;
Parameters.ParamByName('name').Value := trim(na);//excel档的第二列插入到aa表的 b 栏位;
Parameters.ParamByName('ename').Value := trim(ena);//excel档的第一列插入到aa表的 a 栏位;
Parameters.ParamByName('wf_comp').Value := trim(com);//excel档的第一列插入到aa表的 a 栏位;
Parameters.ParamByName('wf_spec').Value := trim(spec);//excel档的第二列插入到aa表的 b 栏位;
execsql;
END;
end;=

end;
showmessage('数据导入成功!');
WorkBook.Close;
ExcelApp.Quit;
ExcelApp := Unassigned;
WorkBook := Unassigned;

ExcelWorkBook1.Close(false);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Screen.Cursor:=crDefault;
end;

相关阅读

  • <em>Delphi</em>中如何将<em>excel</em>中的数

  • 乔山办公网excel表格制作
  • a和b的字段类型是不是有Double类型?请检查Excel中是不是出现了除数字外的其他字符所以导致你插入数据库时会出现错误提示(string类型不能转换成double类型)delphi中怎么实现EXCEL导入导出到
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网