java使用poi自定义excel标题头并导出(springmvc+poi)
项目使用的是jeecg开源框架(springmvc+spring+hibernate+。。。。。。等)此代码仅供参考!如有更好的意见或建议可留言。
创建excel大致分这几步:
1、创建HSSFWorkbook对象(也就是excel文档对象) 2、通过HSSFWorkbook对象创建sheet对象(也就是excel中的sheet) 3、通过sheet对象创建HSSFROW对象(row行对象)
4、通过HSSFROW对象创建列cell并set值(列名)
controller 层 /**
* excel自定义导出
* @param hAqscTieupsummary
* @param request
* @param response
* @param dataGrid
* @param modelMap
* @return
*/
@SuppressWarnings("deprecation")
@RequestMapping(params = "exportEXL")
public String exportEXL(HAqscTieupsummaryEntity hAqscTieupsummary,
HttpServletRequest request, HttpServletResponse response,
DataGrid dataGrid, ModelMap modelMap) {
try {
String dateType = "yyyy";
SimpleDateFormat df = new SimpleDateFormat(dateType);// 设置日期格式
SimpleDateFormat df1 = new SimpleDateFormat("yyyy.MM.dd");// 设置日期格式
// 创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = new HSSFWorkbook();
HSSFRow row = null;
HSSFCell cell = null;
// 建立新的sheet对象(excel的表单) 并设置sheet名字
HSSFSheet sheet = wb.createSheet("占压管线台账信息");
sheet.setDefaultRowHeightInPoints(30);// 设置缺省列高sheet.setDefaultColumnWidth(20);//设置缺省列宽
//----------------标题样式---------------------
HSSFCellStyle titleStyle = wb.createCellStyle(); //标题样式
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
Font ztFont = wb.createFont();
ztFont.setItalic(false); // 设置字体为斜体字
ztFont.setColor(Font.COLOR_NORMAL); // 将字体设置为“红色”
ztFont.setFontHeightInPoints((short)16); // 将字体大小设置为18px
ztFont.setFontName("宋体"); // 将“宋体”字体应用到当前单元格上
ztFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗
// ztFont.setUnderline(Font.U_DOUBLE); // 添加(Font.U_SINGLE单条下划线/Font.U_DOUBLE双条下划线)
// ztFont.setStrikeout(true); // 是否添加删除线
titleStyle.setFont(ztFont);
//-------------------------------------------
//----------------二级标题格样式----------------------------------
HSSFCellStyle titleStyle2 = wb.createCellStyle(); //表格样式
titleStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
Font ztFont2 = wb.createFont();
ztFont2.setItalic(false); // 设置字体为斜体字
ztFont2.setColor(Font.COLOR_NORMAL); // 将字体设置为“红色”
ztFont2.setFontHeightInPoints((short)11); // 将字体大小设置为18px
ztFont2.setFontName("宋体"); // 字体应用到当前单元格上
ztFont2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗
// ztFont.setUnderline(Font.U_DOUBLE); // 添加(Font.U_SINGLE单条下划线/Font.U_DOUBLE双条下划线)
// ztFont.setStrikeout(true); // 是否添加删除线
titleStyle2.setFont(ztFont2);
//----------------------------------------------------------
//----------------单元格样式----------------------------------
HSSFCellStyle cellStyle = wb.createCellStyle(); //表格样式
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
Font cellFont = wb.createFont();
cellFont.setItalic(false); // 设置字体为斜体字
cellFont.setColor(Font.COLOR_NORMAL); // 将字体设置为“红色”
cellFont.setFontHeightInPoints((short)10); // 将字体大小设置为18px
cellFont.setFontName("宋体"); // 字体应用到当前单元格上
// cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(cellFont);
cellStyle.setWrapText(true);//设置自动换行
//----------------------------------------------------------
// ----------------------创建第一行---------------
// 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
row = sheet.createRow(0);
// 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
cell = row.createCell(0);
// 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 19));
// 设置单元格内容
cell.setCellValue("占压城市地下管线、输油气管道、化工产品管道违法违规建设汇总表");
cell.setCellStyle(titleStyle);
// ---------------------------------------------- // ------------------创建第二行(单位、填表日期)---------------------
row = sheet.createRow(1); // 创建第二行
cell = row.createCell(0);
cell.setCellValue("填报单位名称(盖章): ");
cell.setCellStyle(titleStyle2);
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 3));
cell = row.createCell(4);
sheet.addMergedRegion(new CellRangeAddress(1, 1, 4, 5));
TSBaseUser tb = ResourceUtil.getSessionUserName(); //获取当前登录用户信息
String uid = tb.getId();
String deptId = userDao.getDeptId(uid);
String deptName = userDao.getDeptName(deptId);
cell.setCellValue(deptName);
// cell.setCellValue("*****");
cell.setCellStyle(titleStyle2);
cell = row.createCell(13); // 填表时间
sheet.addMergedRegion(new CellRangeAddress(1, 1, 13, 16));
cell.setCellValue("填表时间:"+df1.format(new Date()));
cell.setCellStyle(titleStyle2);
// HSSFCell cell14 = row.createCell(15); // 填表时间
// cell14.setCellValue();
// cell14.setCellValue("2017.11.30");
// cell14.setCellStyle(titleStyle2);
// ---------------------------------------------- // ------------------创建表头start---------------------
row = sheet.createRow(2); // 创建第三行
sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 0));
cell = row.createCell(0);
cell.setCellValue("序号");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 1, 1));
cell = row.createCell(1);
cell.setCellValue("隐患等级评定");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 2, 2));
cell = row.createCell(2);
cell.setCellValue("隐患名称");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 2, 3, 5));
cell = row.createCell(3);
cell.setCellValue("位置描述");
cell.setCellStyle(cellStyle); cell = row.createCell(4);
cell.setCellStyle(cellStyle);
cell = row.createCell(5);
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 2, 6, 10));
cell = row.createCell(6);
cell.setCellValue("管线情况");
cell.setCellStyle(cellStyle); cell = row.createCell(7);
cell.setCellStyle(cellStyle);
cell = row.createCell(8);
cell.setCellStyle(cellStyle);
cell = row.createCell(9);
cell.setCellStyle(cellStyle);
cell = row.createCell(10);
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 2, 11, 13));
cell = row.createCell(11);
cell.setCellValue("占压物情况");
cell.setCellStyle(cellStyle); cell = row.createCell(12);
cell.setCellStyle(cellStyle);
cell = row.createCell(13);
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 2, 14, 14));
cell = row.createCell(14);
cell.setCellValue("占压物用途");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 15, 15));
cell = row.createCell(15);
cell.setCellValue("已采用的安全防护措施");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 16, 16));
cell = row.createCell(16);
cell.setCellValue("备注");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 17, 17));
cell = row.createCell(17);
cell.setCellValue("联系人电话");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 18, 18));
cell = row.createCell(18);
cell.setCellValue("是否已和区管委和供热办联系");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(2, 3, 19, 19));
cell = row.createCell(19);
cell.setCellValue("是否采取防范措施");
cell.setCellStyle(cellStyle); //--------------------------- 创建第四行--------------------
row = sheet.createRow(3);
sheet.addMergedRegion(new CellRangeAddress(3, 3, 3, 3));
cell = row.createCell(3);
cell.setCellValue("所在区县");
cell.setCellStyle(cellStyle); cell = row.createCell(0);
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 4, 4));
cell = row.createCell(4);
cell.setCellValue("所在街道");
cell.setCellStyle(cellStyle); cell = row.createCell(1);
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 5, 5));
cell = row.createCell(5);
cell.setCellValue("详细地址");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 6, 6));
cell = row.createCell(6);
cell.setCellValue("管线建成时间");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 7, 7));
cell = row.createCell(7);
cell.setCellValue("管线埋深");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 8, 8));
cell = row.createCell(8);
cell.setCellValue("管径");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 9, 9));
cell = row.createCell(9);
cell.setCellValue("管线压力等级");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 10, 10));
cell = row.createCell(10);
cell.setCellValue("占压管线长度");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 11, 11));
cell = row.createCell(11);
cell.setCellValue("占压单位(个人)名称");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 12, 12));
cell = row.createCell(12);
cell.setCellValue("占压物建成时间");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 13, 13));
cell = row.createCell(13);
cell.setCellValue("占压物面积(平方米)");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 14, 14));
cell = row.createCell(14);
cell.setCellValue("经营、出租、自用、居住");
cell.setCellStyle(cellStyle); cell = row.createCell(15);
cell.setCellStyle(cellStyle);
cell = row.createCell(16);
cell.setCellStyle(cellStyle);
cell = row.createCell(17);
cell.setCellStyle(cellStyle);
cell = row.createCell(18);
cell.setCellStyle(cellStyle);
cell = row.createCell(19);
cell.setCellStyle(cellStyle);
//-------------------------表头end---------------------
CriteriaQuery cq = new CriteriaQuery(HAqscTieupsummaryEntity.class,
dataGrid);
org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq,
hAqscTieupsummary, request.getParameterMap());
List<HAqscTieupsummaryEntity> hAqscTieupsummarys = this.hAqscTieupsummaryService
.getListByCriteriaQuery(cq, false);
for (int i = 0; i < hAqscTieupsummarys.size(); i++) { //向表格插入数据
List<Object> data = new ArrayList<>(); //将前台传来的数据存入到list中
// System.out.println(hAqscTieupsummarys.get(i).getSeqNum());
HAqscTieupsummaryEntity entity = hAqscTieupsummarys.get(i);
data.add(entity.getSeqNum());
String yhjb = entity.getYhDjpd();
String hyjb = dao.getHyjb(yhjb);
data.add(hyjb); //隐患级别
data.add(entity.getYhName());
String countryName = dao.getCountryByCode(entity.getAtcounty()); //区县
data.add(countryName);
String code = entity.getAtdistrict();
String streetName = dao.getStreetByCode(code); //街道
data.add(streetName);
data.add(entity.getAddress());
Date buildtime = entity.getPipelineBuildtime();
if (buildtime!=null) {
String format = df.format(buildtime);
data.add(format);
}else{
data.add("");
}
data.add(entity.getPipelineDepth());
data.add(entity.getPipeSize());
data.add(entity.getPipelinePr());
data.add(entity.getTppipelineLength());
data.add(entity.getTieupName());
Date goodsBuildtime = entity.getTieupgoodsBuildtime();
if (buildtime!=null) {
String format = df.format(goodsBuildtime);
data.add(format);
}else{
data.add("");
}
data.add(entity.getTieupgoodsArea());
String useType = entity.getTieupgoodsUse();
data.add(dao.getUseType(useType));
data.add(entity.getUseSecuritymeasures());
data.add(entity.getRemark());
data.add(entity.getTelephone());
data.add(dao.getIsContact(entity.getIsContact()));
data.add(entity.getIsUsesecuritymeasures());
int rowNum = 4+i; //从第四行开始
row = sheet.createRow(rowNum);
for (int j = 0; j < data.size(); j++) { //将数据添加到单元格中
// System.out.println(data.get(j));
sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, j, j));
cell = row.createCell(j);
cell.setCellValue(""+data.get(j)+"");
cell.setCellStyle(cellStyle);
}
} // 输出Excel文件
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition",
"attachment; filename=details.xls"); //filename = 文件名
response.setContentType("application/msexcel");
wb.write(output);
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
前台请求地址:
<t:dgToolBar title="导出" icon="icon-putout" funname="ExportXls"></t:dgToolBar>
//导出
function ExportXls() {
JeecgExcelExport("hAqscTieupsummaryController.do?exportEXL","hAqscTieupsummaryList");
}
excel导出模版如下:

java使用poi自定义excel标题头并导出(springmvc+poi)的更多相关文章
- java使用POI操作excel文件,实现批量导出,和导入
一.POI的定义 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作Exc ...
- 使用JDBC+POI把Excel中的数据导出到MySQL
POI是Apache的一套读MS文档的API,用它还是可以比较方便的读取Office文档的.目前支持Word,Excel,PowerPoint生成的文档,还有Visio和Publisher的. htt ...
- java使用jxl,poi解析excel文件
public interface JavaExcel { /** * 使用jxl写excel文件 */ public void writeJxlExcel(); /** * 使用jxl读excel文件 ...
- 【原创】POI操作Excel导入导出工具类ExcelUtil
关于本类线程安全性的解释: 多数工具方法不涉及共享变量问题,至于添加合并单元格方法addMergeArea,使用ThreadLocal变量存储合并数据,ThreadLocal内部借用Thread.Th ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- 自己的包poi操作Excel工具
在前面的文章<使用poi读写Excel>中分享了一下poi操作Excel的简单演示样例.这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完毕的功能是:读取Excel.汇总E ...
- java通过poi读取excel中的日期类型数据或自定义类型日期
Java 读取Excel表格日期类型数据的时候,读出来的是这样的 12-十月-2019,而Excel中输入的是 2019/10/12 或 2019-10-12 poi处理excel时,当excel没 ...
- JAVA POI替换EXCEL模板中自定义标签(XLSX版本)满足替换多个SHEET中自定义标签
个人说明:为了简单实现导出数据较少的EXCEL(根据自定义书签模板) 一.替换Excel表格标签方法```/** * 替换Excel模板文件内容 * @param map * 需要替换的标签建筑队形式 ...
- 10、借助POI实现Java生成并打印excel报表(1)
10.1.了解 Apache POI 实际开发中,用到最多的是把数据库中数据导出生成报表,尤其是在生产管理或者财务系统中用的非常普遍.生成报表格式一般是EXCEL或者PDF .利用Apache PO ...
随机推荐
- 以通配符(%)开始的like字符串,走索引
在对oracle的SQL优化过程中经常会遇到[like'%abc']破坏索引的问题,但是如果真有此类需求,该如何在不破坏索引的基础上进行查询呢. [sql] view plain copy sys@m ...
- JMeter接口返回数组键值对校验方法
$.data.tourRecommend[?(@.title=="产品特色")].type
- 使用VSCode创建Asp.Net Core
前言 .Net Core 2.0已经发布几个月了,惭愧!身为一个开发人员现在才开始接触,有人说有VS这一宇宙第一IDE在,为啥还要用VSCode,为啥?因为我们是程序猿啊!我们是攻城狮啊!我们爱折腾啊 ...
- Mockplus设计大赛获奖选手专访 | High音:轻松生活,随心嗨音
"看似低调,实则高调的设计,UI设计是用了功力,主页功能和内容一览无余,方便用户选择,金字黑底,给予用户极好的奢华体验.原来听歌也是一种视觉享受.创新性源于对听歌氛围的把握,大幅的图片,刺激 ...
- Clonezilla SE---克隆linux------转载
引入: 本博文将会是<学生机房中的虚拟化>专题中的核心内容.因为,通过本篇博文的讲述,大家可以看到用于网络化批量部署Linux系统的Clonezilla SE搭建的全过程.注意,几乎所有命 ...
- php使用websocket示例详解
一.php 中处理 websocket WebSocket 连接是由客户端主动发起的,所以一切要从客户端出发.第一步是要解析拿到客户端发过来的 Sec-WebSocket-Key 字符串. 复制代码代 ...
- K:线性表
1. 线性表在计算机中可以用顺序存储和链式存储两种存储结构来表示.其中用顺序存储结构表示的线性表成为顺序表,用链式存储结构表示的线性表称为链表,链表又有单链表,双向链表,循环链表之分. 2. 线性表是 ...
- 房上的猫:if选择结构
一.基本if结构: 1.定义:if选择结构是根据条件判断之后再做处理的一种语法结构! 2.逻辑:首先对条件进行判断 >如果为真,则执行代码块 >如果为假,执行代码块后面的部分二.常用逻 ...
- ORA-00600[17059]错误
ORA-00600[17059] ORA-00600[17059]错误大部分都是因为高版本导致,对于本库的分析:因为该库的 shared pool老化比较频繁,到我介入的时候,发现相关该类此sql已经 ...
- Life in Changsha 第二次scrum冲刺
第二次冲刺 第二次冲刺任务 设计留言板功能. 用户故事 用户打开“生活在长大”的界面 程序首页展示校园服务,论坛等相关信息 用户选择留言板 程序界面跳转 用户查看留言,并可以输入留言内容 提交后留 ...