1、异常java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException

 

  解决方法:

 

    使用的poi的相关jar包一定版本一定要相同!!!!!


2、maven所使用jar包,没有使用maven的话,就用poi-3.9.jar和poi-ooxml-3.9.jar(这个主要是用于Excel2007以后的版本)两个jar包就行()

  1. <dependency>
  2. <groupId>org.apache.poi</groupId>
  3. <artifactId>poi</artifactId>
  4. <version>3.9</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.poi</groupId>
  8. <artifactId>poi-ooxml</artifactId>
  9. <version>3.9</version>
  10. </dependency>

3、java导入Excel

    先上传Excel
    

  1. //上传Excel
  2. @RequestMapping("/uploadExcel")
  3. public boolean uploadExcel(@RequestParam MultipartFile file,HttpServletRequest request) throws IOException {
  4. if(!file.isEmpty()){
  5. String filePath = file.getOriginalFilename();
  6. //windows
  7. String savePath = request.getSession().getServletContext().getRealPath(filePath);
  8. //linux
  9. //String savePath = "/home/odcuser/webapps/file";
  10. File targetFile = new File(savePath);
  11. if(!targetFile.exists()){
  12. targetFile.mkdirs();
  13. }
  14. file.transferTo(targetFile);
  15. return true;
  16. }
  17. return false;
  18. }

  在读取Excel里面的内容

    

  1. public static void readExcel() throws Exception{
  2. InputStream is = new FileInputStream(new File(fileName));
  3. Workbook hssfWorkbook = null;
  4. if (fileName.endsWith("xlsx")){
  5. hssfWorkbook = new XSSFWorkbook(is);//Excel 2007
  6. }else if (fileName.endsWith("xls")){
  7. hssfWorkbook = new HSSFWorkbook(is);//Excel 2003
  8. }
  9. // HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
  10. // XSSFWorkbook hssfWorkbook = new XSSFWorkbook(is);
  11. User student = null;
  12. List<User> list = new ArrayList<User>();
  13. // 循环工作表Sheet
  14. for (int numSheet = 0; numSheet <hssfWorkbook.getNumberOfSheets(); numSheet++) {
  15. //HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
  16. Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
  17. if (hssfSheet == null) {
  18. continue;
  19. }
  20. // 循环行Row
  21. for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
  22. //HSSFRow hssfRow = hssfSheet.getRow(rowNum);
  23. Row hssfRow = hssfSheet.getRow(rowNum);
  24. if (hssfRow != null) {
  25. student = new User();
  26. //HSSFCell name = hssfRow.getCell(0);
  27. //HSSFCell pwd = hssfRow.getCell(1);
  28. Cell name = hssfRow.getCell(0);
  29. Cell pwd = hssfRow.getCell(1);
  30. //这里是自己的逻辑
  31. student.setUserName(name.toString());
  32. student.setPassword(pwd.toString());
  33. list.add(student);
  34. }
  35. }
  36. }
  37. }

4、导出Excel

  

  1. //创建Excel
  2. @RequestMapping("/createExcel")
  3. public String createExcel(HttpServletResponse response) throws IOException {
  4. //创建HSSFWorkbook对象(excel的文档对象)
  5. HSSFWorkbook wb = new HSSFWorkbook();
  6. //建立新的sheet对象(excel的表单)
  7. HSSFSheet sheet=wb.createSheet("成绩表");
  8. //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
  9. HSSFRow row1=sheet.createRow(0);
  10. //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
  11. HSSFCell cell=row1.createCell(0);
  12. //设置单元格内容
  13. cell.setCellValue("学员考试成绩一览表");
  14. //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
  15. sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
  16. //在sheet里创建第二行
  17. HSSFRow row2=sheet.createRow(1);
  18. //创建单元格并设置单元格内容
  19. row2.createCell(0).setCellValue("姓名");
  20. row2.createCell(1).setCellValue("班级");
  21. row2.createCell(2).setCellValue("笔试成绩");
  22. row2.createCell(3).setCellValue("机试成绩");
  23. //在sheet里创建第三行
  24. HSSFRow row3=sheet.createRow(2);
  25. row3.createCell(0).setCellValue("李明");
  26. row3.createCell(1).setCellValue("As178");
  27. row3.createCell(2).setCellValue(87);
  28. row3.createCell(3).setCellValue(78);
  29. //.....省略部分代码
  30. //输出Excel文件
  31. OutputStream output=response.getOutputStream();
  32. response.reset();
  33. response.setHeader("Content-disposition", "attachment; filename=details.xls");
  34. response.setContentType("application/msexcel");
  35. wb.write(output);
  36. output.close();
  37. return null;
  38. }

 

 

补充说明乱码问题

  1、文件名乱码(我发现只要解决了文件名乱码,其他乱码也会跟着解决)response.setHeader(“Content-disposition”, “attachment; filename=中文.xls”);

  这个方法可以当做一个公用方法来使用,以后有乱码的都可以调用此方法

  1. public static String toUtf8String(String s){
  2. StringBuffer sb = new StringBuffer();
  3. for (int i=0;i<s.length();i++){
  4. char c = s.charAt(i);
  5. if (c >= 0 && c <= 255){sb.append(c);}
  6. else{
  7. byte[] b;
  8. try { b = Character.toString(c).getBytes("utf-8");}
  9. catch (Exception ex) {
  10. System.out.println(ex);
  11. b = new byte[0];
  12. }
  13. for (int j = 0; j < b.length; j++) {
  14. int k = b[j];
  15. if (k < 0) k += 256;
  16. sb.append("%" + Integer.toHexString(k).toUpperCase());
  17. }
  18. }
  19. }
  20. return sb.toString();
  21. }

  调用的时候,response.setHeader(“Content-disposition”, “attachment; filename=”+toUtf8String(“中文.xls”));

 

  我上网查的时候,网上是说

    今天要说的是在创建工作表时,用中文做文件名和工作表名会出现乱码的问题,先说以中文作为工作表名,大家创建工作表的代码一般如下:

        HSSFWorkbook workbook = new HSSFWorkbook();//创建EXCEL文件

        HSSFSheet  sheet= workbook.createSheet(sheetName);    //创建工作表

      这样在用英文名作为工作表名是没问题的,但如果sheetName是中文字符,就会出现乱码,解决的方法如下代码:

  
     HSSFSheet  sheet= workbook.createSheet();

     workbook.setSheetName(0, sheetName,(short)1); //这里(short)1是解决中文乱码的关键;而第一个参数是工作表的索引号。

 

    但是我发现根本没有这个方法,只需要改了文件名的乱码,其他乱码自然就解决了!!!

版权声明:本文为zml-java原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zml-java/p/6146421.html