导出带有多个标签页的Excel表格代码

来源:互联网 发布:防网络尖兵方法 编辑:程序博客网 时间:2024/06/06 17:24

控制层代码:

  1. @RequestMapping(value = "/getFile")
  2. public void getFile(String endTime, String beginTime, HttpServletResponse response) {
  3. try {
  4. //读取conf.properties文件
  5. ResourceBundle bundle = ResourceBundle.getBundle("conf");
  6. //读取conf.properties文件里的文件路径
  7. String pathStr = bundle.getString("file.fileRootPath");
  8. //设置相对路径加文件名
  9. String filename = pathStr + File.separator + "上课报名统计" + ".xls";
  10. //在内存中创建表格文件
  11. File fileExists = new File(filename);
  12. response.setContentType("application/octet-stream");
  13. //设置响应头,控制浏览器下载该文件
  14. response.addHeader("Content-Disposition", "attachment; filename=/" + new String(("上课报名统计数据信息.xsl").getBytes("GB2312"), "iso8859-1") + File.separator);
  15. //读取要下载的文件,保存到文件输入流
  16. FileInputStream in = new FileInputStream(fileExists);
  17. //创建输出流
  18. OutputStream out = response.getOutputStream();
  19. //创建缓冲区
  20. byte buffer[] = new byte[1024];
  21. int len = 0;
  22. //循环将输入流中的内容读取到缓冲区当中
  23. while ((len = in.read(buffer)) > 0) {
  24. //输出缓冲区的内容到浏览器,实现文件下载
  25. out.write(buffer, 0, len);
  26. }
  27. //关闭文件输入流
  28. in.close();
  29. //关闭输出流
  30. out.close();
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }

服务层代码(主要是查询装填表格使用的数据,创建出文件)

  1. @Override
  2. public void getList(String time) throws Exception {
  3. ResourceBundle bundle = ResourceBundle.getBundle("conf");//读取properties文件
  4. String pathStr = bundle.getString("file.fileRootPath");
  5. String filename = pathStr + File.separator + time + ".xls";
  6. File fileExists = new File(filename);
  7. //获取现在的时间
  8. String nowTime = DateUtil.formatDate(System.currentTimeMillis());
  9. //判断下载的是现在的时间等于指定的时间,就生成下载。
  10. if (time.equals(nowTime)) {
  11. File file = new File(filename);
  12. file.createNewFile();
  13. long beginTime = (Long) DateUtil.getStartAndEndTime(time).get("startTime");
  14. long endTime = (Long) DateUtil.getStartAndEndTime(time).get("endTime");
  15. Map map = new HashMap();
  16. //上课
  17. List<ClassBeginEntity> beginList = this.dataStaDao.getClassBeginList(beginTime, endTime);
  18. //推广
  19. List<StaRecordEntity> recordList = this.dataStaDao.getStaRecordList(beginTime, endTime);
  20. //报名
  21. List<EntryRecordEntity> entryList = this.dataStaDao.getEntryRecordList(beginTime, endTime);
  22. map.put("beginList", beginList);
  23. map.put("recordList", recordList);
  24. map.put("entryList", entryList);
  25. GeneratingExcelTable.export_table(map, time, file);
  26. }
  27. //如果没有就生成下载。
  28. if (!fileExists.exists()) {
  29. File file = new File(filename);
  30. file.createNewFile();
  31. long beginTime = (Long) DateUtil.getStartAndEndTime(time).get("startTime");
  32. long endTime = (Long) DateUtil.getStartAndEndTime(time).get("endTime");
  33. Map map = new HashMap();
  34. //上课
  35. List<ClassBeginEntity> beginList = this.dataStaDao.getClassBeginList(beginTime, endTime);
  36. //推广
  37. List<StaRecordEntity> recordList = this.dataStaDao.getStaRecordList(beginTime, endTime);
  38. //报名
  39. List<EntryRecordEntity> entryList = this.dataStaDao.getEntryRecordList(beginTime, endTime);
  40. map.put("beginList", beginList);
  41. map.put("recordList", recordList);
  42. map.put("entryList", entryList);
  43. GeneratingExcelTable.export_table(map, time, file);
  44. }
  45. }

表格填充数据的类(主要就是这):

  1. package com.sanhai.nep.managerService.util;
  2. import com.sanhai.nep.managerService.entity.ClassBeginEntity;
  3. import com.sanhai.nep.managerService.entity.EntryRecordEntity;
  4. import com.sanhai.nep.managerService.entity.StaRecordEntity;
  5. import jxl.Workbook;
  6. import jxl.format.Alignment;
  7. import jxl.write.*;
  8. import java.io.File;
  9. import java.util.*;
  10. /**
  11. * Created by 胥源博 on 2016/7/29.
  12. */
  13. public class GeneratingExcelTable {
  14. private static WritableCellFormat wcf_value; // 表格数据样式
  15. private static WritableCellFormat wcf_value_left;
  16. private static WritableCellFormat wcf_key; // 表头样式
  17. private static WritableCellFormat wcf_name_left; // 表名样式
  18. private static WritableCellFormat wcf_name_right; // 表名样式
  19. private static WritableCellFormat wcf_name_center; // 表名样式
  20. private static WritableCellFormat wcf_title; // 页名称样式
  21. private static WritableCellFormat wcf_percent_float;
  22. private static int maxLieShu = 12;
  23. /**
  24. * 向表格中填充准备好的数据信息
  25. * 在这里是创建一个表格但是有三个标签页
  26. *
  27. * @param map 准备好的数据:有三个list集合数据
  28. * @param time 时间 在这里当作文件名了
  29. * @param file 在内存中创建好的文件对象
  30. * @throws Exception
  31. */
  32. public static void export_table(Map map, String time, File file) throws Exception {
  33. // 生成Excel文件
  34. /****** 定义表格格式start *****/
  35. WritableFont wf_key = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.BOLD);
  36. WritableFont wf_value = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.NO_BOLD);
  37. //设置单元格样式
  38. wcf_value = new WritableCellFormat(wf_value); //单元格字体样式
  39. wcf_value.setAlignment(jxl.format.Alignment.CENTRE); //单元格水平对齐样式
  40. wcf_value.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //单元格垂直对齐样式
  41. wcf_value.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //单元格边框样式
  42. wcf_value_left = new WritableCellFormat(wf_value);
  43. wcf_value_left.setAlignment(jxl.format.Alignment.LEFT);
  44. wcf_value_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  45. wcf_value_left.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
  46. wcf_value_left.setWrap(true);
  47. wcf_key = new WritableCellFormat(wf_key);
  48. wcf_key.setAlignment(jxl.format.Alignment.CENTRE);
  49. wcf_key.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
  50. wcf_name_left = new WritableCellFormat(wf_key);
  51. wcf_name_left.setAlignment(Alignment.LEFT);
  52. wcf_name_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  53. wcf_name_right = new WritableCellFormat(wf_key);
  54. wcf_name_right.setAlignment(Alignment.LEFT);
  55. wcf_name_center = new WritableCellFormat(wf_key);
  56. wcf_name_center.setAlignment(Alignment.CENTRE);
  57. jxl.write.NumberFormat wf_percent_float = new jxl.write.NumberFormat("0.00"); //定义单元浮点数据类型
  58. wcf_percent_float = new jxl.write.WritableCellFormat(wf_value, wf_percent_float);
  59. wcf_percent_float.setAlignment(jxl.format.Alignment.CENTRE);
  60. wcf_percent_float.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  61. wcf_percent_float.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
  62. WritableFont wf_title = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 24, WritableFont.NO_BOLD); //定义标题样式
  63. wcf_title = new WritableCellFormat(wf_title);
  64. wcf_title.setAlignment(Alignment.CENTRE);
  65. wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
  66. /****** 定义表格格式end *****/
  67. //在指定的路径生成空白的xls文件
  68. List beginList = (List) map.get("beginList");
  69. List recordList = (List) map.get("recordList");
  70. List entryList = (List) map.get("entryList");
  71. Iterator beginList_it = beginList.iterator();
  72. Iterator recordList_it = recordList.iterator();
  73. Iterator entryList_it = entryList.iterator();
  74. //上课记录工作薄
  75. //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
  76. WritableWorkbook wb = Workbook.createWorkbook(file);
  77. //设置Excel工作簿名称
  78. WritableSheet ws = wb.createSheet("上课记录", 0);
  79. int startRowNum = 0; // 起始行
  80. int startColNum = 0; // 起始列
  81. int maxColSize = maxLieShu; // 最大列数
  82. // 设置列宽
  83. ws.setColumnView(0, 35);
  84. ws.setColumnView(1, 35);
  85. ws.setColumnView(2, 35);
  86. ws.setColumnView(3, 35);
  87. ws.setColumnView(4, 35);
  88. ws.setColumnView(5, 35);
  89. ws.setColumnView(6, 35);
  90. ws.setColumnView(7, 35);
  91. ws.setColumnView(8, 35);
  92. ws.setColumnView(9, 35);
  93. ws.setColumnView(10, 35);
  94. ws.setColumnView(11, 35);
  95. ws.setColumnView(12, 35);
  96. ws.addCell(new Label(startColNum, startRowNum, time + "数据信息", wcf_title));
  97. ws.mergeCells(startColNum, startRowNum, startColNum + maxColSize - 1, startRowNum); //合并单元格,合并(1,0)到(1,9)
  98. startColNum = 0;
  99. startRowNum++;
  100. //第1行,绘制表头
  101. ws.addCell(new Label(startColNum, startRowNum, "用户ID", wcf_key));
  102. startColNum++;
  103. ws.addCell(new Label(startColNum, startRowNum, "系列课程标题", wcf_key));
  104. startColNum++;
  105. ws.addCell(new Label(startColNum, startRowNum, "课程ID", wcf_key));
  106. startColNum++;
  107. ws.addCell(new Label(startColNum, startRowNum, "用户角色(0:老师,10:一对一学生,20:旁听学生,30:试听学生)", wcf_key));
  108. startColNum++;
  109. ws.addCell(new Label(startColNum, startRowNum, "用户名字", wcf_key));
  110. startColNum++;
  111. ws.addCell(new Label(startColNum, startRowNum, "电话", wcf_key));
  112. startColNum++;
  113. ws.addCell(new Label(startColNum, startRowNum, "用户身份(0:老师,2:学生)", wcf_key));
  114. startColNum++;
  115. ws.addCell(new Label(startColNum, startRowNum, "地区", wcf_key));
  116. startColNum++;
  117. ws.addCell(new Label(startColNum, startRowNum, "学校", wcf_key));
  118. startColNum++;
  119. //将行数加1,列数重置为0
  120. startRowNum++;
  121. startColNum = 0;
  122. //添加记录
  123. while (beginList_it.hasNext()) {
  124. ClassBeginEntity ar = (ClassBeginEntity) beginList_it.next();
  125. ws.addCell(new Label(startColNum, startRowNum, ar.getUserId(), wcf_key));
  126. startColNum++;
  127. ws.addCell(new Label(startColNum, startRowNum, ar.getClassTitle(), wcf_key));
  128. startColNum++;
  129. ws.addCell(new Label(startColNum, startRowNum, ar.getCourseId(), wcf_key));
  130. startColNum++;
  131. ws.addCell(new Label(startColNum, startRowNum, ar.getUserRole(), wcf_key));
  132. startColNum++;
  133. ws.addCell(new Label(startColNum, startRowNum, ar.getUserName(), wcf_key));
  134. startColNum++;
  135. ws.addCell(new Label(startColNum, startRowNum, ar.getPhoneNumber(), wcf_key));
  136. startColNum++;
  137. ws.addCell(new Label(startColNum, startRowNum, ar.getUserIdEntity(), wcf_key));
  138. startColNum++;
  139. ws.addCell(new Label(startColNum, startRowNum, ar.getAreaName(), wcf_key));
  140. startColNum++;
  141. ws.addCell(new Label(startColNum, startRowNum, ar.getSchool(), wcf_key));
  142. startColNum++;
  143. //将行数加1,列数重置为0
  144. startRowNum++;
  145. startColNum = 0;
  146. }
  147. //设置Excel工作簿名称
  148. WritableSheet ws_sta = wb.createSheet("推广记录", 1);
  149. int startRowNum_sta = 0; // 起始行
  150. int startColNum_sta = 0; // 起始列
  151. // 设置列宽
  152. ws_sta.setColumnView(0, 35);
  153. ws_sta.setColumnView(1, 35);
  154. ws_sta.setColumnView(2, 35);
  155. ws_sta.setColumnView(3, 35);
  156. ws_sta.setColumnView(4, 35);
  157. ws_sta.setColumnView(5, 35);
  158. ws_sta.setColumnView(6, 35);
  159. ws_sta.setColumnView(7, 35);
  160. ws_sta.setColumnView(8, 35);
  161. ws_sta.setColumnView(9, 35);
  162. ws_sta.setColumnView(10, 35);
  163. ws_sta.setColumnView(11, 35);
  164. ws_sta.setColumnView(12, 35);
  165. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, time + "数据信息", wcf_title));
  166. ws_sta.mergeCells(startColNum_sta, startRowNum_sta, startColNum_sta + maxColSize - 1, startRowNum_sta); //合并单元格,合并(1,0)到(1,9)
  167. startColNum_sta = 0;
  168. startRowNum_sta++;
  169. //第1行,绘制表头
  170. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, "旁听ID", wcf_key));
  171. startColNum_sta++;
  172. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, "班海ID", wcf_key));
  173. startColNum_sta++;
  174. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, "课海ID", wcf_key));
  175. startColNum_sta++;
  176. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, "学生昵称", wcf_key));
  177. startColNum_sta++;
  178. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, "课程标题", wcf_key));
  179. startColNum_sta++;
  180. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, "课程主题", wcf_key));
  181. startColNum_sta++;
  182. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, "上课时间", wcf_key));
  183. startColNum_sta++;
  184. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, "推广时间", wcf_key));
  185. startColNum_sta++;
  186. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, "学校", wcf_key));
  187. startColNum_sta++;
  188. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, "学生信息", wcf_key));
  189. startColNum_sta++;
  190. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, "学校ID", wcf_key));
  191. startColNum_sta++;
  192. //将行数加1,列数重置为0
  193. startRowNum_sta++;
  194. startColNum_sta = 0;
  195. //添加记录
  196. while (recordList_it.hasNext()) {
  197. StaRecordEntity ar = (StaRecordEntity) recordList_it.next();
  198. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, ar.getPtId(), wcf_key));
  199. startColNum_sta++;
  200. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, ar.getBhUserId(), wcf_key));
  201. startColNum_sta++;
  202. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, ar.getKhUserId(), wcf_key));
  203. startColNum_sta++;
  204. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, ar.getStuName(), wcf_key));
  205. startColNum_sta++;
  206. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, ar.getPtTitle(), wcf_key));
  207. startColNum_sta++;
  208. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, ar.getTheme(), wcf_key));
  209. startColNum_sta++;
  210. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, ar.getStartClass(), wcf_key));
  211. startColNum_sta++;
  212. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, ar.getTui(), wcf_key));
  213. startColNum_sta++;
  214. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, ar.getSchoolNickName(), wcf_key));
  215. startColNum_sta++;
  216. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, ar.getStuDetails(), wcf_key));
  217. startColNum_sta++;
  218. ws_sta.addCell(new Label(startColNum_sta, startRowNum_sta, ar.getSchoolId(), wcf_key));
  219. startColNum_sta++;
  220. //将行数加1,列数重置为0
  221. startRowNum_sta++;
  222. startColNum_sta = 0;
  223. }
  224. //购买工作簿
  225. //设置Excel工作簿名称
  226. WritableSheet ws_mai = wb.createSheet("购买记录", 2);
  227. int startRowNum_mai = 0; // 起始行
  228. int startColNum_mai = 0; // 起始列
  229. // 设置列宽
  230. ws_mai.setColumnView(0, 35);
  231. ws_mai.setColumnView(1, 35);
  232. ws_mai.setColumnView(2, 35);
  233. ws_mai.setColumnView(3, 35);
  234. ws_mai.setColumnView(4, 35);
  235. ws_mai.setColumnView(5, 35);
  236. ws_mai.setColumnView(6, 35);
  237. ws_mai.setColumnView(7, 35);
  238. ws_mai.setColumnView(8, 35);
  239. ws_mai.setColumnView(9, 35);
  240. ws_mai.setColumnView(10, 35);
  241. ws_mai.setColumnView(11, 35);
  242. ws_mai.setColumnView(12, 35);
  243. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, time + "数据信息", wcf_title));
  244. ws_mai.mergeCells(startColNum_mai, startRowNum_mai, startColNum_mai + maxColSize - 1, startRowNum_mai); //合并单元格,合并(1,0)到(1,9)
  245. startColNum_mai = 0;
  246. startRowNum_mai++;
  247. //第1行,绘制表头
  248. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, "用户ID", wcf_key));
  249. startColNum_mai++;
  250. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, "用户昵称", wcf_key));
  251. startColNum_mai++;
  252. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, "电话", wcf_key));
  253. startColNum_mai++;
  254. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, "价格(元)", wcf_key));
  255. startColNum_mai++;
  256. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, "系列课程标题名", wcf_key));
  257. startColNum_mai++;
  258. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, "课程标题", wcf_key));
  259. startColNum_mai++;
  260. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, "地区", wcf_key));
  261. startColNum_mai++;
  262. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, "学校名", wcf_key));
  263. startColNum_mai++;
  264. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, "开始时间", wcf_key));
  265. startColNum_mai++;
  266. //将行数加1,列数重置为0
  267. startRowNum_mai++;
  268. startColNum_mai = 0;
  269. //添加记录
  270. while (entryList_it.hasNext()) {
  271. EntryRecordEntity ar = (EntryRecordEntity) entryList_it.next();
  272. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, ar.getUserId(), wcf_key));
  273. startColNum_mai++;
  274. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, ar.getNickName(), wcf_key));
  275. startColNum_mai++;
  276. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, ar.getPhoneNumber(), wcf_key));
  277. startColNum_mai++;
  278. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, Long.valueOf(ar.getOrderActualPrice()) / 100 + "", wcf_key));
  279. startColNum_mai++;
  280. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, ar.getCourseTitle(), wcf_key));
  281. startColNum_mai++;
  282. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, ar.getCoursesName(), wcf_key));
  283. startColNum_mai++;
  284. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, ar.getAreaName(), wcf_key));
  285. startColNum_mai++;
  286. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, ar.getSchool(), wcf_key));
  287. startColNum_mai++;
  288. ws_mai.addCell(new Label(startColNum_mai, startRowNum_mai, ar.getCoursesBeginTimes(), wcf_key));
  289. startColNum_mai++;
  290. //将行数加1,列数重置为0
  291. startRowNum_mai++;
  292. startColNum_mai = 0;
  293. }
  294. //注意在这里要生成多个表格标签页就不能中间关闭流
  295. wb.write(); //生成Excel工作簿
  296. wb.close();
  297. }
  298. }

依赖的maven:

  1. <!-- excle表格jar -->
  2. <dependency>
  3. <groupId>net.sourceforge.jexcelapi</groupId>
  4. <artifactId>jxl</artifactId>
  5. <version>2.6.10</version>
  6. </dependency>

注意:在添加多个标签页的时候不能在中间关闭流,不然只会生成最后一个标签页

原创粉丝点击