我连接上excel后,通过显示,把excel单元格内容显示在tstringgrid上,但是有个单元格显示内容为"#DIV/0!";一读到这里就出现could not convert variant of type(error)into type (string),我该如何解决?

解决方案 »

  1.   

    could not convert variant of type(error)into type (string),类型转换错误,没有看看程序中断在哪里,没源码无真像
      

  2.   

    如果有"#DIV/0!"的单元格,我能如何避免提示而继续读取下面单元格的数据呢?
      

  3.   

    我想通过判断让出现'#div/0!' 的单元格显示为空,
    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)。
      

  4.   

    /*********************************
    /   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;
      

  5.   

    /*********************************
    /   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;