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 ...
随机推荐
- springboot 项目maven 打包错误
Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:repackage ...
- 「mysql优化专题」这大概是一篇最好的mysql优化入门文章(1)
优化,一直是面试最常问的一个问题.因为从优化的角度,优化的思路,完全可以看出一个人的技术积累.那么,关于系统优化,假设这么个场景,用户反映系统太卡(其实就是高并发),那么我们怎么优化? 如果请求过多, ...
- iOS开发添加pch文件
首先说一下pch的作用: 1.存放一些全局的宏(整个项目中都用得上的宏) 2.用来包含一些全部的头文件(整个项目中都用得上的头文件) 3.能自动打开或者关闭日志输出功能 如何在Xcode中添加pch文 ...
- dubbo+zookeeper+jsp+springmvc+spring+mybatis+mysql+maven完整示例
项目分为三部分,这里分为三个maven项目(基于web,所以最后一个为maven创建的web项目) 1.接口定义以及实体类定义(api+pojo) --- maven创建java项目,打包成jar 2 ...
- Python 多线程进程高级指南(二)
本文是如何<优雅地实现Python通用多线程/进程并行模块>的后续.因为我发现,自认为懂了一点多线程开发的皮毛,写了那么个multi_helper的玩意儿,后来才发现我靠原来就是一坨屎.自 ...
- su 和 sudo 命令的区别-转载
link 一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要 ...
- selenium爬取百度图片
一:简介 通过selenium模块,模拟火狐浏览器进行搜索下载操作. 二:脚本内容 # -*- coding:utf-8 -*- # 百度图片自动爬去 # Chrome浏览器类似,设置其options ...
- Sublime Text 2 Plugin Installation
For Package Control installation, see the Installation Guide. To install Emmet(ex Zen Coding), do ...
- 发布 Google Chrome插件教程
换个视角,世界不一样.嘘~~~ 如果你会使用js的话,那么你就可以自己动手写一个chrome插件,而且非常容易.google是一个全球化的平台,想想自己的程序被世界人民所使用,是不是很激动? 注册开发 ...
- 自动化测试辅助工具(Selenium IDE等)
本随表目录 Selenium IDE安装和使用 FireBug安装和使用 FirePath安装和使用 Selenium IDE安装 方式一:打开Firefox-->添加组件-->搜索出 ...