1 public ActionResult excelPrint() {
2 HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件
3 HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet
4 sheet.createFreezePane(1, 3);// 冻结
5 // 设置列宽
6 sheet.setColumnWidth(0, 1000);
7 sheet.setColumnWidth(1, 3500);
8 sheet.setColumnWidth(2, 3500);
9 sheet.setColumnWidth(3, 6500);
10 sheet.setColumnWidth(4, 6500);
11 sheet.setColumnWidth(5, 6500);
12 sheet.setColumnWidth(6, 6500);
13 sheet.setColumnWidth(7, 2500);
14 // Sheet样式
15 HSSFCellStyle sheetStyle = workbook.createCellStyle();
16 // 背景色的设定
17 sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
18 // 前景色的设定
19 sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
20 // 填充模式
21 sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
22 // 设置列的样式
23 for (int i = 0; i <= 14; i++) {
24 sheet.setDefaultColumnStyle((short) i, sheetStyle);
25 }
26 // 设置字体
27 HSSFFont headfont = workbook.createFont();
28 headfont.setFontName("黑体");
29 headfont.setFontHeightInPoints((short) 22);// 字体大小
30 headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
31 // 另一个样式
32 HSSFCellStyle headstyle = workbook.createCellStyle();
33 headstyle.setFont(headfont);
34 headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
35 headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
36 headstyle.setLocked(true);
37 headstyle.setWrapText(true);// 自动换行
38 // 另一个字体样式
39 HSSFFont columnHeadFont = workbook.createFont();
40 columnHeadFont.setFontName("宋体");
41 columnHeadFont.setFontHeightInPoints((short) 10);
42 columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
43 // 列头的样式
44 HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
45 columnHeadStyle.setFont(columnHeadFont);
46 columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
47 columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
48 columnHeadStyle.setLocked(true);
49 columnHeadStyle.setWrapText(true);
50 columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
51 columnHeadStyle.setBorderLeft((short) 1);// 边框的大小
52 columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
53 columnHeadStyle.setBorderRight((short) 1);// 边框的大小
54 columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
55 columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色
56 // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
57 columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
58
59 HSSFFont font = workbook.createFont();
60 font.setFontName("宋体");
61 font.setFontHeightInPoints((short) 10);
62 // 普通单元格样式
63 HSSFCellStyle style = workbook.createCellStyle();
64 style.setFont(font);
65 style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
66 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
67 style.setWrapText(true);
68 style.setLeftBorderColor(HSSFColor.BLACK.index);
69 style.setBorderLeft((short) 1);
70 style.setRightBorderColor(HSSFColor.BLACK.index);
71 style.setBorderRight((short) 1);
72 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
73 style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
74 style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
75 // 另一个样式
76 HSSFCellStyle centerstyle = workbook.createCellStyle();
77 centerstyle.setFont(font);
78 centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
79 centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
80 centerstyle.setWrapText(true);
81 centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);
82 centerstyle.setBorderLeft((short) 1);
83 centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
84 centerstyle.setBorderRight((short) 1);
85 centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
86 centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
87 centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
88
89 try {
90 // 创建第一行
91 HSSFRow row0 = sheet.createRow(0);
92 // 设置行高
93 row0.setHeight((short) 900);
94 // 创建第一列
95 HSSFCell cell0 = row0.createCell(0);
96 cell0.setCellValue(new HSSFRichTextString("中非发展基金投资项目调度会工作落实情况对照表"));
97 cell0.setCellStyle(headstyle);
98 /**
99 * 合并单元格
100 * 第一个参数:第一个单元格的行数(从0开始)
101 * 第二个参数:第二个单元格的行数(从0开始)
102 * 第三个参数:第一个单元格的列数(从0开始)
103 * 第四个参数:第二个单元格的列数(从0开始)
104 */
105 CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7);
106 sheet.addMergedRegion(range);
107 // 创建第二行
108 HSSFRow row1 = sheet.createRow(1);
109 HSSFCell cell1 = row1.createCell(0);
110 cell1.setCellValue(new HSSFRichTextString("本次会议时间:2009年8月31日 前次会议时间:2009年8月24日"));
111 cell1.setCellStyle(centerstyle);
112 // 合并单元格
113 range = new CellRangeAddress(1, 2, 0, 7);
114 sheet.addMergedRegion(range);
115 // 第三行
116 HSSFRow row2 = sheet.createRow(3);
117 row2.setHeight((short) 750);
118 HSSFCell cell = row2.createCell(0);
119 cell.setCellValue(new HSSFRichTextString("责任者"));
120 cell.setCellStyle(columnHeadStyle);
121 cell = row2.createCell(1);
122 cell.setCellValue(new HSSFRichTextString("成熟度排序"));
123 cell.setCellStyle(columnHeadStyle);
124 cell = row2.createCell(2);
125 cell.setCellValue(new HSSFRichTextString("事项"));
126 cell.setCellStyle(columnHeadStyle);
127 cell = row2.createCell(3);
128 cell.setCellValue(new HSSFRichTextString("前次会议要求/n/新项目的项目概要"));
129 cell.setCellStyle(columnHeadStyle);
130 cell = row2.createCell(4);
131 cell.setCellValue(new HSSFRichTextString("上周工作进展"));
132 cell.setCellStyle(columnHeadStyle);
133 cell = row2.createCell(5);
134 cell.setCellValue(new HSSFRichTextString("本周工作计划"));
135 cell.setCellStyle(columnHeadStyle);
136 cell = row2.createCell(6);
137 cell.setCellValue(new HSSFRichTextString("问题和建议"));
138 cell.setCellStyle(columnHeadStyle);
139 cell = row2.createCell(7);
140 cell.setCellValue(new HSSFRichTextString("备 注"));
141 cell.setCellStyle(columnHeadStyle);
142 // 访问数据库,得到数据集
143 List<DeitelVO> deitelVOList = getEntityManager().queryDeitelVOList();
144 int m = 4;
145 int k = 4;
146 for (int i = 0; i < deitelVOList.size(); i++) {
147 DeitelVO vo = deitelVOList.get(i);
148 String dname = vo.getDname();
149 List<Workinfo> workList = vo.getWorkInfoList();
150 HSSFRow row = sheet.createRow(m);
151 cell = row.createCell(0);
152 cell.setCellValue(new HSSFRichTextString(dname));
153 cell.setCellStyle(centerstyle);
154 // 合并单元格
155 range = new CellRangeAddress(m, m + workList.size() - 1, 0, 0);
156 sheet.addMergedRegion(range);
157 m = m + workList.size();
158
159 for (int j = 0; j < workList.size(); j++) {
160 Workinfo w = workList.get(j);
161 // 遍历数据集创建Excel的行
162 row = sheet.getRow(k + j);
163 if (null == row) {
164 row = sheet.createRow(k + j);
165 }
166 cell = row.createCell(1);
167 cell.setCellValue(w.getWnumber());
168 cell.setCellStyle(centerstyle);
169 cell = row.createCell(2);
170 cell.setCellValue(new HSSFRichTextString(w.getWitem()));
171 cell.setCellStyle(style);
172 cell = row.createCell(3);
173 cell.setCellValue(new HSSFRichTextString(w.getWmeting()));
174 cell.setCellStyle(style);
175 cell = row.createCell(4);
176 cell.setCellValue(new HSSFRichTextString(w.getWbweek()));
177 cell.setCellStyle(style);
178 cell = row.createCell(5);
179 cell.setCellValue(new HSSFRichTextString(w.getWtweek()));
180 cell.setCellStyle(style);
181 cell = row.createCell(6);
182 cell.setCellValue(new HSSFRichTextString(w.getWproblem()));
183 cell.setCellStyle(style);
184 cell = row.createCell(7);
185 cell.setCellValue(new HSSFRichTextString(w.getWremark()));
186 cell.setCellStyle(style);
187 }
188 k = k + workList.size();
189 }
190 // 列尾
191 int footRownumber = sheet.getLastRowNum();
192 HSSFRow footRow = sheet.createRow(footRownumber + 1);
193 HSSFCell footRowcell = footRow.createCell(0);
194 footRowcell.setCellValue(new HSSFRichTextString(" 审 定:XXX 审 核:XXX 汇 总:XX"));
195 footRowcell.setCellStyle(centerstyle);
196 range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0, 7);
197 sheet.addMergedRegion(range);
198
199 HttpServletResponse response = getResponse();
200 HttpServletRequest request = getRequest();
201 String filename = "未命名.xls";//设置下载时客户端Excel的名称
202 // 请见:http://zmx.javaeye.com/blog/622529
203 filename = Util.encodeFilename(filename, request);
204 response.setContentType("application/vnd.ms-excel");
205 response.setHeader("Content-disposition", "attachment;filename=" + filename);
206 OutputStream ouputStream = response.getOutputStream();
207 workbook.write(ouputStream);
208 ouputStream.flush();
209 ouputStream.close();
210
211 } catch (Exception e) {
212 e.printStackTrace();
213 }
214 return null;
215 }

