借助POI的excel接口,可以方便得实现excel导出功能:

首先需要引入poi对应的jar包

1.前端jsp页面需要一个a链接。

web页面文件MIM类型的下载,只需要一个a元素,该a可以链到该文件在服务器端的实体路径,也可以链接到一个servelt,将该文件写到response的输出流中。其他的下载浏览器会自动帮助完成。

<a type="button" class="btn btn-warning" href='/WebTest/ExportExcel' id='export'>导出</a>

2,该链接对应的后台处理servlet代码实现

package com.bobo.servlet;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.bobo.helper.ExcelHelper;
import com.bobo.modal.ShitiModal;
import com.bobo.modal.XixiangModal;

public class ExportExcel extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        process(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        process(request, response);
    }

    @SuppressWarnings("deprecation")
    private void process(HttpServletRequest request,
            HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        response.reset();
        response.setContentType("application/x-msexcel");

        String fileName = URLEncoder.encode("巡查实体列表", "utf-8");
        response.addHeader("Content-Disposition", "attachment; filename="
                + new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xls");
        HSSFWorkbook wb = new HSSFWorkbook();
        // 表头数足
        String[] title = { "实体编号", "实体名称", "", "", "巡查人", "巡查结果", "巡查时间",
                "细项名称", "巡查人", "巡查结果", "巡查时间", "不合格原因" };
        ArrayList<ShitiModal> content = new ArrayList<ShitiModal>();
        for (int i = 0; i < 6; i++) {
            ShitiModal shiti = new ShitiModal();
            shiti.setShitiCity("杭州");
            shiti.setShitiName("二龙路基站");
            shiti.setShitiNum("9405844939");
            shiti.setShitiPerson("张三");
            shiti.setShitiProvince("浙江");
            shiti.setShitiResult("不合格");
            shiti.setShitiTime("2015年4月28日");
            // 填充细分项
            ArrayList<XixiangModal> xixiangList = new ArrayList<XixiangModal>();
            for (int j = 0; j < 2; j++) {
                XixiangModal xixiang = new XixiangModal();
                xixiang.setXixiangMember("张三");
                xixiang.setXixiangName("开关电源");
                xixiang.setXixiangReason("管道线路井内余线没有靠墙");
                xixiang.setXixiangResult("不合格");
                xixiang.setXixiangTime("2015年4月28日");
                xixiangList.add(xixiang);
            }
            shiti.setXixiangList(xixiangList);
            content.add(shiti);
        }
        ExcelHelper eHelper = new ExcelHelper();
        // 生成excel中的一张表格
        HSSFSheet sheet = eHelper.export(wb, "sheet1", title, content);

        // 直接存到服务器端
        // FileOutputStream fileOut;
        // try {
        // fileOut = new FileOutputStream("workbook.xls");
        // wb.write(fileOut);
        // fileOut.close();
        // } catch (Exception e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
        // 写入输出流
        try {
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

3,相关实体类

package com.bobo.modal;

import java.util.ArrayList;

public class ShitiModal {
    private String shitiNum;
    private String shitiName;
    private String shitiProvince;
    private String shitiCity;
    private String shitiPerson;
    private String shitiTime;
    private String shitiResult;
    private ArrayList<XixiangModal> xixiangList;    
    public String getShitiPerson() {
        return shitiPerson;
    }

    public void setShitiPerson(String shitiPerson) {
        this.shitiPerson = shitiPerson;
    }

    public String getShitiTime() {
        return shitiTime;
    }

    public void setShitiTime(String shitiTime) {
        this.shitiTime = shitiTime;
    }

    public String getShitiResult() {
        return shitiResult;
    }

    public void setShitiResult(String shitiResult) {
        this.shitiResult = shitiResult;
    }



    public String getShitiNum() {
        return shitiNum;
    }

    public void setShitiNum(String shitiNum) {
        this.shitiNum = shitiNum;
    }

    public String getShitiName() {
        return shitiName;
    }

    public void setShitiName(String shitiName) {
        this.shitiName = shitiName;
    }

    public String getShitiProvince() {
        return shitiProvince;
    }

    public void setShitiProvince(String shitiProvince) {
        this.shitiProvince = shitiProvince;
    }

    public String getShitiCity() {
        return shitiCity;
    }

    public void setShitiCity(String shitiCity) {
        this.shitiCity = shitiCity;
    }

    public ArrayList<XixiangModal> getXixiangList() {
        return xixiangList;
    }

    public void setXixiangList(ArrayList<XixiangModal> xixiangList) {
        this.xixiangList = xixiangList;
    }

}
ShitiModal

相关文章:

  • 2022-12-23
  • 2021-05-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-15
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
相关资源
相似解决方案