老是,文件解析失败,但保存成功,代码也没写错呀
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | package com.cl.service; import com.cl.data.Course; import com.cl.dto.ImportExcelParamDto; import com.cl.dto.ImportExcelResultDto; import com.cl.util.FileUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; public class ExcelService { public CourseService imp(ImportExcelParamDto paramDto) throws IOException { //有来有回,对导入的一个结果进行预处理,封装一个复杂对象,对出错信息进行一个处理 // ImportExcelResultDto result=new ImportExcelResultDto(); CourseService result= new CourseService(); // result.setTitle(paramDto.getTitle()); List<Course> courseList= new ArrayList<>(); result.setCs(courseList); String fileName = null ; try { fileName = FileUtil.save(paramDto.getExcel(), FileUtil.SAVE_PATH); } catch (Exception e) { e.printStackTrace(); result.setMsg( "上传文件保存失败!" ); } if (fileName != null ) { Workbook workbook = null ; try { //另外一种需求就是,只需要获取到文件的输入流,而不要保存文件 //方法,new File(paramDto.getExcel().getInputStream())即可 workbook = WorkbookFactory.create( new File(FileUtil.SAVE_PATH+fileName)); Sheet sheet = workbook.getSheetAt( 0 ); //sheet-row-cell int rowNum = sheet.getLastRowNum(); for ( int i = 0 ; i <= rowNum; i++) { Course course= new Course(); Row row = sheet.getRow(i); course.setCourseId(row.getCell( 0 ).getStringCellValue()); course.setCourseName(row.getCell( 1 ).getStringCellValue()); course.setCourseType(row.getCell( 2 ).getStringCellValue()); course.setDescription(row.getCell( 3 ).getStringCellValue()); course.setCourseTime(( float )row.getCell( 4 ).getNumericCellValue()); course.setUsername(row.getCell( 5 ).getStringCellValue()); courseList.add(course); System.out.println(row.getCell( 0 ).getStringCellValue()); System.out.println(row.getCell( 1 ).getStringCellValue()); System.out.println(row.getCell( 2 ).getStringCellValue()); System.out.println(row.getCell( 3 ).getStringCellValue()); System.out.println(( float )row.getCell( 4 ).getNumericCellValue()); System.out.println(row.getCell( 5 ).getStringCellValue()); } } catch (Exception e){ e.printStackTrace(); result.setMsg( "文件解析失败" ); } finally { if (workbook != null ) { try { //释放wookBook所占用的资源 workbook.close(); } catch (IOException e) { e.printStackTrace(); } } } } return result; } //导出课程相关信息 public Workbook export( boolean flag){ Workbook workbook; //这个地方有分为03和07版excel if (flag){ //03 workbook= new XSSFWorkbook(); } else { //07 workbook= new HSSFWorkbook(); } //思路:sheet->row->cell Sheet sheet = workbook.createSheet( "my sheet" ); //创建一个二维数据来模拟数据 List<List<String>> content = this .getContent(); for ( int i= 0 ;i<content.size();i++) { Row row = sheet.createRow(i); List<String> rowData=content.get(i); for ( int j = 0 ; j < rowData.size(); j++) { row.createCell(j).setCellValue(rowData.get(j)); System.out.println(content.get(i).get(j)); } } return workbook; } //获取用户提交的数据 private List<List<String>> getContent(){ return null ; } } |
0
收起
正在回答 回答被采纳积分+1
2回答
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