java 导出excel

来源:互联网 发布:淘宝查优惠券插件 编辑:程序博客网 时间:2024/06/05 19:15

页面:

$("#exportBtn").live("click", function () {                           window.open('/exportMoneyConsume?type=1&incident=1');            });

后台:

controller

@Get("/exportMoneyConsume")@LoginCheckRequiredpublic String exportMoneyConsume(Invocation inv,MoneyConsumeView param) throws Exception {String fileName="adf"+System.currentTimeMillis()+".xls";String path=Constants.UPLOAD_BASE_FOLD +"/"+fileName;exportMoneyConsume(path,param);downLoad(inv, fileName, path);return "@";}

Service:

public void exportMoneyConsume(String path, MoneyConsumeView param) {List<MoneyConsumeView> list = MoneyConsumeDAO.exportMoneyConsume(param);// 1.创建Excel工作薄对象HSSFWorkbook wb = new HSSFWorkbook();// 2.创建Excel工作表对象HSSFSheet sheet = wb.createSheet("new Sheet");// 3.创建单元格样式CellStyle cellStyle = wb.createCellStyle();// 设置这些样式cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);sheet.setDefaultColumnWidth(20);int i = 0;// 4.创建Excel工作表的行HSSFRow row = sheet.createRow(i);row.createCell(0).setCellValue("");row.createCell(1).setCellValue("aa");row.createCell(2).setCellValue("bb");row.createCell(3).setCellValue("df");row.createCell(4).setCellValue("fasasf");row.createCell(5).setCellValue("adsf");row.createCell(6).setCellValue("asdf");row.createCell(7).setCellValue("asdf");row.createCell(8).setCellValue("asdf");row.createCell(9).setCellValue("asfd");row.createCell(10).setCellValue("safd");row.createCell(11).setCellValue("asfd");for (MoneyConsumeView moneyConsume : list) {if (moneyConsume.getLevel() == null) {continue;}String fileName = SchoolLevelEnum.getName(moneyConsume.getLevel());if (StringUtils.isNotBlank(fileName)) {moneyConsume.setLevelName(fileName);}row = sheet.createRow(i + 1);row.createCell(0).setCellValue(i + 1);row.createCell(1).setCellValue(areaCacheService.getAreaNameCache(moneyConsume.getAreaCode()));row.createCell(2).setCellValue(moneyConsume.getLevelName());row.createCell(3).setCellValue(schoolCacheService.getSchoolNameCache(moneyConsume.getSchoolId()));row.createCell(4).setCellValue(moneyConsume.getUserName());row.createCell(5).setCellValue(DateUtils.formatDate(moneyConsume.getCreateTime(), DateUtils.FORMAT_DAY_TIME));row.createCell(6).setCellValue(moneyConsume.getName());row.createCell(7).setCellValue(moneyConsume.getPrice());row.createCell(8).setCellValue(moneyConsume.getStudentCount());row.createCell(9).setCellValue(moneyConsume.getDiscount());row.createCell(10).setCellValue(moneyConsume.getPayAmount());if(moneyConsume.getPresentBalance()!=null){row.createCell(11).setCellValue(moneyConsume.getPresentBalance());}else{row.createCell(11).setCellValue(0);}i = i + 1;}// 设置sheet名称和单元格内容wb.setSheetName(0, "第一张工作表");// 设置单元格内容 cell.setCellValue("单元格内容");// 最后一步,将文件存到指定位置try {FileOutputStream fout = new FileOutputStream(path);wb.write(fout);fout.close();} catch (Exception e) {e.printStackTrace();}}

public void downLoad(Invocation inv, String fileName, String path) throws IOException {File file = new File(path);// 构造要下载的文件if (file.exists()) {    InputStream ins = null;BufferedInputStream bins = null;OutputStream outs = null;BufferedOutputStream bouts = null;try {ins = new FileInputStream(path);// 构造一个读取文件的IO流对象bins = new BufferedInputStream(ins);// 放到缓冲流里面outs = inv.getResponse().getOutputStream();// 获取文件输出IO流bouts = new BufferedOutputStream(outs);inv.getResponse().setContentType("application/x-download");// 设置response内容的类型inv.getResponse().setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));// 设置头部信息int bytesRead = 0;byte[] buffer = new byte[8192];// 开始向网络传输文件流while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {bouts.write(buffer, 0, bytesRead);}bouts.flush();// 这里一定要调用flush()方法} catch (Exception e) {e.printStackTrace();} finally {if (bouts != null) {bouts.close();bouts = null;}if (outs != null) {outs.close();outs = null;}if (bins != null) {bins.close();bins = null;}if (ins != null) {ins.close();ins = null;}file.delete();}} else {logger.info("导出的文件不存在");}}



0 0