Excel2000, OleServer,ComObj, ExcelXP添加到上面的uses列表里面,添加对应的EXECEL控件。
ExcelApplication1: TExcelApplication
ExcelWorkbook1: TExcelWorkbook
ExcelWorksheet1: TExcelWorksheet
然后在你 的事件里写下面代码。原来写的数据太多,删除了一些列的导入内容,你自己再调试一下就行了。
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,'此记录已存在,是否覆盖原记录?','提示',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
导出:
uses Excel2000, OleServer,ComObj, ExcelXP
var
i,j:Integer
da,na,ena,com,spec:string
begin
i:=1
with data.product do begin
sql.Clear
sql.Add('select * from twoven where printquantity >= 1 order by number')
data.product.Active:=true
end
dbgrid2.Enabled:=false
try
if SaveDialog1.Execute then
begin
ExcelApplication1.Connect
excelapplication1.Workbooks.Add(null,0)
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1])
ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet)
ExcelWorkSheet1.Cells.Item[1,1]:='布号'
ExcelWorkSheet1.Cells.Item[1,2]:='品名'
ExcelWorkSheet1.Cells.Item[1,3]:='英文名称'
ExcelWorkSheet1.Cells.Item[1,4]:='成份'
ExcelWorkSheet1.Cells.Item[1,5]:='规格'
data.product.First
while not data.product.Eof do begin
if dbgrid2.DataSource.DataSet.FieldValues['enterdate']=null then
da:=datetimetostr(date())
else
da:=datetimetostr(dbgrid2.DataSource.DataSet.fieldvalues['enterdate'])
if dbgrid2.DataSource.DataSet.FieldValues['ename']=null then
ena:=''
else
ena:=trim(dbgrid2.DataSource.DataSet.fieldvalues['ename'])
if dbgrid2.DataSource.DataSet.FieldValues['name']=null then
na:=''
else
na:=trim(dbgrid2.DataSource.DataSet.fieldvalues['name'])
if dbgrid2.DataSource.DataSet.FieldValues['wf_comp']=null then
com:=''
else
com:=trim(dbgrid2.DataSource.DataSet.fieldvalues['wf_comp'])
if dbgrid2.DataSource.DataSet.FieldValues['wf_spec']=null then
spec:=''
else
spec:=trim(dbgrid2.DataSource.DataSet.fieldvalues['wf_spec'])
i:=i+1
ExcelWorkSheet1.Cells.Item[i,1]:=dbgrid2.DataSource.DataSet.fieldvalues['number']
ExcelWorkSheet1.Cells.Item[i,2]:=na
ExcelWorkSheet1.Cells.Item[i,3]:=ena
ExcelWorkSheet1.Cells.Item[i,4]:=com
ExcelWorkSheet1.Cells.Item[i,5]:=spec
data.product.next
end
showmessage('数据导出成功!')
ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName)
ExcelWorkBook1.Close(false)
ExcelApplication1.Disconnect
ExcelApplication1.Quit
Screen.Cursor:=crDefault
可以将excel中的一个页签视为一个表,excel文件视为一个数据库,使用ADO连接这个数据库,连接串中加入ExtendedProperties=Excel
8.0,假如excel中的页签名是“测试”,在delphi中用sql *** 作时,类似语句如下:select
*
from
[测试$],虽然可以支持中文,不过不建议使用中文的页签名称。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)