HSSFWorkBooK用法 ---Excel表的导出和设置的更多相关文章

  1. HSSFWorkBooK用法 —Excel表的导出和设置

    HSSFWorkBooK用法 —Excel表的导出和设置 2013年02月21日 ⁄ 综合 ⁄ 共 9248字 ⁄ 字号 小 中 大 ⁄ 评论关闭 public ActionResult excelP ...

  2. JAVA实现创建Excel表并导出(转发)

    <span style="font-family:Verdana, Arial, Helvetica, sans-serif;line-height:25.2px;background ...

  3. 如果从excel表中导出insert-sql

    =CONCATENATE("INSERT INTO p_act_lottery(actId,status,grantWay,createTime,invalidTime,amount,pri ...

  4. 【ITOO 1】将List数据导出Excel表

    需求描述:在课表导入的时候,首先给用户提供模板(excel),然后将用户填写好的数据读取到list集合中.再进行判空处赋值处理,以及去重处理.这篇博客,主要介绍读取excel表和导出excel表的方法 ...

  5. SSM中使用POI实现excel的导入导出

    环境:导入POI对应的包 环境: Spring+SpringMVC+Mybatis POI对应的包 <dependency> <groupId>org.apache.poi&l ...

  6. ASP.NET导出excel表方法汇总

    asp.net里导出excel表方法汇总  1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) ...

  7. asp.net使用MVC4框架基于NPOI做导出数据到Excel表

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...

  8. java中的数据导出到Excel表中

    整个项目中导出数据到.Excel的源码 import java.io.BufferedOutputStream; import java.io.FileInputStream; import java ...

  9. JAVA笔记-如何将百万级数据高效的导出到Excel表单

    今天,一朋友问我使用JAVA有没有什么办法导出百万级的数据到Excel工作表. 当时我的第一个念头就是这真的是一个好疯狂的念头.然后就想假如真的有这样类似的需求,我自己应该怎么做呢? ps: 首先科普 ...

