java 导出excel实例

**

将数据导出xlsx

**
//EasyExcel 导出

		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.0.5</version>
        </dependency>
public void export(HttpServletResponse response) throws Exception {
        try {
        	//查询需要导出的数据
			List<Info> listInfo =infoMapper.query();
            if (CollectionUtils.isEmpty(listInfo)) {
                response.setStatus(HttpStatus.NOT_FOUND.value());
            } else {
                response.setHeader("content-encoding","gzip");
                GZIPOutputStream os = new GZIPOutputStream(response.getOutputStream());
                response.setContentType("application/vnd.ms-excel");
                response.setCharacterEncoding("utf-8");
                response.setHeader("Content-disposition", "attachment;filename=列表.xlsx");
                EasyExcel.write(os, infoVo.class).sheet().doWrite(listInfo);
            }
        }catch (Exception e) {
            log.error("获取列表失败", e);
            throw new Exception("获取列表失败");
        }

    }
@Data
public class infoVo{

    @ExcelProperty("省份")
    private String provinceName;
    @ExcelProperty("市")
    private String cityName;
    @ExcelProperty("区/县")
    private String districtName;
    @ExcelProperty("大区")
    private String regionalName
    @ExcelProperty("时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date time;

}

//第二种方式:输出流导出

public void export(HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        map.put("forestryInspectionVo", forestryInspectionVo);
       List<Info> list=infoMapper.query();
        try {
            String fileName = "表.xls"; // 文件名
            String sheetName = "列表";// sheet名
            String[] title = new String[]{"位置","名称","类型"
            };
            String content[][] = new String[list.size()][];
            for (int i = 0; i < list.size(); i++) {
                content[i] = new String[title.length];
                content[i][0] = list.get(i).getPositioning()==null?null:list.get(i).getPositioning().toString();
                content[i][1] = list.get(i).getUserName()==null?null:list.get(i).getUserName().toString();
                content[i][2] = list.get(i).getTypeName()==null?null:list.get(i).getTypeName().toString();
             
            }
            HSSFWorkbook wb = ExcelUtilPro.getHSSFWorkbook(sheetName, title, content, null);
            // 输出Excel文件
            OutputStream output = response.getOutputStream();
            response.setHeader("Content-Disposition", "attachment; filename="
                    + new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
            wb.write(output);
            output.flush();
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值