一、引子
在web平台开发中仅经常会遇到一下需要批量的问题,通常得做法是使用excel上传,下面主要介绍一下在实际开发中到的实例。
二、准备工作
1、需要导入的jar包(主要用到poi包)
(1)poi-3.14.jar
(2)poi-ooxml-3.14.jar
(3)poi-ooxml-schemas-3.14.jar
(4)xmlbeans-2.6.0.jar
2、主要的API
(1)import org.apache.poi.ss.usermodel.Workbook,对应Excel文档;
(2)import org.apache.poi.hssf.usermodel.HSSFWorkbook,对应xls格式的Excel文档;
(3)import org.apache.poi.xssf.usermodel.XSSFWorkbook,对应xlsx格式的Excel文档;
(4)import org.apache.poi.ss.usermodel.Sheet,对应Excel文档中的一个sheet;
(5)import org.apache.poi.ss.usermodel.Row,对应一个sheet中的一行;
(6)import org.apache.poi.ss.usermodel.Cell,对应一个单元格。
三、上代码
excel格式:
代码:
1 packagetext;2
3 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.io.InputStream;6 importjava.util.ArrayList;7 importjava.util.HashMap;8 importjava.util.LinkedHashMap;9 importjava.util.List;10 importjava.util.Map;11
12 importorg.apache.poi.hssf.usermodel.HSSFWorkbook;13 importorg.apache.poi.ss.usermodel.Cell;14 importorg.apache.poi.ss.usermodel.Row;15 importorg.apache.poi.ss.usermodel.Sheet;16 importorg.apache.poi.ss.usermodel.Workbook;17 importorg.apache.poi.xssf.usermodel.XSSFWorkbook;18
19 public classExcelSolution {20 public static voidmain(String[] args) {21 String filePath = "E:\\XXXXupload\\地点.xlsx";22 List> resultList =ExcelUtil(filePath);23 for(Map map : resultList) {24 System.out.println(map);25 }26 }27 public static List>ExcelUtil(String filePath){28 ArrayList> reList = new ArrayList>();29 File file = newFile(filePath);30 Workbook wb=null;31 if(file.exists() &&file.isFile()){32 try{33 if(filePath.toLowerCase().endsWith(".xls")){34 wb = new HSSFWorkbook(newFileInputStream(file));35 }else if (filePath.toLowerCase().endsWith(".xlsx")) {36 wb = new XSSFWorkbook(newFileInputStream(file));37 }38 Sheet sheet =wb.getSheetAt(0);39 int rowNum = sheet.getPhysicalNumberOfRows(); //获取行数
40 if (rowNum >1) {41 for (int i = 1; i < rowNum; i++) {42 Map map = new HashMap();43 Row row =sheet.getRow(i);44 if (row!=null) {45 //第一列:省份
46 Cell cell0 = sheet.getRow(i).getCell(0);47 if(cell0 != null && !"".equals(cell0)){48 String Name =(String) cell0.getStringCellValue().trim();49 map.put("name", Name);50 }51 Cell cell1 = sheet.getRow(i).getCell(1);52 if(cell1 != null && !"".equals(cell1)){53 String provinceName =(String)cell1.getStringCellValue().trim();54 map.put("provinceName", provinceName);55 }56 Cell cell2 = sheet.getRow(i).getCell(2);57 if(cell0 != null && !"".equals(cell2)){58 String cityName =(String)cell2.getStringCellValue().trim();59 map.put("cityname", cityName);60 }61
62 reList.add(map);63 }64
65 }66
67 }68 } catch(Exception e) {69 System.out.println("************ 解析EXCEL失败 ************");70 e.printStackTrace();71 return null;72 }73 }74 returnreList;75 }76 }
输出:
五、遇到问题及解决
1、报错:
原因: 当导包不一致时会出现问题。
2.代码错误
1 int rowNum = sheet.getPhysicalNumberOfRows(); //获取行数
2 if (rowNum >1) {3 Map map = new HashMap();//出现在错误的行,一个new,存储地址是一样的
4 for (int i = 1; i < rowNum; i++) {5 Row row =sheet.getRow(i);6 if (row!=null) {7 //第一列:省份
8 Cell cell0 = sheet.getRow(i).getCell(0);9 if(cell0 != null && !"".equals(cell0)){10 String Name =(String) cell0.getStringCellValue().trim();11 map.put("name", Name);12 }13
14 reList.add(map);15 }16
17 }18
19 }
错误结果: