简体   繁体   English

使用JAVA apache poi从xlsx读取数据无法读取行的最后一个单元格

[英]Read data from xlsx using JAVA apache poi fails to read the last cell of a row

Below is the code to read data from 9 excel cells.下面是从 9 个 excel 单元格读取数据的代码。

Excel data format is at the end of code. Excel 数据格式位于代码末尾。 It has two rows.它有两排。 It never reads the last cells data,tried all the combinations它从不读取最后一个单元格数据,尝试了所有组合

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Exceldata {
public static XSSFWorkbook workbook = null;
public static XSSFSheet sheet = null;
public static XSSFRow row = null;
public static XSSFCell cell = null;
public static  String[] col_name= new String[9];
public static String[][] user_data=new String[2][9];

public static void main(String[] args) {
try {
 File src= new File("C:\\Users\\Public\\Testdata.xlsx");
 FileInputStream fis=new FileInputStream(src);
 XSSFWorkbook book= new XSSFWorkbook(fis);         
 XSSFSheet sheet_userdata= book.getSheet("UserDetails");          
 for(int p=0;p<book.getNumberOfSheets();p++) 
 {
  System.out.println("Sheet Name is : "+ book.getSheetName(p));
  }
 row=sheet_userdata.getRow(0);
 for(int j =0;j<9;j++) 
 {
  cell=row.getCell(j);
  col_name[j]=String.valueOf(cell.getStringCellValue());
  System.out.println(col_name[j]);
  }
   //Read  data from cells
  for(int i=0;i<sheet_userdata.getLastRowNum();i++)
  {
  row=sheet_userdata.getRow(i+1);
  for(int k=0;k<9;k++) 
{
  if((cell=row.getCell(k))!= null) 
 {
  switch(cell.getCellType())
{
 case Cell.CELL_TYPE_STRING:
   if(k==1) 
 {
 user_data[i][k]=String.valueOf(cell.getStringCellValue());
 System.out.println(col_name[k]+" is : ");
 System.out.println(user_data[i][k]);
 }else if(k==2)
{
 user_data[i][k]=String.valueOf(cell.getStringCellValue());
 System.out.println(col_name[k]+" is : ");
 System.out.println(user_data[i][k]);
}
 else if(k==3) 
{
 user_data[i][k]=String.valueOf(cell.getStringCellValue());
 System.out.println(col_name[k]+" is : ");
 System.out.println(user_data[i][k]);
}
else if(k==5) 
{
 user_data[i][k]=String.valueOf(cell.getStringCellValue());
 System.out.println(col_name[k]+" is : ");
 System.out.println(user_data[i][k]);
 }
else if(k==6) 
{
 user_data[i][k]=String.valueOf(cell.getStringCellValue());
 System.out.println(col_name[k]+" is : ");
 System.out.println(user_data[i][k]);
 }
else if(k==7) 
{
 user_data[i][k]=String.valueOf(cell.getStringCellValue());
 System.out.println(col_name[k]+" is : ");
 System.out.println(user_data[i][k]);
 }
 else if(k==8) 
{
  user_data[i][k]=String.valueOf((cell.getStringCellValue()));
System.out.println(col_name[k]+" is : ");
System.out.println(user_data[i][k]);
}
break;
case Cell.CELL_TYPE_NUMERIC:
 if(k==0) 
{
 user_data[i][k]=String.valueOf((long)(cell.getNumericCellValue()));
 System.out.println(col_name[k]+" is : ");                                        
 System.out.println(user_data[i][k]);                                                                              
 }
else if(k==4) 
{
 user_data[i][k]=String.valueOf((long)(cell.getNumericCellValue()));
 System.out.println(col_name[k]+" is : ");
 System.out.println(user_data[i][k]);
 }
break;
 case Cell.CELL_TYPE_BOOLEAN:
user_data[i][k]=cell.getStringCellValue();
System.out.println(user_data[i][k]);
 break;
case Cell.CELL_TYPE_BLANK:
 //user_data[i][k]="";
if(k==8) 
{
 user_data[i][k]=String.valueOf((cell.getStringCellValue()));
 System.out.println(col_name[k]+" is : ");
 System.out.println(user_data[i][k]);
}
break;
case Cell.CELL_TYPE_FORMULA:
  user_data[i][k]=String.valueOf(cell.getCellFormula());
 System.out.println(user_data[i][k]);
 break;
 case Cell.CELL_TYPE_ERROR:
user_data[i][k]=String.valueOf(cell.getErrorCellString());
 System.out.println(user_data[i][k]);
 break;
}
}
}
}
}catch(Exception e) 
{
e.printStackTrace();
}
                                                            
                                            
 }

 }

XLS sheet data has two rows like this( NOte : Status cell is empty) XLS 工作表数据有两行像这样(注意:状态单元格为空)

S.No.| S.No.| ID |First Name |Last Name |Contact number |Email id |Usermode |channel |Status 1 test@test.com Ram singh 45455666 xyz@test.com Password XYZ empty cell ID |名字 |姓氏 |联系电话 |电子邮件 ID |用户模式 ​​|频道 |状态 1 test@test.com Ram singh 45455666 xyz@test.com 密码 XYZ 空单元格

Option 1 you have checked cell null condition but not "" this condition.There might be chances that if you have added data manually to excel sheet that cell is not BLANK and have some space etc. Try to Add this选项 1 您已经检查了单元格空条件,但没有检查“”这个条件。如果您手动将数据添加到 Excel 工作表,该单元格不是空白的并且有一些空间等。尝试添加这个

if((row.getCell(k))!= ""){
//now check column is 8
if(k==8)

Option 2 That cell type might be UNKNOWN so add this case as well选项 2 该单元格类型可能是 UNKNOWN 所以也添加这种情况

case Cell.CellType _NONE 

Option 3 Your are expecting that cell type as blank but it could be either numeric or string so try to add K==8 condition in both of this switch case or find out specific type of that cell first.选项 3 您期望该单元格类型为空白,但它可以是数字或字符串,因此请尝试在这两种切换案例中添加 K==8 条件或首先找出该单元格的特定类型。

实际上,当使用 getcell(int k); 时,当单元格为空或空或没有值时,它会给出 NULL 指针异常;因此,解决方案是在 if((cell=row.getCell(k))!=null) 之前添加一行以将 null 转换为空白,如下所示 cell=row.getcell(k,row.CREATE_NULL_AS_BLANK)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM