- public String outPutExcel(ModelMap model, HttpServletRequest request,
- HttpServletResponse response) {
-
- //獲取時間區(qū)域
- String timeArea = request.getParameter("timeArea");
- String keyword = request.getParameter("keyword");
- String select = request.getParameter("select");
- //獲取操作類型
- String operate = request.getParameter("operate");
-
- // File file = new File("daily.xls");
- try {
- OutputStream os = response.getOutputStream();// 取得輸出流
- response.reset();// 清空輸出流
- response.setHeader("Content-disposition", "attachment; filename="
- + new String("daily".getBytes("GB2312"),
- "iso8859_1") + ".xls");// 設(shè)定輸出文件頭
- response.setContentType("application/msexcel");// 定義輸出類型
-
- WritableWorkbook workbook = Workbook.createWorkbook(os);
- if (workbook != null) {
-
- WritableSheet sheet = workbook.createSheet("sheet1", 0);
-
- // 設(shè)置標題 sheet.addCell(new jxl.write.Label(列(從0開始), 行(從0開始), 內(nèi)容.));
- try {
- sheet.addCell(new Label(0, 0, "區(qū)域名"));
- sheet.addCell(new Label(1, 0, "所在位置"));
- sheet.addCell(new Label(2, 0, "操作人員"));
- sheet.addCell(new Label(3, 0, "時間"));
- sheet.addCell(new Label(4, 0, "操作類型"));
- sheet.addCell(new Label(5, 0, "備注"));
-
- // 設(shè)置單元格的寬度
- sheet.setColumnView(0, 20);
- sheet.setColumnView(1, 40);
- sheet.setColumnView(2, 15);
- sheet.setColumnView(3, 30);
- sheet.setColumnView(4, 15);
- sheet.setColumnView(5, 20);
-
- //添加數(shù)據(jù)
- List<SysActionLog> list = this.getSysActionLogs(timeArea, keyword, select,
- operate);
-
- SysActionLog log = null;
- for (int r = 0; r < list.size(); r++) { //行編號
- log = list.get(r);
- //區(qū)域名
- sheet.addCell(new Label(0, r + 1, log.getFilealia()));
- //所在位置
- sheet.addCell(new Label(1, r + 1, log.getFilename()));
- //操作人員
- sheet.addCell(new Label(2, r + 1, log.getAuthor()));
- //時間
- sheet.addCell(new Label(3, r + 1, log.getFormatDate()));
- //操作類型
- sheet.addCell(new Label(4, r + 1, log.getSysAction().getMessage()));
- //備注
- sheet.addCell(new Label(5, r + 1, log.getComment()));
- }
-
- //從內(nèi)存中寫入文件中
- workbook.write();
- //關(guān)閉資源,釋放內(nèi)存
- workbook.close();
-
- /*response.reset();
- FileInputStream f = new FileInputStream(file);
- byte[] fb = new byte[f.available()];
- f.read(fb);
- response
- .setHeader("Content-disposition", "attachment; filename="
- + new String("daily.xls"
- .getBytes("gb2312"), "iso8859-1"));
- ByteArrayInputStream bais = new ByteArrayInputStream(fb);
- int b;
- while ((b = bais.read()) != -1) {
- response.getOutputStream().write(b);
- }
- response.getOutputStream().flush();
-
- //刪除服務(wù)器上的文件
- file.deleteOnExit();*/
-
- } catch (RowsExceededException e) {
- logger.error("sheet不存在", e);
- } catch (WriteException e) {
- logger.error("創(chuàng)建列名出錯", e);
- }
- }
-
- } catch (IOException e) {
- logger.error("文件創(chuàng)建出錯", e);
- }
- return "daily.vm";
- }
|