码迷,mamicode.com
首页 > 其他好文 > 详细

Excel导出

时间:2019-08-19 00:03:50      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:exce   version   ppi   XML   byte   chm   service   list   content   

首先需要在pom.xml导入相关依赖,如下:

 <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
 </dependency>

控制层代码:

   @GetMapping("/export")
    public ResponseEntity<byte[]> exportExcel() throws IOException {
        return POIUtils.exportExcel((List<Position>) positionService.getAllPosition(null,null).getData());

    }

更新service层getAllPosition方法,对分页page和size做一个飞空判断

   public RespPageBean getAllPosition(Integer page,Integer size) {
        if (page!=null&&size!=null) {
            page = (page - 1) * size;
        }
      List<Position> list= positionMapper.getAllPosition(page,size);
      RespPageBean respPageBean=new RespPageBean();
      respPageBean.setData(list);
      respPageBean.setTotal(positionMapper.getTotal());
        return respPageBean;
    }

并更新positionMapper.xml文件做非空判断

 <select id="getAllPosition" resultMap="BaseResultMap">
    select * from position
    <if test="page !=null and size !=null">
      limit #{page},#{size}
    </if>
  </select>

然后在config包下创建POIUtils类,并定义exportExcel方法如下:

public class POIUtils {
    public static ResponseEntity<byte[]> exportExcel(List<Position> positions) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        workbook.createInformationProperties();
        SummaryInformation information = workbook.getSummaryInformation();
        information.setTitle("职位表");
        information.setAuthor("javaboy");
        information.setComments("备注");
HSSFSheet hssfSheet
= workbook.createSheet("职位表");//创建表格对象,并定义表名称 HSSFRow row0 = hssfSheet.createRow(0);//创建字段行 HSSFCell c0 = row0.createCell(0);//创建第一列 HSSFCell c1 = row0.createCell(1); HSSFCell c2 = row0.createCell(2); HSSFCell c3 = row0.createCell(3); c0.setCellValue("编号");//设置第一行第一列字段 c1.setCellValue("名称"); c2.setCellValue("创建时间"); c3.setCellValue("是否启用");
HSSFCellStyle dateCellStyle
= workbook.createCellStyle(); dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));//设置日期格式 for (int i = 0; i < positions.size(); i++) { HSSFRow r = hssfSheet.createRow(i + 1); HSSFCell cell0 = r.createCell(0); Position p = positions.get(i); ((HSSFCell) cell0).setCellValue(p.getId());//设置对应数据 HSSFCell cell1 = r.createCell(1); ((HSSFCell) cell1).setCellValue(p.getName()); HSSFCell cell2 = r.createCell(2); cell2.setCellStyle(dateCellStyle); ((HSSFCell) cell2).setCellValue(p.getCreatedate()); HSSFCell cell3 = r.createCell(3); ((HSSFCell) cell3).setCellValue(p.getEnabled() ? "" : ""); } HttpHeaders headers = new HttpHeaders(); headers.setContentDispositionFormData("attachment", new String("职位表222.xls".getBytes("UTF-8"), "iso-8859-1")); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); ByteArrayOutputStream baos = new ByteArrayOutputStream(); workbook.write(baos); return new ResponseEntity<>(baos.toByteArray(), headers, HttpStatus.CREATED); } }

 

Excel导出

标签:exce   version   ppi   XML   byte   chm   service   list   content   

原文地址:https://www.cnblogs.com/zzjlxy-225223/p/11374384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!