HSSFWorkBooK用法 —Excel表的导出和设置
HSSFWorkBooK用法 —Excel表的导出和设置
- public ActionResult excelPrint() {
- HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件
- HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet
- sheet.createFreezePane(1, 3);// 冻结
- // 设置列宽
- sheet.setColumnWidth(0, 1000);
- sheet.setColumnWidth(1, 3500);
- sheet.setColumnWidth(2, 3500);
- sheet.setColumnWidth(3, 6500);
- sheet.setColumnWidth(4, 6500);
- sheet.setColumnWidth(5, 6500);
- sheet.setColumnWidth(6, 6500);
- sheet.setColumnWidth(7, 2500);
- // Sheet样式
- HSSFCellStyle sheetStyle = workbook.createCellStyle();
- // 背景色的设定
- sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
- // 前景色的设定
- sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
- // 填充模式
- sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
- // 设置列的样式
- for (int i = 0; i <= 14; i++) {
- sheet.setDefaultColumnStyle((short) i, sheetStyle);
- }
- // 设置字体
- HSSFFont headfont = workbook.createFont();
- headfont.setFontName("黑体");
- headfont.setFontHeightInPoints((short) 22);// 字体大小
- headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
- // 另一个样式
- HSSFCellStyle headstyle = workbook.createCellStyle();
- headstyle.setFont(headfont);
- headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
- headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
- headstyle.setLocked(true);
- headstyle.setWrapText(true);// 自动换行
- // 另一个字体样式
- HSSFFont columnHeadFont = workbook.createFont();
- columnHeadFont.setFontName("宋体");
- columnHeadFont.setFontHeightInPoints((short) 10);
- columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- // 列头的样式
- HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
- columnHeadStyle.setFont(columnHeadFont);
- columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
- columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
- columnHeadStyle.setLocked(true);
- columnHeadStyle.setWrapText(true);
- columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
- columnHeadStyle.setBorderLeft((short) 1);// 边框的大小
- columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
- columnHeadStyle.setBorderRight((short) 1);// 边框的大小
- columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
- columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色
- // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
- columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
- HSSFFont font = workbook.createFont();
- font.setFontName("宋体");
- font.setFontHeightInPoints((short) 10);
- // 普通单元格样式
- HSSFCellStyle style = workbook.createCellStyle();
- style.setFont(font);
- style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
- style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
- style.setWrapText(true);
- style.setLeftBorderColor(HSSFColor.BLACK.index);
- style.setBorderLeft((short) 1);
- style.setRightBorderColor(HSSFColor.BLACK.index);
- style.setBorderRight((short) 1);
- style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
- style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
- style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
- // 另一个样式
- HSSFCellStyle centerstyle = workbook.createCellStyle();
- centerstyle.setFont(font);
- centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
- centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
- centerstyle.setWrapText(true);
- centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);
- centerstyle.setBorderLeft((short) 1);
- centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
- centerstyle.setBorderRight((short) 1);
- centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
- centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
- centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
- try {
- // 创建第一行
- HSSFRow row0 = sheet.createRow(0);
- // 设置行高
- row0.setHeight((short) 900);
- // 创建第一列
- HSSFCell cell0 = row0.createCell(0);
- cell0.setCellValue(new HSSFRichTextString("中非发展基金投资项目调度会工作落实情况对照表"));
- cell0.setCellStyle(headstyle);
- /**
- * 合并单元格
- * 第一个参数:第一个单元格的行数(从0开始)
- * 第二个参数:第二个单元格的行数(从0开始)
- * 第三个参数:第一个单元格的列数(从0开始)
- * 第四个参数:第二个单元格的列数(从0开始)
- */
- CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7);
- sheet.addMergedRegion(range);
- // 创建第二行
- HSSFRow row1 = sheet.createRow(1);
- HSSFCell cell1 = row1.createCell(0);
- cell1.setCellValue(new HSSFRichTextString("本次会议时间:2009年8月31日 前次会议时间:2009年8月24日"));
- cell1.setCellStyle(centerstyle);
- // 合并单元格
- range = new CellRangeAddress(1, 2, 0, 7);
- sheet.addMergedRegion(range);
- // 第三行
- HSSFRow row2 = sheet.createRow(3);
- row2.setHeight((short) 750);
- HSSFCell cell = row2.createCell(0);
- cell.setCellValue(new HSSFRichTextString("责任者"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(1);
- cell.setCellValue(new HSSFRichTextString("成熟度排序"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(2);
- cell.setCellValue(new HSSFRichTextString("事项"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(3);
- cell.setCellValue(new HSSFRichTextString("前次会议要求/n/新项目的项目概要"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(4);
- cell.setCellValue(new HSSFRichTextString("上周工作进展"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(5);
- cell.setCellValue(new HSSFRichTextString("本周工作计划"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(6);
- cell.setCellValue(new HSSFRichTextString("问题和建议"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(7);
- cell.setCellValue(new HSSFRichTextString("备 注"));
- cell.setCellStyle(columnHeadStyle);
- // 访问数据库,得到数据集
- List<DeitelVO> deitelVOList = getEntityManager().queryDeitelVOList();
- int m = 4;
- int k = 4;
- for (int i = 0; i < deitelVOList.size(); i++) {
- DeitelVO vo = deitelVOList.get(i);
- String dname = vo.getDname();
- List<Workinfo> workList = vo.getWorkInfoList();
- HSSFRow row = sheet.createRow(m);
- cell = row.createCell(0);
- cell.setCellValue(new HSSFRichTextString(dname));
- cell.setCellStyle(centerstyle);
- // 合并单元格
- range = new CellRangeAddress(m, m + workList.size() - 1, 0, 0);
- sheet.addMergedRegion(range);
- m = m + workList.size();
- for (int j = 0; j < workList.size(); j++) {
- Workinfo w = workList.get(j);
- // 遍历数据集创建Excel的行
- row = sheet.getRow(k + j);
- if (null == row) {
- row = sheet.createRow(k + j);
- }
- cell = row.createCell(1);
- cell.setCellValue(w.getWnumber());
- cell.setCellStyle(centerstyle);
- cell = row.createCell(2);
- cell.setCellValue(new HSSFRichTextString(w.getWitem()));
- cell.setCellStyle(style);
- cell = row.createCell(3);
- cell.setCellValue(new HSSFRichTextString(w.getWmeting()));
- cell.setCellStyle(style);
- cell = row.createCell(4);
- cell.setCellValue(new HSSFRichTextString(w.getWbweek()));
- cell.setCellStyle(style);
- cell = row.createCell(5);
- cell.setCellValue(new HSSFRichTextString(w.getWtweek()));
- cell.setCellStyle(style);
- cell = row.createCell(6);
- cell.setCellValue(new HSSFRichTextString(w.getWproblem()));
- cell.setCellStyle(style);
- cell = row.createCell(7);
- cell.setCellValue(new HSSFRichTextString(w.getWremark()));
- cell.setCellStyle(style);
- }
- k = k + workList.size();
- }
- // 列尾
- int footRownumber = sheet.getLastRowNum();
- HSSFRow footRow = sheet.createRow(footRownumber + 1);
- HSSFCell footRowcell = footRow.createCell(0);
- footRowcell.setCellValue(new HSSFRichTextString(" 审 定:XXX 审 核:XXX 汇 总:XX"));
- footRowcell.setCellStyle(centerstyle);
- range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0, 7);
- sheet.addMergedRegion(range);
- HttpServletResponse response = getResponse();
- HttpServletRequest request = getRequest();
- String filename = "未命名.xls";//设置下载时客户端Excel的名称
- // 请见:http://zmx.javaeye.com/blog/622529
- filename = Util.encodeFilename(filename, request);
- response.setContentType("application/vnd.ms-excel");
- response.setHeader("Content-disposition", "attachment;filename=" + filename);
- OutputStream ouputStream = response.getOutputStream();
- workbook.write(ouputStream);
- ouputStream.flush();
- ouputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
HSSFWorkBooK用法 —Excel表的导出和设置的更多相关文章
- HSSFWorkBooK用法 ---Excel表的导出和设置
public ActionResult excelPrint() { HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件 HSSFShe ...
- JAVA实现创建Excel表并导出(转发)
<span style="font-family:Verdana, Arial, Helvetica, sans-serif;line-height:25.2px;background ...
- 如果从excel表中导出insert-sql
=CONCATENATE("INSERT INTO p_act_lottery(actId,status,grantWay,createTime,invalidTime,amount,pri ...
- 【ITOO 1】将List数据导出Excel表
需求描述:在课表导入的时候,首先给用户提供模板(excel),然后将用户填写好的数据读取到list集合中.再进行判空处赋值处理,以及去重处理.这篇博客,主要介绍读取excel表和导出excel表的方法 ...
- SSM中使用POI实现excel的导入导出
环境:导入POI对应的包 环境: Spring+SpringMVC+Mybatis POI对应的包 <dependency> <groupId>org.apache.poi&l ...
- ASP.NET导出excel表方法汇总
asp.net里导出excel表方法汇总 1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) ...
- asp.net使用MVC4框架基于NPOI做导出数据到Excel表
NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...
- java中的数据导出到Excel表中
整个项目中导出数据到.Excel的源码 import java.io.BufferedOutputStream; import java.io.FileInputStream; import java ...
- JAVA笔记-如何将百万级数据高效的导出到Excel表单
今天,一朋友问我使用JAVA有没有什么办法导出百万级的数据到Excel工作表. 当时我的第一个念头就是这真的是一个好疯狂的念头.然后就想假如真的有这样类似的需求,我自己应该怎么做呢? ps: 首先科普 ...
随机推荐
- linux下安装微信小程序开发工具
一.环境:: ubuntu 16.04 二.安装过程: 2.1 安装wine sudo apt-get install wine 2.2 安装nwjs-sdk 2.2.1 下载linux版nwjs-s ...
- Linux上Java的安装与配置
由于使用 yum 或者 apt-get 命令 安装 openjdk 可能存在类库不全,从而导致用户在安装后运行相关工具时可能报错的问题,所以此处我们推荐采用手动解压安装的方式来安装 JDK.具体步骤如 ...
- Nexus Repository 搭建及使用
Nexus Repository 是搭建maven的镜像的工具之一,在全球范围内使用挺广的. 一.Nexus 搭建过程 Nexus 镜像的搭建还是相对简单的,将下载的文件解压到相应的目录下,然后进入. ...
- HBase底层存储原理——我靠,和cassandra本质上没有区别啊!都是kv 列存储,只是一个是p2p另一个是集中式而已!
理解HBase(一个开源的Google的BigTable实际应用)最大的困难是HBase的数据结构概念究竟是什么?首先HBase不同于一般的关系数据库,它是一个适合于非结构化数据存储的数据库.另一个不 ...
- HDU 6092 Rikka with Subset(dp)
http://acm.hdu.edu.cn/showproblem.php?pid=6092 题意: 给出两个数组A和B,A数组一共可以有(1<<n)种不同的集合组合,B中则记录了每个数出 ...
- 关于ES6的箭头函数的详解
ok 坑比函数~~箭头函数~~不自己动手写看懂也不行~~~ 当然你也可以一点一点的把函数复制到Babel里面去将ES6转换成ES5 (斗笔行为) 老谢写的笔记教程就是深入(通俗易懂)哈哈~~~ 第 ...
- python 判断一个数字是否为4的幂
def is_Power_of_four(n): while n and not (n & 0b11): n >>= ) print(is_Power_of_four()) pri ...
- 批量删除Redis数据库中的Key
批量删除KeyRedis 中有删除单个 Key 的指令 DEL,但好像没有批量删除 Key 的指令,不过我们可以借助 Linux 的 xargs 指令来完成这个动作 redis-cli keys &q ...
- OpenGL入门程序五:三维绘制
1.现实世界观察一个物体的时候,可能涉及到的三维变化: 1>视图变化------从不同的角度观察. 2>模型变化------移动.旋转物体,计算机中当然还可以对物体进行缩放. 3>投 ...
- OpenGL入门程序二:绘制简单的圆
学习 绘制一个圆: ; const float Pi = 3.1415926536f; const float R = 0.5f; //绘制一个圆 void DrawCircle() { //绘制一个 ...