随机推荐

  1. arduino串口输出问题

  2. 【Codeforces Round #433 (Div. 2) B】Maxim Buys an Apartment

    [链接]h在这里写链接 [题意] 你有n个位置,然后其中有k个地方是已经被人占据了的. 一个"好的位置"的定义是指,这个位置相邻的地方其中至少有一个被人占据了. k个被人占据的位置 ...

  3. FTP、WEB虚拟目录作用

    随风原文FTP.WEB虚拟目录作用 在 IIS中,双击您要为之添加虚拟目录的服务以显示其属性表.    单击“目录”选项卡.    单击“添加”.    单击“浏览”从“目录”框中选择一个目录.    ...

  4. 1、Java快速入门

    第一课 Java基础1. ubuntu上环境搭建 (建议使用我们提供的VMWare映象文件) 如果要自己安装ubuntu, 请参考<韦东山Android系统视频使用手册.pdf> ubun ...

  5. 如何获取AppStore软件安装包的路径

    本帖最后由 chinald 于 2015-10-16 13:59 编辑 前言:本文介绍在Mac下如何找到AppStore下载的安装包路径,以及如何提取出来供以后使用,希望对大家有所帮助(前提:想要提取 ...

  6. 前端css常用的选择小汇

    要使用css对HTML页面中的元素实现一对一,一对多或者多对一的控制,这就需要用到CSS选择器.选择器就是选择器用来指定样式的作用范围. 类选择器: 类选择器在css中比较常见,首先要在普通标签中设置 ...

  7. 【微信小程序】自定义模态框实例

    原文链接:https://mp.weixin.qq.com/s/23wPVFUGY-lsTiQBtUdhXA 1 概述 由于官方API提供的显示模态弹窗,只能简单地显示文字内容,不能对对话框内容进行自 ...

  8. php实现从尾到头打印列表

    php实现从尾到头打印列表 一.总结 4.数组倒序:array_reverse() 5.函数肯定要return,而不是echo 二.php实现从尾到头打印列表 输入一个链表,从尾到头打印链表每个节点的 ...

  9. 我的嵌入式Qt开发第一课——基于BBB和hmc5843三轴电子罗盘

    几次想照着课本系统地学习Qt,但我发现还是有详细问题驱动时学习比較快. 于是我给自己设定了这个任务: 读取HMC5843的三轴磁场强度值,计算出角度,并把角度用直观形式显示在图形界面上. 这里面涉及到 ...

  10. python opencv3 —— findContours

    findContours 是 opencv 下的轮廓提取函数. 1. api 分析 findContours(image, mode, method[, contours[, hierarchy[, ...