excel导入
-
public static List<Map<String,Object>> test1() throws Exception{
-
File file = new File("C:/Users/luo_a/Desktop/数据测试.xlsx");
-
if (!file.exists()){
-
throw new Exception("文件不存在!");
-
}
-
InputStream in = new FileInputStream(file);
-
-
// 读取整个Excel
-
XSSFWorkbook sheets = new XSSFWorkbook(in);
-
// 获取第一个表单Sheet
-
XSSFSheet sheetAt = sheets.getSheetAt(0);
-
//默认第一行为标题行,i = 0
-
XSSFRow titleRow = sheetAt.getRow(0);
-
List<Map<String,Object>> mapList = new ArrayList<>();
-
// 循环获取每一行数据
-
for (int i = 1; i < sheetAt.getPhysicalNumberOfRows(); i++) {
-
XSSFRow row = sheetAt.getRow(i);
-
// 读取每一列内容
-
Map<String,Object> map = new HashMap<>();
-
for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) {
-
XSSFCell titleCell = titleRow.getCell(index);
-
XSSFCell cell = row.getCell(index);
-
cell.setCellType(CellType.STRING);
-
if (cell.getStringCellValue().equals("")) {
-
continue;
-
}
-
//表头数据
-
String titleName = titleCell.getStringCellValue();
-
//单元格内容
-
String valueName = cell.getStringCellValue();
-
//每一行的数据
-
map.put(titleName,valueName);
-
}
-
mapList.add(map);
-
}
-
System.out.println(JSON.toJSONString(mapList));
-
return mapList;
-
}