Java 读取带有POI(无标题)的Excel文件
我想读excel文件使用poi没有标题头,我的预期结果是这个 这是我的密码Java 读取带有POI(无标题)的Excel文件,java,excel,apache-poi,Java,Excel,Apache Poi,我想读excel文件使用poi没有标题头,我的预期结果是这个 这是我的密码 public String processExcel(Model model, @RequestParam(value = "excelfile", required = false) MultipartFile excelfile, HttpSession session) { try { List<UserRegistrationDetail>
public String processExcel(Model model, @RequestParam(value = "excelfile", required = false) MultipartFile excelfile, HttpSession session) {
try {
List<UserRegistrationDetail> lstUser = new ArrayList<>();
Workbook workbook = null;
if (excelfile.getOriginalFilename().endsWith("xlsx")) {
workbook = new XSSFWorkbook(excelfile.getInputStream());
} else if (excelfile.getOriginalFilename().endsWith("xls")) {
workbook = new HSSFWorkbook(excelfile.getInputStream());
} else {
model.addAttribute("msg", new IllegalArgumentException("The specified file is not Excel file"));
}
Sheet worksheet = workbook.getSheetAt(0);
Iterator<Row> iterator = worksheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
UserRegistrationDetail user = new UserRegistrationDetail();
while (cellIterator.hasNext()) {
Cell nextCell = cellIterator.next();
int columnIndex = nextCell.getColumnIndex();
switch (columnIndex) {
case 0:
user.setId(String.valueOf(nextCell.getNumericCellValue()));
break;
case 1:
user.setEmail(nextCell.getStringCellValue());
break;
case 2:
user.setFullname(nextCell.getStringCellValue());
break;
}
}
lstUser.add(user);
}
model.addAttribute("listUser", lstUser);
session.setAttribute("listUserImport", lstUser);
} catch (Exception e) {
model.addAttribute("msg", e.getMessage());
}
return "reportregistrationuser";
}
publicstringprocessexcel(模型模型@RequestParam(value=“excelfile”,required=false)多部分文件excelfile,HttpSession会话){
试一试{
List lstUser=new ArrayList();
工作簿=空;
if(excelfile.getOriginalFilename().endsWith(“xlsx”)){
工作簿=新的XSSF工作簿(excelfile.getInputStream());
}else if(excelfile.getOriginalFilename().endsWith(“xls”)){
工作簿=新的HSSF工作簿(excelfile.getInputStream());
}否则{
model.addAttribute(“msg”,新的IllegalArgumentException(“指定的文件不是Excel文件”);
}
工作表=工作簿。getSheetAt(0);
迭代器迭代器=工作表。迭代器();
while(iterator.hasNext()){
行nextRow=iterator.next();
迭代器cellIterator=nextRow.cellIterator();
UserRegistrationDetail用户=新的UserRegistrationDetail();
while(cellIterator.hasNext()){
Cell-nextCell=cellIterator.next();
int columnIndex=nextCell.getColumnIndex();
开关(列索引){
案例0:
user.setId(String.valueOf(nextCell.getNumericCellValue());
打破
案例1:
user.setEmail(nextCell.getStringCellValue());
打破
案例2:
user.setFullname(nextCell.getStringCellValue());
打破
}
}
lstUser.add(用户);
}
model.addAttribute(“listUser”,lstUser);
setAttribute(“listUserImport”,lstUser);
}捕获(例外e){
model.addAttribute(“msg”,e.getMessage());
}
返回“reportregistrationuser”;
}
目前我的代码只读取像这样的excel文件
如何实现我的预期结果,我在做什么?在迭代每一行之前,首先使用
iterator.next()将迭代器移动到第二行。
因此,在while循环中,它将从第二行开始
Iterator<Row> iterator = worksheet.iterator();
//Add the below line
Row headerRow= iterator.next();
Iterator Iterator=工作表.Iterator();
//添加下面的行
Row headerRow=iterator.next();