麻烦老师看看,为什么我的导出功能无法实现?

麻烦老师看看,为什么我的导出功能无法实现?

以下是在index页面提示的错误信息:

HTTP Status 500 – Internal Server Error

Type Exception Report

Message Servlet execution threw an exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile
org.apache.poi.openxml4j.opc.OPCPackage.create(OPCPackage.java:365)
org.apache.poi.xssf.usermodel.XSSFWorkbook.newPackage(XSSFWorkbook.java:495)
org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:261)
org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:257)
org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:245)
org.imooc.service.ExcelService.export(ExcelService.java:56)
org.imooc.servlet.ExportExcelServlet.doPost(ExportExcelServlet.java:18)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

java.lang.ClassNotFoundException: org.apache.commons.compress.archivers.zip.ZipFile
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1309)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1138)
org.apache.poi.openxml4j.opc.OPCPackage.create(OPCPackage.java:365)
org.apache.poi.xssf.usermodel.XSSFWorkbook.newPackage(XSSFWorkbook.java:495)
org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:261)
org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:257)
org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:245)
org.imooc.service.ExcelService.export(ExcelService.java:56)
org.imooc.servlet.ExportExcelServlet.doPost(ExportExcelServlet.java:18)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Note The full stack trace of the root cause is available in the server logs.

然后这是我exportExcel.jsp页面的代码。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE" />
      <title></title>
      <link rel="stylesheet" type="text/css" href="css/all.css" />
      <link rel="stylesheet" type="text/css" href="css/pop.css" />
      <link rel="stylesheet" type="text/css" href="css/main.css" />
   </head>
   <body style="background: #e1e9eb;">
      <form action="${basePath}/exportExcel" id="mainForm" method="post">
         <div class="right">
            <div class="current">
               当前位置:<a href="#">导入/导出</a> &gt; 导出Excel
            </div>
            <div class="rightCont">
               <p class="g_title fix">导出Excel</p>
               <table class="tab1">
                  <tbody>
                     <tr>
                        <td align="right" width="80">标题:</td>
                        <td width="280"><input id="title" value="" style="width: 100%;" class="allInput" type="text" /></td>
                        <td style="text-align: right;" width="150"><input class="tabSub" value="查询" onclick="" type="button" />&nbsp;&nbsp;&nbsp;&nbsp;
                           <input class="tabSub" value="导出" onclick="document.getElementById('mainForm').submit();" type="button" /></td>
                     </tr>
                  </tbody>
               </table>
               <div class="zixun fix">
                  <table class="tab2" width="100%">
                     <tbody>
                        <tr>
                           <th>序号</th>
                           <th>姓名</th>
                           <th>年龄</th>
                           <th>出生日期</th>
                        </tr>
                        <tr>
                           <td>1</td>
                           <td>张三</td>
                           <td>25</td>
                           <td>1990-09-01</td>
                        </tr>
                        <tr>
                           <td>2</td>
                           <td>李四</td>
                           <td>25</td>
                           <td>1990-09-01</td>
                        </tr>
                        <tr>
                           <td>3</td>
                           <td>王五</td>
                           <td>25</td>
                           <td>1990-09-01</td>
                        </tr>
                     </tbody>
                  </table>
               </div>
            </div>
         </div>
      </form>
   </body>
</html>

然后是Servlet

package org.imooc.servlet;

import org.apache.poi.ss.usermodel.Workbook;
import org.imooc.service.ExcelService;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ExportExcelServlet")
public class ExportExcelServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ExcelService service=new ExcelService();
        Workbook workbook = service.export(true);
        response.setHeader("Content-Disposition","attachment;filename=export.xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        //直接将文件输出提供下载导出
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        workbook.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
package org.imooc.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ExportExcelInitServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/jsp/exportExcel.jsp").forward(req,resp);
    }
}

下面的是ExcelService

package org.imooc.service;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.imooc.dto.ImportExcelParamDto;
import org.imooc.dto.ImportExcelResultDto;
import org.imooc.entity.Student;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExcelService {

    public ImportExcelResultDto imp(ImportExcelParamDto dto){
        ImportExcelResultDto result = new ImportExcelResultDto();
        result.setTitle(dto.getTitle());
        List<Student> studentList = new ArrayList<Student>();
        result.setStudentList(studentList);

        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(dto.getExcel().getInputStream());
            Sheet sheet = workbook.getSheetAt(0);
            int rowNum = sheet.getLastRowNum();
            for (int i = 1; i <= rowNum; i++) {
                Row row = sheet.getRow(i);
                Student student = new Student();
                studentList.add(student);
                student.setName(row.getCell(0).getStringCellValue());
                student.setAge((int)row.getCell(1).getNumericCellValue());
                student.setTime(row.getCell(2).getDateCellValue());
            }
        } catch (IOException e) {
            e.printStackTrace();
            result.setMsg("解析Excel失败");
        }finally {
            if (workbook!=null){
                try {
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public Workbook export(boolean isXlsx){
        Workbook workbook;
        if (isXlsx){
            workbook = new XSSFWorkbook();
        }else{
            workbook = new HSSFWorkbook();
        }
        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));
            }
        }
        return workbook;
    }

    private List<List<String>> getContent() {
        List<List<String>> result = new ArrayList<>();
        List<String> row = new ArrayList<>();
        result.add(row);
        row.add("序号");
        row.add("姓名");
        row.add("年龄");
        row.add("时间");

        row = new ArrayList<>();
        result.add(row);
        row.add("1");
        row.add("路人甲");
        row.add("18");
        row.add("2010-01-01");

        row = new ArrayList<>();
        result.add(row);
        row.add("2");
        row.add("路人乙");
        row.add("19");
        row.add("2010-01-02");

        row = new ArrayList<>();
        result.add(row);
        row.add("3");
        row.add("路人丙");
        row.add("20");
        row.add("2010-01-03");
        return result;
    }
}

麻烦老师指点迷津……

正在回答

登陆购买课程后可参与讨论,去登陆

2回答

你好!我用你的代码替换了源代码中的代码,导出没有问题,你用的是老师源码中的POI jar包,还是其他版本的?

http://img1.sycdn.imooc.com//climg/5bd67c740001fca304090347.jpg

祝学习愉快!

  • 我吃芒果哥 提问者 #1
    老师,我用的4.0.0版本的,4个包都有,然后commons-collection4-4.2,fileupload1.3.3,io-2.6,tabglibs的版本都是1.2.5,xmlbeans-3.0.1
    2018-10-29 11:35:30
  • 我吃芒果哥 提问者 #2
    老师,解决了,原来是我没有导入commons-compress这个包,不过课程里也没说,我是看debug里总提示commons下的compress文件中有zip文件有问题,百度解决了。
    2018-10-29 13:42:20
提问者 我吃芒果哥 2018-10-29 13:45:20

http://img1.sycdn.imooc.com//climg/5bd69e37000135ef03140293.jpg

如果还有同学跟我一样的,可以考虑是否是因为没有导入commons-compress这个包,我可能是加载文件流的时候出现了问题。导致在导入excel07,导出excel,导出word时均出现了无法实现功能的问题。

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10205    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师