一.需要读取的excel格式如下图
二需要读取的excel格式如下图:
Java实现方式:
1.在pom中添加依赖
|
2.使用Excel工具类
|
3.编写实现一中读取excel的类
|
4.编写实现二中读取excel的类
public static Map<String, List<TQuestion>> addReportByExcel(InputStream inputStream, String fileName){ boolean isE2007 = false; //判断是否是excel2007格式 if(fileName.endsWith("xlsx")){ isE2007 = true; } try { InputStream input = inputStream; //建立输入流 Workbook wb = null; //根据文件格式(2003或者2007)来初始化 if(isE2007){ wb = new XSSFWorkbook(input); }else{ wb = new HSSFWorkbook(input); } Sheet sheet = wb.getSheetAt(0); //获得第一个表单 int count = sheet.getLastRowNum()+1;//总行数 Map<String,List<TQuestion>> datas = new HashMap<>(); List<TQuestion> data = new ArrayList<>(); for(int i = 1; i < count;i++){ Row row = sheet.getRow(i); TQuestion question = new TQuestion(); String score = null; String title = ReadExcelUtil.getCellValue(row.getCell(0)); String optionA = ReadExcelUtil.getCellValue(row.getCell(1)); String optionB = ReadExcelUtil.getCellValue(row.getCell(2)); String optionC = ReadExcelUtil.getCellValue(row.getCell(3)); String optionD = ReadExcelUtil.getCellValue(row.getCell(4)); String rightAnswer = ReadExcelUtil.getCellValue(row.getCell(5)); if(StringUtils.isNumeric(ReadExcelUtil.getCellValue(row.getCell(6)))){ score = ReadExcelUtil.getCellValue(row.getCell(6)); }else { score = "0"; } String type = ReadExcelUtil.getCellValue(row.getCell(7)); String nd = ReadExcelUtil.getCellValue(row.getCell(8)); question.setTitle(title); question.setOptiona(optionA); question.setOptionb(optionB); question.setOptionc(optionC); question.setOptiond(optionD); question.setRightAnswer(rightAnswer); question.setScore(Double.valueOf(score)); question.setType(type); question.setNd(nd); data.add(question); } datas.put("objs",data); return datas; } catch (Exception ex) { return null; } public static void main(String[] args) throws FileNotFoundException { String filename = "123.pdf"; String filePath = "E:/ideaWorkSpace"; File dest = new File(filePath+"/"+filename); InputStream is = new FileInputStream(dest); addReportByExcel(is,filename); } } |