我有一个非常奇怪的问题:
为了阅读 excel,我使用 NPOI nget 包,我的代码是
HttpPostedFileBase file = Request.Files[0];
string fileName = file.FileName;
Stream filestream = file.InputStream;
string fileExtension = System.IO.Path.GetExtension(Request.Files[0].FileName);
if (fileExtension == ".xls" || fileExtension == ".xlsx")
{
HSSFWorkbook hssfwb;
hssfwb= new HSSFWorkbook(filestream);
ISheet sheet = hssfwb.GetSheetAt(0);
for (int row = 1; row <= sheet.LastRowNum; row++)
{
if (sheet.GetRow(row) != null) //null is when the row only contains empty cells
{
Cordinates cord = new Cordinates();
cord.x =sheet.GetRow(row).GetCell(1).StringCellValue;
cord.y = sheet.GetRow(row).GetCell(2).ToString();
cord.h = sheet.GetRow(row).GetCell(3).ToString();
lstCordinatestoReturn.Add(cord);
}
}
所以最奇怪的是我的单元格中的值是 123,233 13,333 (只有数字和逗号)并且无论单元格被格式化为文本单元格 - 我总是用点得到值
cord.y = sheet.GetRow(row).GetCell(2).ToString(); // theresult will ve 123.2333
甚至第一个是给一个例外 ()
cord.x =sheet.GetRow(row).GetCell(1).StringCellValue; // gives an exception : Cannot get a text value from a numeric cell