Java导入excel

需要先倒入POI的jar
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>


public String exportCharge(MultipartHttpServletRequest request) {
try {
//得到上传的文件
MultipartFile fileFile = request.getFile("file");
//转换成输入流
InputStream in = fileFile.getInputStream();
XSSFWorkbook readWb = new XSSFWorkbook(in);
//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数

XSSFSheet sheet = readWb.getSheetAt(0);
//循环行Row
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
System.out.println("当前插入第"+rowNum);
XSSFRow hssfRow = sheet.getRow(rowNum);
AllPhone allPhone=new AllPhone();
if (hssfRow != null) {
for (int colNum = 0; colNum < hssfRow.getPhysicalNumberOfCells(); colNum++) {
String tmp = hssfRow.getCell(colNum).toString();
if (colNum == 0) {
int begin = tmp.indexOf(".");
} else if (colNum == 1) {
allPhone.setStage(tmp);
}else if (colNum == 2) {

}else if (colNum == 3) {
allPhone.setCardtype(tmp);
}else if (colNum == 4) {


}else if (colNum == 5) {
if(StringUtil.isNotEmpty(tmp)){
allPhone.setPostcode(tmp);
}
}
}
}
}

} catch (Exception e) {
e.printStackTrace();
}
return "ok";
}


原文地址:https://www.cnblogs.com/foreverstudy/p/11321234.html