JavaWeb使用POI导出Execel

本文参考https://www.cnblogs.com/gudongcheng/p/8268909.html

使用POI导出Execel表格,首先得导入jar包

<!-- poi  -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.10.1</version>
            </dependency>

 工具类代码:

package com.zhiyou.zjc.util;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExportExcel {

     public static void buildExcelDocument(List<Map<String,Object>> list,HttpServletResponse response) throws Exception {
            
            HSSFWorkbook workbook=new HSSFWorkbook();  //工作簿
            // 从model对象中获取userList
            // 创建Excel的sheet
            HSSFSheet sheet = workbook.createSheet("会员列表");
            Map<String,Object> map=list.get(0);
            int num=0;
            HSSFRow first= sheet.createRow(0);
            for(String key: map.keySet()) {
                 first.createCell(num).setCellValue(key);
                 num++;
            }
            // 填充数据
            int rowNum = 1;
            for(Map<String,Object> data:list) {  //得到第n条记录
                HSSFRow row= sheet.createRow(rowNum); //创建一行
                int n=0; //表示该行的列
                for(String key: data.keySet()) {
                     row.createCell(n).setCellValue(data.get(key).toString());
                     n++;
                }
                rowNum++;
            }
            setResponseHeader(response, "会员列表.xls");
            OutputStream os = response.getOutputStream();
            workbook.write(os);
            os.flush();
            os.close();
        }
     //发送响应流方法
        public static void setResponseHeader(HttpServletResponse response, String fileName) {
            try {
                try {
                    fileName = new String(fileName.getBytes(),"ISO8859-1");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                response.setContentType("application/octet-stream;charset=ISO8859-1");
                response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
                response.addHeader("Pargam", "no-cache");
                response.addHeader("Cache-Control", "no-cache");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

}

控制层代码:

@RequestMapping("exportExcel")
    public String exportExcel(@RequestParam(value = "page" ,defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,HttpServletResponse response) {
        List<Map<String, Object>> list=userService.selectMap(page,rows);
        try {
            ExportExcel.buildExcelDocument(list,response);
            return null;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

Service层代码:

public List<Map<String, Object>> selectMap(Integer page, Integer rows) {
        // TODO Auto-generated method stub
        PageHelper.startPage(page, rows);
        List<Map<String,Object>> selectAll = userMapper.selectAll();
        return selectAll;
    } 

SQL语句:(注意返回类型)

 <select id="selectAll" resultType="java.util.LinkedHashMap">
      select * from tb_user
  </select>

前端代码(这里只能使用表单提交,不能使用ajax,否则就不会出现下载本地选择效果

 使用的是EasyUI框架

  text:'导出',
    iconCls:'icon-remove',
    handler:function(){
        var optins = $("#userList").datagrid("getPager").data("pagination").options;
        var page = optins.pageNumber;
        var rows = optins.pageSize;
        var form=$("<form>").attr({
            "action":"/user/exportExcel",
            "method":"POST"
        }).append("<input type='hidden' name='page' value='"+page+"'/>")
        .append("<input type='hidden' name='rows' value='"+rows+"'/>");
        $(document.body).append(form);
        form.submit();
    }

 

posted @ 2019-10-10 22:00  小成~  阅读(662)  评论(0编辑  收藏  举报