Delphi如何读取excel数据
原创将数据从数据库导入到excel 很简单,但如果你想放轻松,但如果你想excel 中的数据delphi以下是我个人收集的比较可行的方案,我不敢欣赏,所以我拿出来供大家参考。!
procedure load(rowCount,colCount:integer; fileName:String; var grid:TStringGrid);
//从Excel从要从中读取数据的读取数据中读取数据 Grid
var
v:variant;
i,j:integer;
begin
grid.RowCount:=rowCount;
grid.ColCount:=colCount;
v:=createoleobject(Excel.Application);//创建OLE对象
try
form2.show;
form2.ProgressBar1.Position := 0;
form2.ProgressBar1.Max := 65535;
V.workBooks.Open(fileName);
//for i:=1 to rowCount do
for i:=1 to 65535 do
for j:=1 to colCount do
//if grid.Cells[j-1,i-1] = then break;
form2.ProgressBar1.Position := i;
grid.Cells[j-1,i-1]:=v.workbooks[1].sheets[1].cells[i,j];
v.workbooks[1].close;
finally
v.quit;
form2.close;
end
end;
{
procedure save(tableName:String;grid:TStringGrid);
// 将 Grid 中的数据 SQL Server 数据表中
var
valuesStr:string;
i,j:integer;
begin
if not CreateTable(tableName,grid.ColCount) then
begin
showmessage(Error On CreateTable);
exit;
end;
for i:=1 to grid.RowCount-1 do
begin
valuesStr:=inttostr(i)+,;
for j:=0 to grid.ColCount-1 do
valuesStr:=valuesStr+Grid.Cells[j,i]+,;
if not insertone(tableName,valuesStr) then
begin
showmessage(Error On Row(+inttostr(i)+));
exit;
end;
end;
showmessage(数据导入成功数据导入成功数据导入成功);
end;
function insertone(const tableName, ValuesStr: string): boolean;
// 插入记录插入行插入记录
var
tmpstr,s:string;
p:integer;
begin
result:=true;
tmpstr:=ValuesStr;
with query1 do
begin
close;
sql.Clear;
sql.Add(insert into +tableName+ values();
s:=;
while tmpstr<> do
begin
p:=pos(,,tmpstr);
s:=s++copy(tmpstr,1,p-1)+,;
system.Delete(tmpstr,1,p);
end;
s:=copy(s,1,length(s)-1);
sql.Add(s);
sql.Add());
try
execsql;
except
result:=false;
end;
end;
end;
function CreateTable(const tableName:String; aFieldCount: integer): boolean;
// 创建表
var
tmpstr:string;
i:integer;
begin
result:=true;
tmpstr:=if exists (select * from sysobjects where Name=
+tableName+) drop table +tableName+ create table +tableName+(;
for i:=1 to aFieldCount do
tmpstr:=tmpstr+F+inttostr(i)+ varchar(50),;
delete(tmpstr,length(tmpstr),1);
tmpstr:=tmpstr+);
with query1 do
begin
close;
sql.Clear;
sql.Add(tmpstr);
try
execsql;
except
result:=false;
end;
end;
end;
}
转载于:https://www.cnblogs.com/tecsoon/archive/2009/03/25/1421383.html
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除