赞
踩
- @RequestMapping("/dataLogToExcel")
- @ResponseBody
- public void OperationLogToExcel(HttpServletRequest request,
- HttpServletResponse response) throws UnsupportedEncodingException {
-
-
- // 生成提示信息,
- response.setContentType("application/vnd.ms-excel");
- String codedFileName = null;
- OutputStream fOut = null;
- try
- {
- // 进行转码,使其支持中文文件名
- codedFileName = java.net.URLEncoder.encode("数据操作日志", "UTF-8");
- response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
- // response.addHeader("Content-Disposition", "attachment; filename=" + codedFileName + ".xls");
- // 产生工作簿对象
- HSSFWorkbook workbook = new HSSFWorkbook();
- //产生工作表对象
- HSSFSheet sheet = workbook.createSheet();
-
- //设置表格默认列宽度为15个字节
- sheet.setDefaultColumnWidth(15);
-
- //设置自动换行
- sheet.setFitToPage(true);
- // 设置标题
- HSSFCellStyle titleStyle = workbook.createCellStyle();
- // 居中显示
- titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- // 标题字体
- HSSFFont titleFont = workbook.createFont();
- // 字体大小
- titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
- titleStyle.setFont(titleFont);
-
- HSSFCellStyle contentStyle = workbook.createCellStyle();
- contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- HSSFFont contentFont = workbook.createFont();
- contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
- contentStyle.setFont(contentFont);
-
- // 产生表格标题行
- HSSFRow row = sheet.createRow(0);
- String[] headers = new String[] { "序号", "操作时间", "同步类型", "同步条数", "同步内容",
- "同步结果", };
- for (int i = 0; i < headers.length; i++) {
- HSSFCell cell = row.createCell(i);
- HSSFRichTextString text = new HSSFRichTextString(headers[i]);
- cell.setCellValue(text);
- }
- //生成数据
- List<DataLog> dataLogList = dataLogService.selectAllDataLog();
- int rowCount = 1;
- for (int i = 0; i < dataLogList.size(); i++, rowCount++) {
- HSSFRow dataRow = sheet.createRow(rowCount);
- DataLog dataLog=dataLogList.get(i);
-
- //序号
- HSSFCell cell0 = dataRow.createCell(0);
- cell0.setCellValue((i + 1));
- cell0.setCellStyle(contentStyle);
-
-
-
- //操作时间
- HSSFCell cell1 = dataRow.createCell(1);
- Date dataTime=dataLog.getDataTime();
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateString = formatter.format(dataTime);
- cell1.setCellValue(dateString);
- cell1.setCellStyle(contentStyle);
-
- //同步类型
- HSSFCell cell2 = dataRow.createCell(2);
- cell2.setCellValue(dataLog.getDataType());
- cell2.setCellStyle(contentStyle);
- cell2.setAsActiveCell();
-
- //同步条数
- HSSFCell cell3 = dataRow.createCell(3);
- cell3.setCellValue(dataLog.getDataCount());
- cell3.setCellStyle(contentStyle);
- cell3.setAsActiveCell();
-
- //同步内容
- HSSFCell cell4 = dataRow.createCell(4);
- cell4.setCellValue(dataLog.getDataContent());
- cell4.setCellStyle(contentStyle);
-
- //同步结果
- HSSFCell cell5 = dataRow.createCell(5);
- cell5.setCellValue(dataLog.getDataContent());
- cell5.setCellStyle(contentStyle);
-
-
- }
-
-
- fOut = response.getOutputStream();
- workbook.write(fOut);
- }
- catch (UnsupportedEncodingException e1)
- {}
- catch (Exception e)
- {}
- finally
- {
- try
- {
- fOut.flush();
- fOut.close();
- }
- catch (IOException e)
- {}
- //session.setAttribute("state", "open");
- }
- System.out.println("文件生成...");
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。