侧边栏壁纸
  • 累计撰写 46 篇文章
  • 累计创建 38 个标签
  • 累计收到 2 条评论
隐藏侧边栏

poi导出工具类

哒卟刘先生
2022-01-10 / 0 评论 / 0 点赞 / 112 阅读 / 3,252 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-01-10,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

poi导出excel工具类,留着备用

1,依赖

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.15</version>
		</dependency>

工具类

public class ExcelExportUtil {

    /**
     * 导出Excel
     * @param sheetName sheet名称
     * @param title 标题
     * @param values 内容
     * @param wb HSSFWorkbook对象
     * @return
     */
    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String []title, String [][]values, HSSFWorkbook wb){

        // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
        if(wb == null){
            wb = new HSSFWorkbook();
        }

        // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        //声明列对象
        HSSFCell cell = null;

        //创建标题
        for(int i=0;i<title.length;i++){
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        //创建内容
        for(int i=0;i<values.length;i++){
            row = sheet.createRow(i + 1);
            for(int j=0;j<values[i].length;j++){
                //将内容按顺序赋给对应的列对象
                row.createCell(j).setCellValue(values[i][j]);
            }
        }
        return wb;
    }
}

3,接口

 @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {

        List<ActivityRegisterDto> list = activityRegisterBaseService.getlistAll();
        //excel标题
        String[] title = {"ID", "登录账号", "姓名", "年龄", "手机号", "报名时间", "地区", "推荐机构", "辅导老师", "支付状态"};

        //excel文件名
        String fileName = "活动报名信息表" + System.currentTimeMillis() + ".xls";
        //sheet名
        String sheetName = "活动报名信息表";
        int size = list.size();
        String [][] content = new String[size][10];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            ActivityRegisterDto registerDto = list.get(i);
            content[i][0] = registerDto.getId() + "";
            content[i][1] = registerDto.getUserName() + "";
            content[i][2] = registerDto.getName() + "";
            content[i][3] = registerDto.getAge() + "";
            content[i][4] = registerDto.getPhone() + "";
            content[i][5] = registerDto.getCreateTime() + "";
            content[i][6] = registerDto.getAddress() + "";
            content[i][7] = registerDto.getRecommendOrg() + "";
            content[i][8] = registerDto.getTeacherName() + "";
            content[i][9] = registerDto.getPayState() + "";
        }

        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelExportUtil.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

相应流方法

//发送响应流方法
    public 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();
        }
    }
0

评论区