java解析excel:案例一
需求:
转成
代码如下:
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; import com.common.util.ExcelReader; import com.common.util.dto.ExcelDto; import com.common.util.dto.ExcelSheetDto; import com.dr.util.PublicUtil; public class ExcelReaderTest { public static void main(String[] args) throws FileNotFoundException { //引入excel解析 ExcelReader excelReader = new ExcelReader(); // List<Map> list = new ArrayList<Map>(); Map<String, List<String>> map = new HashMap<String, List<String>>(); File file = new File("E:\\testExcel\\sxd.xlsx"); try { ExcelDto excelDto = excelReader.readExcel(new FileInputStream(file), file.getName()); List<ExcelSheetDto> excelSheet= excelDto.getExcelSheetList(); //获取最后一个不为空的sheet ExcelSheetDto sheetDto=PublicUtil.getLastNotNullSheet(excelSheet); String[] colNames = sheetDto.getColNames(); String[][] contents = sheetDto.getRowsContent(); // 设置时间 for (int i = 1; i < contents.length; i++) { if (contents[i] != null && StringUtils.isEmpty(contents[i][0]) && i > 0) { contents[i][0] = contents[i - 1][0]; } } if(colNames!=null){ for (int i = 0; i < colNames.length; i++) { int count = 1; String colName = colNames[i]; List<String> list = new ArrayList<String>(); for (int j = 0; j < contents.length; j++) { list.add(contents[j][i]); } map.put(colName, list); } for (int i = 0; i < map.get("日期").size()/2; i++) { for (int j = 1; j < 5; j++) { System.out.println(map.get("日期").get(i*2)); String apptype = "XJ_JLM_JLM0"+j; System.out.println(apptype); System.out.println(map.get(apptype).get(i*2)); System.out.println(map.get(apptype).get(i*2+1)); } } } } catch (Exception e) { e.printStackTrace(); } } }