再次请教:EXCEL导入发生的错误 ( 积分: 100 )

  • 主题发起人 主题发起人 tgbdlwm
  • 开始时间 开始时间
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;
 
在做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;
 
最高版本支持到EXCEL2000,用SQL2005吧
 
那你也要指出是哪里错才行啊,用断点跟踪一下嘛,代码我写的,我认为在代码上是没有问题的
 
要查一下ExcelWorkbook1.ConnectTo的参数,office的版本会影响参数个数及含义。另外对比delphi中的office单元版本和运行程序的office版本,最好一致。
 
sorry,这样的问题你的指出问题出在哪啊
 
不要使用控件,自己创建 createoleobject('excel.application');没有问题
 
TO CIS:
用你的方法帮我改一下上面的代码可以么
 
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
ExtCtrls, ExcelXP, OleServer, Variants, IdGlobal,
ShellApi, DB, adodb, ComObj, ActiveX, ComCtrls;

var

MsExcel, XlBook, XlSheet: Variant;

begin

MsExcel := createoleobject('excel.application');
XlBook := MsExcel.Workbooks.Open(sFileName);
XlSheet := XlBook.sheets[1];

只是创建不同,其它用法一样
 
解决了,是有些差别
 
谢谢民,结帐
 
后退
顶部