我连接上excel后,通过显示,把excel单元格内容显示在tstringgrid上,但是有个单元格显示内容为"#DIV/0!";一读到这里就出现could not convert variant of type(error)into type (string),我该如何解决?
解决方案 »
- 一个奇怪的数据链接问题,搞了三天了还不行,急?大家帮一帮手
- tquery之params应用:参数位置一变就出错?
- 一道关于应用WebSnap开发的项目的发布问题。
- 高手来看看这个问题,如何取得句柄
- 数组
- 关于网络蚂蚁悬浮窗的问题,为什么我的悬浮窗会被最小化?
- 非Delphi用户请教一个非常简单的数据库访问问题?来者有分!
- ??哪位大哥给个塑胶制品厂的能力计划方面给我?谢谢
- DBTreeView中,增加同级目录或者子级目录的时候,自定义的字段如何赋值?
- --------------- 中国315 --------------
- delphi xe2 是如何创建动态链接库的 *.dll
- Delphi7中Icomop控件远程连接opcserver报错问题
if ExcelApp.Cells[3,9].Value='#div/0!' then
begin
stg1.cells[9,i+1] :='';
end
else
begin
stg1.cells[9,i+1] :=ExcelApp.Cells[3,9].Value;
end;
但是在循环里又出现
could not convert variant of type(string)into type (double)。
/ Excel文件导到StringGrid控件中
/
/参数:
/ AGrid:TStringGrid StringGrid控件
/ FileName:String 带路径的Excel文件
/返回:
/ True:成功导入
/ False:导入失败
/
/引入:Use ComObj
/
/注释:
/ 2005-03-28 by Baken.Zhang
*********************************/
function ExcelToStringGrid(AGrid: TStringGrid;const FileName: string): Boolean;
const
xlCellTypeLastCell = $0000000B;
var
XLApp, Sheet: OLEVariant;
RangeMatrix: Variant;
x, y, k, r: Integer;
begin
Result := False;
try
XLApp := CreateOleObject('Excel.Application');
except
Application.MessageBox('错误提示','系统没有安装Excel, 无法创建文件!',MB_OK);
Exit;
end; try
XLApp.Visible := False;
XLApp.Workbooks.Open(FileName);
Sheet := XLApp.Workbooks[ExtractFileName(FileName)].WorkSheets[1]; Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
x := XLApp.ActiveCell.Row;
y := XLApp.ActiveCell.Column; AGrid.RowCount := x + 1;
AGrid.ColCount := y + 1; RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[x, y]].Value;
k := 1;
repeat
for r := 1 to y do
begin
try
AGrid.Cells[ r , k ] := RangeMatrix[k, r];
except
AGrid.Cells[ r , k ] := '#DIV!0';
end;
end;
Inc(k, 1);
AGrid.RowCount := k + 1;
until k > x;
RangeMatrix := Unassigned;
finally
if not VarIsEmpty(XLApp) then
begin
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
Result := True;
end;
end;
end;
/ Excel文件导到StringGrid控件中
/
/参数:
/ AGrid:TStringGrid StringGrid控件
/ FileName:String 带路径的Excel文件
/返回:
/ True:成功导入
/ False:导入失败
/
/引入:Use ComObj
/
/注释:
/ 2005-03-28 by Baken.Zhang
*********************************/
function ExcelToStringGrid(AGrid: TStringGrid;const FileName: string): Boolean;
const
xlCellTypeLastCell = $0000000B;
var
XLApp, Sheet: OLEVariant;
RangeMatrix: Variant;
x, y, k, r: Integer;
begin
Result := False;
try
XLApp := CreateOleObject('Excel.Application');
except
Application.MessageBox('错误提示','系统没有安装Excel, 无法创建文件!',MB_OK);
Exit;
end; try
XLApp.Visible := False;
XLApp.Workbooks.Open(FileName);
Sheet := XLApp.Workbooks[ExtractFileName(FileName)].WorkSheets[1]; Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
x := XLApp.ActiveCell.Row;
y := XLApp.ActiveCell.Column; AGrid.RowCount := x + 1;
AGrid.ColCount := y + 1; RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[x, y]].Value;
k := 1;
repeat
for r := 1 to y do
begin
try
AGrid.Cells[ r , k ] := RangeMatrix[k, r];
except
AGrid.Cells[ r , k ] := '#DIV!0';
end;
end;
Inc(k, 1);
AGrid.RowCount := k + 1;
until k > x;
RangeMatrix := Unassigned;
finally
if not VarIsEmpty(XLApp) then
begin
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
Result := True;
end;
end;
end;