当前位置:   article > 正文

springmvc 导出excel_spring mvc 导出excle 写入respose 前端接收

spring mvc 导出excle 写入respose 前端接收
  1. @RequestMapping("/dataLogToExcel")
  2. @ResponseBody
  3. public void OperationLogToExcel(HttpServletRequest request,
  4. HttpServletResponse response) throws UnsupportedEncodingException {
  5. // 生成提示信息,
  6. response.setContentType("application/vnd.ms-excel");
  7. String codedFileName = null;
  8. OutputStream fOut = null;
  9. try
  10. {
  11. // 进行转码,使其支持中文文件名
  12. codedFileName = java.net.URLEncoder.encode("数据操作日志", "UTF-8");
  13. response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
  14. // response.addHeader("Content-Disposition", "attachment; filename=" + codedFileName + ".xls");
  15. // 产生工作簿对象
  16. HSSFWorkbook workbook = new HSSFWorkbook();
  17. //产生工作表对象
  18. HSSFSheet sheet = workbook.createSheet();
  19. //设置表格默认列宽度为15个字节
  20. sheet.setDefaultColumnWidth(15);
  21. //设置自动换行
  22. sheet.setFitToPage(true);
  23. // 设置标题
  24. HSSFCellStyle titleStyle = workbook.createCellStyle();
  25. // 居中显示
  26. titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  27. // 标题字体
  28. HSSFFont titleFont = workbook.createFont();
  29. // 字体大小
  30. titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
  31. titleStyle.setFont(titleFont);
  32. HSSFCellStyle contentStyle = workbook.createCellStyle();
  33. contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  34. HSSFFont contentFont = workbook.createFont();
  35. contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
  36. contentStyle.setFont(contentFont);
  37. // 产生表格标题行
  38. HSSFRow row = sheet.createRow(0);
  39. String[] headers = new String[] { "序号", "操作时间", "同步类型", "同步条数", "同步内容",
  40. "同步结果", };
  41. for (int i = 0; i < headers.length; i++) {
  42. HSSFCell cell = row.createCell(i);
  43. HSSFRichTextString text = new HSSFRichTextString(headers[i]);
  44. cell.setCellValue(text);
  45. }
  46. //生成数据
  47. List<DataLog> dataLogList = dataLogService.selectAllDataLog();
  48. int rowCount = 1;
  49. for (int i = 0; i < dataLogList.size(); i++, rowCount++) {
  50. HSSFRow dataRow = sheet.createRow(rowCount);
  51. DataLog dataLog=dataLogList.get(i);
  52. //序号
  53. HSSFCell cell0 = dataRow.createCell(0);
  54. cell0.setCellValue((i + 1));
  55. cell0.setCellStyle(contentStyle);
  56. //操作时间
  57. HSSFCell cell1 = dataRow.createCell(1);
  58. Date dataTime=dataLog.getDataTime();
  59. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  60. String dateString = formatter.format(dataTime);
  61. cell1.setCellValue(dateString);
  62. cell1.setCellStyle(contentStyle);
  63. //同步类型
  64. HSSFCell cell2 = dataRow.createCell(2);
  65. cell2.setCellValue(dataLog.getDataType());
  66. cell2.setCellStyle(contentStyle);
  67. cell2.setAsActiveCell();
  68. //同步条数
  69. HSSFCell cell3 = dataRow.createCell(3);
  70. cell3.setCellValue(dataLog.getDataCount());
  71. cell3.setCellStyle(contentStyle);
  72. cell3.setAsActiveCell();
  73. //同步内容
  74. HSSFCell cell4 = dataRow.createCell(4);
  75. cell4.setCellValue(dataLog.getDataContent());
  76. cell4.setCellStyle(contentStyle);
  77. //同步结果
  78. HSSFCell cell5 = dataRow.createCell(5);
  79. cell5.setCellValue(dataLog.getDataContent());
  80. cell5.setCellStyle(contentStyle);
  81. }
  82. fOut = response.getOutputStream();
  83. workbook.write(fOut);
  84. }
  85. catch (UnsupportedEncodingException e1)
  86. {}
  87. catch (Exception e)
  88. {}
  89. finally
  90. {
  91. try
  92. {
  93. fOut.flush();
  94. fOut.close();
  95. }
  96. catch (IOException e)
  97. {}
  98. //session.setAttribute("state", "open");
  99. }
  100. System.out.println("文件生成...");
  101. }

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

闽ICP备14008679号