这个是按钮这个是相关事件// 导出按钮$('#exportBtn').click(function(){txtBeginDate = $..._$("#btnexport").click(function () if">
当前位置:   article > 正文

java通过模板导出excel的一个实例

$("#btnexport").click(function () if

写之前,大家请先下好poi的相关jar包,网上遍地都是,不多说

<input type="button" id="exportBtn" class="CommonBtn" value="导出Excel" />  这个是按钮

这个是相关事件

  1. // 导出按钮
  2. $('#exportBtn').click(function(){
  3. txtBeginDate = $('#txtBeginDate').datebox('getValue');
  4. txtEndDate = $('#txtEndDate').datebox('getValue');
  5. window.location.href='${CTX_ROOT}/TBoilerrunlogController?method=getExcelByBoiler&begin='+txtBeginDate+'&end='+txtEndDate+'';
  6. }); //我是转向spring mvc的controllor里面了,我这里还给传了俩个值,一个是开始时间,一个是结束时间


controllor代码如下:

  1. @RequestMapping(params = "method=getExcelByBoiler")
  2. @ResponseBody
  3. public String getExcelByBoiler(HttpServletRequest request,
  4. HttpServletResponse response) {
  5. String begin="";
  6. String end="";
  7. if(request.getParameter("begin")!=null && request.getParameter("end") !=null){
  8. begin=request.getParameter("begin");
  9. end = request.getParameter("end");
  10. }
  11. try {
  12. String path =request.getServletContext().getRealPath("")
  13. + "/excelTemplate/统计.xls"; //这个是我的excel模板
  14. InputStream in = new FileInputStream(new File(path));
  15. SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //为了转时间
  16. HSSFWorkbook work = new HSSFWorkbook(in);
  17. // 得到excel的第0张表
  18. HSSFSheet sheet = work.getSheetAt(0);
  19. // 得到第1行的第一个单元格的样式
  20. HSSFRow rowCellStyle = sheet.getRow(2);
  21. HSSFCellStyle columnOne = rowCellStyle.getCell(0).getCellStyle();
  22. HSSFCellStyle columnOne1 = rowCellStyle.getCell(1).getCellStyle();
  23. HSSFCellStyle columnOne2 = rowCellStyle.getCell(4).getCellStyle();
  24. // 这里面的行和列的数法与计算机里的一样,从0开始是第一
  25. // 填充title数据
  26. HSSFRow row = sheet.getRow(0);
  27. HSSFCell cell = row.getCell(2);
  28. cell.setCellValue("一号机组启停统计(" + begin + "~" + end + ")");
  29. int i = 2;// 计数器
  30. List<Map<String, Object>> list = this.getBoilerRunlog("GL1",begin,end);
  31. // 创建每个单元格,添加样式,最后合并
  32. work.getSheetAt(0).shiftRows(3, 3, list.size() - 1); // 把第三行移动到列表的长度-1的地方 这个地方我是要把模板的第四行放到最后一行
  33. int numberbegin = 0;// 统计
  34. int numberend = 0;// 统计
  35. for (Map map : list) {
  36. row = sheet.createRow(i);// 得到行
  37. cell = row.createCell(0);// 得到第0个单元格
  38. if (!(map.get("actiondatetime") == null)) {
  39. cell.setCellValue(simpleFormat.format(map.get("actiondatetime")));
  40. } else {
  41. cell.setCellValue("");
  42. }
  43. cell.setCellStyle(columnOne);// 填充样式
  44. cell = row.createCell(1);
  45. if (!(map.get("actionstatus") == null)) {
  46. cell.setCellValue(map.get("actionstatus").toString());
  47. } else {
  48. cell.setCellValue("");
  49. }
  50. cell.setCellStyle(columnOne1);// 填充样式
  51. cell = row.createCell(2);
  52. if (!(map.get("logmemo") == null)) {
  53. cell.setCellValue(map.get("logmemo").toString());
  54. } else {
  55. cell.setCellValue("");
  56. }
  57. cell.setCellStyle(columnOne1);// 填充样式
  58. cell = row.createCell(3);
  59. if (!(map.get("rundays") == null)) {
  60. cell.setCellValue(map.get("rundays").toString());
  61. numberbegin++;
  62. } else {
  63. cell.setCellValue("");
  64. }
  65. cell.setCellStyle(columnOne1);// 填充样式
  66. cell = row.createCell(4);
  67. if (!(map.get("stophours") == null)) {
  68. cell.setCellValue(map.get("stophours").toString());
  69. numberend++;
  70. } else {
  71. cell.setCellValue("");
  72. }
  73. cell.setCellStyle(columnOne2);// 填充样式
  74. // .....给每个单元格填充数据和样式
  75. i++;
  76. }
  77. HSSFRow row1 = sheet.getRow(list.size() + 2);
  78. HSSFCell cell1 = row1.getCell(0);
  79. cell1.setCellValue("启停共" + (numberbegin+numberend) + "次," + "其中启" + numberbegin
  80. + "次,停" + numberend + "次");//
  81. /*******************************sheet二*******************************************/
  82. HSSFSheet sheet1 = work.getSheetAt(1);
  83. // 得到第1行的第一个单元格的样式
  84. HSSFRow rowCellStyle1 = sheet1.getRow(2);
  85. HSSFCellStyle columnOne01 = rowCellStyle1.getCell(0).getCellStyle();
  86. HSSFCellStyle columnOne02 = rowCellStyle1.getCell(1).getCellStyle();
  87. HSSFCellStyle columnOne03 = rowCellStyle1.getCell(4).getCellStyle();
  88. HSSFRow row2 = sheet1.getRow(0);
  89. HSSFCell cell2 = row2.getCell(2);
  90. cell2.setCellValue("二号机组启停统计(" + begin + "~" + end + ")");
  91. int j = 2;// 计数器
  92. List<Map<String, Object>> list2 = this.getBoilerRunlog("GL2",begin,end);
  93. // 创建每个单元格,添加样式,最后合并
  94. work.getSheetAt(1).shiftRows(3, 3, list2.size() - 1); // 把第三行移动到列表的长度-1的地方
  95. int numberbegin1 = 0;// 统计
  96. int numberend1 = 0;// 统计
  97. for (Map map : list2) {
  98. row2 = sheet1.createRow(j);// 得到行
  99. cell2 = row2.createCell(0);// 得到第0个单元格
  100. if (!(map.get("actiondatetime") == null)) {
  101. cell2.setCellValue(simpleFormat.format(map.get("actiondatetime")));
  102. } else {
  103. cell2.setCellValue("");
  104. }
  105. cell2.setCellStyle(columnOne01);// 填充样式
  106. cell2 = row2.createCell(1);
  107. if (!(map.get("actionstatus") == null)) {
  108. cell2.setCellValue(map.get("actionstatus").toString());
  109. } else {
  110. cell2.setCellValue("");
  111. }
  112. cell2.setCellStyle(columnOne02);// 填充样式
  113. cell2 = row2.createCell(2);
  114. if (!(map.get("logmemo") == null)) {
  115. cell2.setCellValue(map.get("logmemo").toString());
  116. } else {
  117. cell2.setCellValue("");
  118. }
  119. cell2.setCellStyle(columnOne02);// 填充样式
  120. cell2 = row2.createCell(3);
  121. if (!(map.get("rundays") == null)) {
  122. cell2.setCellValue(map.get("rundays").toString());
  123. numberbegin1++;
  124. } else {
  125. cell2.setCellValue("");
  126. }
  127. cell2.setCellStyle(columnOne02);// 填充样式
  128. cell2 = row2.createCell(4);
  129. if (!(map.get("stophours") == null)) {
  130. cell2.setCellValue(map.get("stophours").toString());
  131. numberend1++;
  132. } else {
  133. cell2.setCellValue("");
  134. }
  135. cell2.setCellStyle(columnOne03);// 填充样式
  136. // .....给每个单元格填充数据和样式
  137. i++;
  138. }
  139. HSSFRow row3 = sheet1.getRow(list2.size() + 2);
  140. HSSFCell cell3 = row3.getCell(0);
  141. cell3.setCellValue("启停共" + (numberbegin1+numberend1) + "次," + "其中启" + numberbegin1
  142. + "次,停" + numberend1 + "次");//
  143. /****************************输出流*****************************************/
  144. String address = simpleFormat.format(new Date());
  145. OutputStream os = response.getOutputStream();// 取得输出流
  146. response.setContentType("application/vnd.ms-excel");
  147. response.setHeader("Content-disposition",
  148. "attachment;filename=jzqttj_"+address+".xls");
  149. work.write(os);
  150. os.close();
  151. } catch (FileNotFoundException e) {
  152. System.out.println("文件路径错误");
  153. e.printStackTrace();
  154. } catch (IOException e) {
  155. System.out.println("文件输入流错误");
  156. e.printStackTrace();
  157. }
  158. return null;
  159. }


这个大概思路就是,我把提前写好的excel模板的要遍历的那一行每个cell(单元格)的样式复制出来,然后在接下来的每行每个cell都给赋予相关的样式

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/shipeng22022/p/4614026.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/726139
推荐阅读
相关标签
  

闽ICP备14008679号