看到好多存储数据到EXCEL中的文件,却没有从EXCEL中读取数据的办法。
s:string;
v,w:variant; v:=createoleobject('Excel.Application');
v.visible := true;
w:=v.workbooks.open('G:\test.xls'); s:=v.cells(1,1).value;//这一步出错
请问,这时候如何读取第一个格的数据到一个字符串变量中?非常感谢
s:string;
v,w:variant; v:=createoleobject('Excel.Application');
v.visible := true;
w:=v.workbooks.open('G:\test.xls'); s:=v.cells(1,1).value;//这一步出错
请问,这时候如何读取第一个格的数据到一个字符串变量中?非常感谢
解决方案 »
- sybase转到DB2,有无人总结这方面注意问题和事项,菜鸟还没接触过DB2
- 请教:打开工程是提示“Class TSWbemLocator not found”怎么解决
- 问一条SQL语句的写法.关于复制表数据的.
- [讨论]帮忙分析一下,自己写的软件在某些win2k/xp机子上拨号上后马上就断开的原因![高手请进]
- 调用DLL生成的MDI子窗体TAB键,ENTER键失效问题(可加分)
- 关于ado的问题
- 3给分lykyl thank you!理由http://www.csdn.net/expert/topic/1024/1024416.xml
- delphi 是怎样将文本文件导入数据库,特别在对sql的日期时间型字段的导入?
- 请教各位大师一个问题,谢谢了!!!
- 如果把Ico,bmp,jpg的格式互相转换,和改变图像大小后保存
- 一个小问题!!!!
- 关于数据库选择和备份的问题?
但它并不能完全读取Excel的文件。如一些格式信息等。这是一大不足。
用自动化对象也是不错的,不过要编定大量的代码才能够实现,相当于普通桌面数据库的功能。还需要懂得VBA这门语言才行。
var ExcelApp,MyWorkBook: OLEVariant;
i,j: Integer;
begin
try
ExcelApp:=CreateOleObject('Excel.Application');
MyWorkBook:=CreateOleobject('Excel.Sheet');
except
application.Messagebox('无法打开Xls文件,请确认已 经安装EXCEL.','',
mb_OK+mb_IconStop);
Exit;
end;
//ExcelApp.Visible := true;
MyworkBook:= ExcelApp.workBooks.Open(ExtractFileDir(Application.ExeName)
+ '\' + Edit1.Text + '.xls');
for i := 3 to 9 do begin
//Read a row into table
table1.Append;
table1.FieldByName('Name').AsString := MyWorkBook.WorkSheets[1].Cells[i,1].Value;
table1.FieldByName('Size').AsString := MyWorkBook.WorkSheets[1].Cells[i,2].Value;
table1.FieldByName('Weight').AsString := MyWorkBook.WorkSheets[1].Cells[i,3].Value;
table1.FieldByName('Area').AsString := MyWorkBook.WorkSheets[1].Cells[i,4].Value;
end;
//showmessage(MyWorkBook.WorkSheets[1].Columns[1].numberformat);
//showmessage(MyWorkBook.WorkSheets[1].Columns[2].numberformat); ExcelApp:=Unassigned; //释放VARIANT变量
end;
**************************打开Excel:
procedure TForm1.BitBtn1Click(Sender: TObject);
var
i:integer;
aa:string;
begin
ExcelApplication1.FindFile;
Try
ExcelApplication1.Connect;
Except
MessageDlg('Excel 没有安装,请安装后运行。', mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0]:=false;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[ExcelApplication1.Workbooks.count]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
ListBox2.Items.Clear;
SpeedButton1.Enabled:=true; for i:=1 to ExcelWorksheet1.Cells.Columns.Count do
begin
aa:=ExcelWorksheet1.Cells.item[1,i];
if aa<>'' then
ListBox2.Items.Add(ExcelWorksheet1.Cells.item[1,i]);
end;执行导入:
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
i,j:integer;
aa,bb:string;
Myarray:array[1..100,1..2]of integer;begin
if ListBox1.ItemIndex>0 then
ListBox1.ItemIndex:=ListBox1.ItemIndex-1;
label3.Caption:=inttostr(ListBox1.ItemIndex);
//建立数据导入的联结关系,得到字段对应数组。
//array〔i,1〕 记录目的字段;array〔i,2〕记录EXCEl中的列数。
for i:=0 to listbox1.Items.Count-1 do begin
myarray[i+1,1]:=i+1;
for j:=1 to ExcelWorksheet1.Cells.Columns.Count do begin
aa:=ExcelWorksheet1.Cells.item[1,j];
if trim(aa)='' then break;
if listbox2.Items[i]=aa then
myarray[i+1,2]:=j;
end;
end; //数据导入
i:=1;
query1.Active:=true;
query1.Edit;
while trim(ExcelWorksheet1.Cells.item[i+1,1])<>'' do begin
aa:=ExcelWorksheet1.Cells.item[i+1,1];
if query1.Locate('ID',aa,[loCaseInsensitive])then begin
query1.edit;
for j:=1 to listbox1.Items.Count do begin
if myarray[j,2]<>0 then
Query1.Fields[j-1].Value:=ExcelWorksheet1.Cells.item[i+1,myarray[j,2]];
end;
// showmessage('有重复记录。')
end else begin
query1.edit;
query1.insert;
for j:=1 to listbox1.Items.Count do begin
if myarray[j,2]<>0 then
Query1.Fields[j-1].Value:=ExcelWorksheet1.Cells.item[i+1,myarray[j,2]];
end;
end;
Label3.caption:=inttostr(i);
i:=i+1; end;
query1.post;
showmessage('共导入'+inttostr(i-1)+'条员工信息。');
end;
1,新建一个ACCESS数据库,点右建,选导入项。
2,选择.XML驱动;选中相应的EXCEL文件,按照提示导入即可了。简单方便。效率还高,不要写代码的。:)
a := ExcelApp.Activesheet.Cells.Item[i,j];