T
tgbdlwm
Unregistered / Unconfirmed
GUEST, unregistred user!
在做EXCEL导入时,我在导入OFFICE2000 的EXCEL文件时没有报错,但导 OFFICE XP的EXCEL时会偶尔报错,不知是什么原因。请指点,很急的。
我的开发环境:W2000 SERVER ,SQL 2000,D7
===打开指定EXCEL文件
var
i: Integer;
filename:string;
begin
combobox1.Items.Clear;
OpenDialog1.Filter := '*.XLS|*.XLS';
OpenDialog1.DefaultExt := 'XLS';
if OpenDialog1.Execute then
begin
try
ExcelApplication1.Connect;
Edit1.text:= OpenDialog1.FileName;
Filename :=Edit1.Text;
except
Application.Messagebox('Excel 没有安装!', 'Hello', MB_ICONERROR +
mb_Ok);
Abort;
end;
ExcelApplication1.Visible[0] := false;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Open
(Filename, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam,0));
for i := 1 to ExcelWorkbook1.Worksheets.Count do
begin
combobox1.items.Add((ExcelWorkbook1.Worksheets.Item
as _WorkSheet).Name);
end;
ComboBox1.ItemIndex :=0;
end;
end;
==========导入指定的EXCEL文件
var
i,j: Integer;
row, col: Integer;
arow,acol,endrow,srow:Integer;
begin
for i:=1 to StringGrid1.RowCount-1 do
for j:=2 to StringGrid1.ColCount-1 do
StringGrid1.Cells[j,i]:='';
arow:=4; //EXCEL文件开始行
acol:=1; // 开始列
endrow:=30; // 终止行
srow:=1; //stringgrid1写数据开始行
try
ExcelWorkSheet1.ConnectTo(ExcelWorkbook1.Worksheets.Item[ComboBox1.ItemIndex + 1] as _WorkSheet);
ExcelWorkSheet1.Activate;
col := ExcelWorksheet1.UsedRange[0].Cells.Columns.Count;
if col>18 then
col:=18; //超出StringGrid1列总数时
row:=endrow;
if (row-arow)>(stringgrid1.rowcount-1-srow) then
row:=stringgrid1.rowcount-1-srow+arow; //超出行时
for I := arow to row do
begin
for J := acol to col do
StringGrid1.Cells[j-acol+2,i-arow+srow]:= ExcelWorksheet1.Cells.Item[i, j];
end;
ExcelWorkSheet1.Disconnect ;
ExcelWorkbook1.Disconnect ;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit ;
except
ExcelWorkSheet1.Disconnect ;
ExcelWorkbook1.Disconnect ;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit ;
application.messagebox('导入的数据不符合标准','警告',Mb_ok+Mb_IconStop);
end;
end;
我的开发环境:W2000 SERVER ,SQL 2000,D7
===打开指定EXCEL文件
var
i: Integer;
filename:string;
begin
combobox1.Items.Clear;
OpenDialog1.Filter := '*.XLS|*.XLS';
OpenDialog1.DefaultExt := 'XLS';
if OpenDialog1.Execute then
begin
try
ExcelApplication1.Connect;
Edit1.text:= OpenDialog1.FileName;
Filename :=Edit1.Text;
except
Application.Messagebox('Excel 没有安装!', 'Hello', MB_ICONERROR +
mb_Ok);
Abort;
end;
ExcelApplication1.Visible[0] := false;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Open
(Filename, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam,0));
for i := 1 to ExcelWorkbook1.Worksheets.Count do
begin
combobox1.items.Add((ExcelWorkbook1.Worksheets.Item
as _WorkSheet).Name);
end;
ComboBox1.ItemIndex :=0;
end;
end;
==========导入指定的EXCEL文件
var
i,j: Integer;
row, col: Integer;
arow,acol,endrow,srow:Integer;
begin
for i:=1 to StringGrid1.RowCount-1 do
for j:=2 to StringGrid1.ColCount-1 do
StringGrid1.Cells[j,i]:='';
arow:=4; //EXCEL文件开始行
acol:=1; // 开始列
endrow:=30; // 终止行
srow:=1; //stringgrid1写数据开始行
try
ExcelWorkSheet1.ConnectTo(ExcelWorkbook1.Worksheets.Item[ComboBox1.ItemIndex + 1] as _WorkSheet);
ExcelWorkSheet1.Activate;
col := ExcelWorksheet1.UsedRange[0].Cells.Columns.Count;
if col>18 then
col:=18; //超出StringGrid1列总数时
row:=endrow;
if (row-arow)>(stringgrid1.rowcount-1-srow) then
row:=stringgrid1.rowcount-1-srow+arow; //超出行时
for I := arow to row do
begin
for J := acol to col do
StringGrid1.Cells[j-acol+2,i-arow+srow]:= ExcelWorksheet1.Cells.Item[i, j];
end;
ExcelWorkSheet1.Disconnect ;
ExcelWorkbook1.Disconnect ;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit ;
except
ExcelWorkSheet1.Disconnect ;
ExcelWorkbook1.Disconnect ;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit ;
application.messagebox('导入的数据不符合标准','警告',Mb_ok+Mb_IconStop);
end;
end;