无法将Excel值的类型"double"转换为"string" [英] Cannot convert type 'double' to 'string' for Excel values

查看:167
本文介绍了无法将Excel值的类型"double"转换为"string"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载网络上许多地方所示的Excel文件.

I'm loading an Excel file as indicated in many places over the web.

OpenFileDialog chooseFile = new OpenFileDialog();
            chooseFile.Filter = "Excel files (*.xls,*.xlsl)|*.xls;*.xlsx";
            if (chooseFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                selectedFileName = chooseFile.FileName;                
                textBox1.Text = selectedFileName;
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                Workbook wb = excel.Workbooks.Open(selectedFileName);                
                Sheets excelSheets = wb.Worksheets;
                string currentSheet = "Sheet 1";
                excelWorksheet = (Worksheet)excelSheets.get_Item(currentSheet);
                Range usedRange = excelWorksheet.UsedRange;
                Range lastCell = usedRange.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
                lastRow = lastCell.Row;
                lastColumn = lastCell.Column;

                //release com object you won't need.
                Marshal.ReleaseComObject(excel);
                Marshal.ReleaseComObject(wb);

            }

当我尝试使用此功能从单元格获取单个值时出现问题:

Troubles come when I try to get the single value from a cell with this function:

private string getCellValue(Worksheet Sheet, int Row, int Column)
        {            
            var cellValue = (string)(Sheet.Cells[Row, Column] as Range).Value;            
            return cellValue;
        }

此代码对于许多单元格都可以正常工作,但是突然卡在一个单元格上,从而引发此异常:

This code works fine for a lot of cells, but suddenly get stuck to one cell raising this exception:

无法将类型"double"转换为"string"

Cannot convert type 'double' to 'string'

这很奇怪,因为它可以与所有其他单元格完美配合并将所有内容转换为字符串.似乎对空白单元格也没有任何麻烦.我还已将excel文件中的每个单元格都指定为文本",但实际上对此行为一无所知.

This is weird because it perfectly works with all the other cells and converts everything to string. It seems not even to give any trouble with blank cells. I've also specified every cell from the excel file to be "text", but really has no clue for this behavior.

也尝试通过这种方式进行显式转换.

Also tried an explicit conversion in this way.

private string getCellValue(Worksheet Sheet, int Row, int Column)
        {            
            string cellValue = Sheet.Cells[Row, Column].Value.ToString();
            return cellValue;
        }

现在引发的异常是:

无法对空引用执行运行时绑定

Cannot perform runtime binding on a null reference

推荐答案

Text属性可用于从单元格中检索文本内容.因此,您可以尝试按以下方式使用该属性:

The Text property can be used to retrieve the text contents from a Cell. So, you could try to use the property as follows:

private string getCellValue(Worksheet Sheet, int Row, int Column)
    {            
        string cellValue = Sheet.Cells[Row, Column].Text.ToString();
        return cellValue;
    }

这篇关于无法将Excel值的类型"double"转换为"string"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