当前位置:编程学习 > C#/ASP.NET >>

在线求助关于NPIO读取Excel文件的问题

          using( Workbook wb=new HSSFWorkbook(fs))
                     {
                        Sheet sheet= wb.GetSheetAt(0);
                       
                        string title1 = sheet.GetRow(0).GetCell(0).StringCellValue;
                        string title2 = sheet.GetRow(0).GetCell(1).StringCellValue;
                        string title3 = sheet.GetRow(0).GetCell(2).StringCellValue;
                        string title4 = sheet.GetRow(0).GetCell(3).StringCellValue;
                        string sql = null;
                        string homephone = null;
                        string carnum = null;
                        string carenginnum = null;
                         int rowsnum=0;
                        DateTime buytime;
                        if (title1 == "固定电话" && title2 == "车号" && title3 == "车架号" && title4 == "购买时间")
                        {
                            int rowcount = sheet.LastRowNum;
                            for (int i = 1; i <= rowcount; i++)
                            {
                                //导入execl表单元格的数据
                                homephone = sheet.GetRow(i).GetCell(0).StringCellValue == null ? null : sheet.GetRow(i).GetCell(0).StringCellValue; ;
                                carnum = sheet.GetRow(i).GetCell(1).StringCellValue == null ? null : sheet.GetRow(i).GetCell(1).StringCellValue;
                                carenginnum = sheet.GetRow(i).GetCell(2).StringCellValue == null ? null : sheet.GetRow(i).GetCell(2).StringCellValue;
                                buytime =sheet.GetRow(i).GetCell(3).DateCellValue;

在读取单元格之前我已经判断了是否为空值,可是为什么单元格一有空值的时候就表错呢
--------------------编程问答--------------------
homephone = sheet.GetRow(i).GetCell(0).StringCellValue == null ? null : sheet.GetRow(i).GetCell(0).StringCellValue;

错不一定是 StringCellValue  == null
前面的  sheet.GetRow(i).GetCell(0)  sheet.GetRow(i) 也有可能为null --------------------编程问答--------------------
引用 1 楼 df398286232 的回复:
homephone = sheet.GetRow(i).GetCell(0).StringCellValue == null ? null : sheet.GetRow(i).GetCell(0).StringCellValue;

错不一定是 StringCellValue  == null
前面的  sheet.GetRow(i).GetCell(0)  sheet.GetRow(i) 也有可能为null

问题是我已经先判断总行数了,getrow()怎么会NULL呢,而且我发现当单元格有数据的时候才会报错~~~~ --------------------编程问答-------------------- 在线等解决。。。。。。。。。。。。。。 --------------------编程问答-------------------- A列是空的话,GetCell(0)报错吧 --------------------编程问答-------------------- 也许它getrow的时候就错了。单元格没有值和没有这个单元格是不一样的。 --------------------编程问答--------------------
引用 3 楼 woaixiaoxiong 的回复:
在线等解决。。。。。。。。。。。。。。

A列是有值的,就是当单元格空值的时候报错,有值的话不会报错 --------------------编程问答--------------------
引用 5 楼 Jelly1989 的回复:
也许它getrow的时候就错了。单元格没有值和没有这个单元格是不一样的。

行是有数据的,单元格空值的时候报错,有值的话不会报错 --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,