jxl导出excel

public String generationExcel(String sql, String[] title, HttpServletRequest request) throws Exception { String dir = null; dir = request.getSession().getServletContext().getRealPath("") + File.separator + "excel" + File.separator + "metting"; File fileDir = new File(dir); if (!fileDir.exists()) { fileDir.mkdirs(); } String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); /* 文件名称 */ String fileName = "meeting_" + date + ".xls"; /* Excel生成路径 */ String exclepath = dir + File.separator + fileName; Session session = super.getGiantBaseDao().getHibernateSession(); WritableWorkbook book = null; try { /* 创建一个工作薄对象 */ book = Workbook.createWorkbook(new File(exclepath)); /* 创建出一个工作区 */ WritableSheet sheet = book.createSheet(date, 0); /* 根据传入的标题长度生成标题 */ /* 执行SQL查询要导出的数据 */ List list = session.createSQLQuery(sql).list(); if(list.size()==0){ return null; } for (int j = 0; j < title.length; j++) { Label label = new Label(j, 0, title[j]); sheet.addCell(label); } for (int i = 0; i < list.size(); i++) { Object[] cell = list.get(i); for (int j = 0; j < cell.length; j++) { if (cell[j] != null) { if (j == 7) { String strT = ""; if ("0".equals(cell[j].toString().trim())) { strT = "未通知"; } else if ("1".equals(cell[j].toString().trim())) { strT = "已通知"; } else { strT = "未知"; } // 对财务数据进行部分修改 Label label = new Label(j, i + 1, strT); sheet.addCell(label); }else{ // 对财务数据进行部分修改 Label label = new Label(j, i + 1, cell[j].toString()); sheet.addCell(label); } } else { Label label = new Label(j, i + 1, ""); sheet.addCell(label); } } } book.write(); } catch (Exception e) { throw new Exception(e.getMessage()); } finally { if (book != null) { try { book.close(); } catch (WriteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } if (session != null) { session.close(); } } return fileName; }

try {
           上面的方法生成路径

            if (fileName != null && !"".equals(fileName)) {
                ServletActionContext.getResponse().sendRedirect(
                        getRequest().getContextPath() + "/excel/metting/"
                                + fileName);
                return null;
            } else {
                resultInfo = "没有查询到相关数据";
            }
        } catch (Exception e) {
            resultInfo = "生成Excel发生异常:" + e.getMessage();
        }

你可能感兴趣的:(jxl导出excel)