poi导出表格设置标题

> package com.port.socket.controller;
> 
> import com.port.socket.bean.CellModel; import
> com.port.socket.utils.createCSVUtil; import
> org.apache.poi.hssf.usermodel.*; import
> org.apache.poi.ss.usermodel.HorizontalAlignment; import
> org.apache.poi.ss.usermodel.VerticalAlignment; import
> org.apache.poi.ss.util.CellRangeAddress; import
> org.apache.poi.xssf.streaming.SXSSFWorkbook; import
> org.springframework.stereotype.Controller; import
> org.springframework.web.bind.annotation.RequestMapping; import
> org.springframework.web.bind.annotation.ResponseBody;
> 
> import javax.servlet.http.HttpServletResponse; import java.io.*;
> import java.util.*;
> 
> /**  * @description:  * @projectName:demo  *
> @see:com.port.socket.controller  * @FileName:aad2  * @author:****  *
> @createTime:2020/6/2 17:31  * @version:1.0  */
> @RequestMapping("/demo") @Controller public class aad2 {
>         /**
>          * description 导出excel 操作表头
>                 * author **
>                 * createTime 2020/6/17 15:56
>                 * param [response]
>                 * return java.util.Map<java.lang.String,java.lang.Object>
>          **/
>         @RequestMapping("/excel")
>         @ResponseBody
>         public Map<String,Object> text(HttpServletResponse response) throws IOException {
>                 HSSFWorkbook workbook = new HSSFWorkbook();
>                 //设置表头居中 样式
>                 HSSFCellStyle style = workbook.createCellStyle();
>                 //设置表头字体
>              HSSFFont font = workbook.createFont();
> 
>             HSSFSheet sheet = workbook.createSheet("sheet");
>             //存放表格表头
>             getTitle(style,sheet,font);
> 
>             String fileName="demo.xls";
>             //设置头信息
>             response.setHeader("Content-disposition",
>                     "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));// 设置文件头编码格式
>             response.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");//
> 设置类型
>             response.setHeader("Cache-Control", "no-cache");// 设置头
>             response.setDateHeader("Expires", 0);// 设置日期头
>             // 输出到下载人的电脑上
>             workbook.write(response.getOutputStream());
>             // 刷新
>             response.getOutputStream().flush();
>             // 关闭
>             response.getOutputStream().close();
>             return (Map<String, Object>) new HashMap<String, Object>().put("asdf","ad");
> 
>         }
> 
>     private void getTitle(HSSFCellStyle style, HSSFSheet sheet,HSSFFont font) {
>         style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中
>         font.setFontName("黑体");//设置字体
>         style.setWrapText(true);//自动换行
>         style.setFont(font); //设置引用字体
>         List<String> li= new ArrayList<String>();
>         li.add("项目名称");li.add("项目类别");li.add("项目类型");li.add("项目单位");
>         li.add("是否跨年");li.add("上报国网"); li.add("项目负责人");li.add("开始时间");
>         li.add("结束时间");li.add("项目内容"); li.add("总投资(万)");
>         HSSFRow row0 = sheet.createRow(0);
>         for (int i = 0; i < li.size(); i++) {
>             HSSFCell cell_00 = row0.createCell(i);//获取表头
>             cell_00.setCellStyle(style);cell_00.setCellValue(li.get(i));
>             //设置合并行或列  CellRangeAddress("开始行","结束行","开始列","结束列");
>             sheet.addMergedRegion( new CellRangeAddress(0,1, i, i));
>         }
>         int i=11;
>         HSSFCell cell_011 = row0.createCell(i);
>         int t=i+2;
>         cell_011.setCellStyle(style);cell_011.setCellValue("物资采购");
>         sheet.addMergedRegion(new CellRangeAddress(0, 0, i, t));
>         i=t+1;
>         HSSFCell cell_012 = row0.createCell(i);
>         t=i+2;
>         cell_012.setCellStyle(style);cell_012.setCellValue("服务采购");
>         sheet.addMergedRegion(new CellRangeAddress(0,0, i, t));
>         i=t+1;
>         HSSFCell cell_013 = row0.createCell(i);
>         t=i+2;
>         cell_013.setCellStyle(style);cell_013.setCellValue("项目实施");
>         sheet.addMergedRegion(new CellRangeAddress(0,0, i, t));
>         i=t+1;
>         HSSFCell cell_014 = row0.createCell(i);
>         t=i+2;
>         cell_014.setCellStyle(style);cell_014.setCellValue("项目验收");
>         sheet.addMergedRegion( new CellRangeAddress(0,0, i, t));
>         HSSFRow row1 = sheet.createRow(1);
>         HSSFCell cell_021 = row1.createCell(11);
>         cell_021.setCellStyle(style);cell_021.setCellValue("物资1");
>         HSSFCell cell_022 = row1.createCell(12);
>         cell_022.setCellStyle(style);cell_022.setCellValue("物资2");
>         HSSFCell cell_023 = row1.createCell(13);
>         cell_023.setCellStyle(style);cell_023.setCellValue("物资3");
>         } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值