想向excel中数据的最后一行的几个字段插入数据,该怎么弄,我的程序通不过(24分)

  • 主题发起人 主题发起人 godyang
  • 开始时间 开始时间
G

godyang

Unregistered / Unconfirmed
GUEST, unregistred user!
filename:=opendialog1.FileName;
ExcelApplication1.Workbooks.Open(FileName,null,null,null,null,null,null,null,null,null,null,null,null,0);
excelapplication1.Visible[0]:=true;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelApplication1.Worksheets[1] as _worksheet);
ir := Sheets("sheet1").[a65535].End(xlUp).Row + 1 ;
Range(Cells(iR, 1), Cells(iR, 1)).Value := 'datetime' ;
 
给点有建设性的代码也好啊,
网上都是新建。。。
我要打开。。。
 
向Excel查询

select * from OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;HDR=yes;database=c:/book1.xls;','select * from [Sheet1$]') where c like '%f%'

select * from
OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;IMEX=2;DATABASE=c:/book1.xls',[sheet1$])

1)hdr=yes时可以把xls的第1行作为字段看待,如第1个中hdr=no的话,where时就会报错
2)[]和美圆$必须要,否则M$可不认这个账

2、修改Execl

update OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;hdr=yes;database=c:/book1.xls;','select * from [Sheet1$]')
set a='erquan' where c like '%f%'

3、导入导出


insert into OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;hdr=yes;database=c:/book1.xls;','select * from [Sheet2$]')(id,name)
select id,name from serv_user

或BCP

master..xp_cmdshell'bcp "serv-htjs.dbo.serv_user" out "c:/book2.xls" -c -q -S"." -U"sa" -P"sa"'

从Excel向SQLServer导入:

select * into serv_user_bak
from OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;HDR=yes;database=c:/book1.xls;','select * from [Sheet1$]')

如果表serv_user_bak不存在,则创建
 
我的问题现在不是对数据集的操作,

ir:=ExcelWorksheet1.[a65535].End(xlUp).Row + 1 ;
Range(Cells(iR, 1), Cells(iR, 1)).Value := 'datetime' ;
通不过
 
有个笨方法,你可以定位在最后一行数据用update语句更新啊!
 
定位在最后一行数据我用的语句不知道怎么了,使用不了
 
我猜来的代码:
procedure TForm1.Button1Click(Sender: TObject);
var dirname,filename,mystr1:string;
ir:integer;
//range:variant;
//olecon: TOleContainer;
begin
// chdir('//138.1.6.2/dyeing/lab/²âÊÔ·¿²âÊÔ±¨¸æ/2006 ²âÊÔ½á¹û±¨¸æ/²âÊÔ×ܽá/²»Í¬²¼ÖÖ²âÊÔͳ¼Æ');
chdir('D:/');
if ioresult <> 0 then
showmessage('Ö¸¶¨Ä¿Â¼ÎÞ·¨Çл»')
else
begin
showmessage('Ŀ¼³É¹¦Çл»');
//od1.InitialDir:='//138.1.6.2/dyeing/lab/²âÊÔ·¿²âÊÔ±¨¸æ/2006 ²âÊÔ½á¹û±¨¸æ/²âÊÔ×ܽá/²»Í¬²¼ÖÖ²âÊÔͳ¼Æ';
od1.InitialDir:='D:/';
if od1.Execute then
begin
filename:=od1.FileName;
ExcelApplication1.Workbooks.Open(FileName,null,null,null,null,null,null,null,null,null,null,null,null,0);
excelapplication1.Visible[0]:=true;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelApplication1.Worksheets[0] as _worksheet);
end;
// ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[day] as _Worksheet);
// ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1]) as _Worksheet );
// ExcelWorksheet1.ConnectTo(ExcelWorkbook1.WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet);
// ir:=ExcelWorksheet1.[a65535].End(xlUp).Row + 1 ;
ir := ExcelApplication1.Sheets('sheet1').[a65535].End(xlUp).Row + 1;
// Range(Cells(iR, 1), Cells(iR, 1)).Value := 'datetime' ;
ExcelWorksheet1.Cells(15, 1).Value := 'datetime' ;
end;
end;
 
我就是想把下面的代码在delphi中实现出来。
Sub labtest()
Dim iR, datetime, vatcode
datetime = Range("d2:e2").Value
vatcode = Range("J2:N2").Value
Windows("manage.xls").Activate
iR = Sheets("sheet1").[a65535].End(xlUp).Row + 1
Range(Cells(iR, 1), Cells(iR, 1)).Value = datetime
Range(Cells(iR, 2), Cells(iR, 2)).Value = vatcode
Windows("work.xls").Activate
End Sub
 
我自己搞定了。多谢
 
后退
顶部