I'm loading an Excel file as indicated in many places over the web.
我正在加载一个Excel文件,如网上许多地方所示。
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:
此代码适用于许多单元格,但突然陷入一个单元格引发此异常:
Cannot convert type 'double' to 'string'
无法将'double'类型转换为'string'
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.
这很奇怪,因为它完美地适用于所有其他单元格并将所有内容转换为字符串。似乎甚至没有给空白细胞带来任何麻烦。我还指定excel文件中的每个单元格都是“文本”,但实际上并不知道这种行为。
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;
}
And now the raised exception is:
现在引发的例外是:
Cannot perform runtime binding on a null reference
无法对空引用执行运行时绑定
4 个解决方案
#1
11
The Text property can be used to retrieve the text contents from a Cell. So, you could try to use the property as follows:
Text属性可用于从Cell检索文本内容。因此,您可以尝试使用该属性,如下所示:
private string getCellValue(Worksheet Sheet, int Row, int Column)
{
string cellValue = Sheet.Cells[Row, Column].Text.ToString();
return cellValue;
}
#2
3
try this
private string getCellValue(Worksheet Sheet, int Row, int Column)
{
object cellValue = Sheet.Cells(Row, Column).Value;
if (cellValue != null) {
return Convert.ToString(cellValue);
} else {
return string.Empty;
}
}
#3
1
Try to use this code below:
尝试使用以下代码:
private string getCellValue(Worksheet Sheet, int Row, int Column)
{
string cellValue = Sheet.Cells[Row, Column].Text.ToString();
return cellValue;
}
Hope it works!
希望它有效!
#4
0
Try with:
double dbl =(double)cell.Value;
isDouble = String.Format("{0:0}", dbl).ToString();
This works for me.
这对我有用。
#1
11
The Text property can be used to retrieve the text contents from a Cell. So, you could try to use the property as follows:
Text属性可用于从Cell检索文本内容。因此,您可以尝试使用该属性,如下所示:
private string getCellValue(Worksheet Sheet, int Row, int Column)
{
string cellValue = Sheet.Cells[Row, Column].Text.ToString();
return cellValue;
}
#2
3
try this
private string getCellValue(Worksheet Sheet, int Row, int Column)
{
object cellValue = Sheet.Cells(Row, Column).Value;
if (cellValue != null) {
return Convert.ToString(cellValue);
} else {
return string.Empty;
}
}
#3
1
Try to use this code below:
尝试使用以下代码:
private string getCellValue(Worksheet Sheet, int Row, int Column)
{
string cellValue = Sheet.Cells[Row, Column].Text.ToString();
return cellValue;
}
Hope it works!
希望它有效!
#4
0
Try with:
double dbl =(double)cell.Value;
isDouble = String.Format("{0:0}", dbl).ToString();
This works for me.
这对我有用。