标签:
/** * 汇总报表数据下载 * */ private ModelAndView exportSummaryDatadown(HttpServletRequest request, HttpServletResponse response, List<InterfaceCost> interfaceCostListNew) throws Exception { //保存到磁盘 String time = QDateTime.dateToString(new Date(), "yyyy-MM-dd HH:mi:ss"); time = time.replaceAll("-", ""); time =time.replace(":", ""); time =time.replace(" ", ""); time = time.substring(2); String file_name = time + ".xls"; String uploadDir = request.getRealPath("/resources") + "\\Interfaceparameter\\"; OutputStream out = null; try { File dirPath = new File(uploadDir); if (!dirPath.exists()) { dirPath.mkdirs(); } out = new FileOutputStream(uploadDir+file_name); // 设置第一行(表头的格式) jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc); // 表格样式 wcfFC.setAlignment(jxl.format.Alignment.CENTRE);// 水平居中 wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 垂直居中 wcfFC.setBorder(Border.ALL, BorderLineStyle.THIN);// 线条 WritableWorkbook wb = Workbook.createWorkbook(out);// 写入流中 WritableSheet ws = wb.createSheet("sheet1", 1); int[] geshi = {30,20,20} ;// 获取列宽 for (int i = 0; i < geshi.length; i++) { ws.setColumnView(i, geshi[i]); } // int ioNor = 0; // 订单列记录值标识 //数据总计 jxl.write.Label name = new jxl.write.Label(0, 0, "接口名称", wcfFC); ws.addCell(name); jxl.write.Label count = new jxl.write.Label(1, 0, "使用次数", wcfFC); ws.addCell(count); jxl.write.Label amount = new jxl.write.Label(2, 0, "消费点数", wcfFC); ws.addCell(amount); // 显示第二行以后的数据 // 设置第二行以后数据的格式 wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); wcfFC = new WritableCellFormat(wfc); wcfFC.setAlignment(jxl.format.Alignment.CENTRE);// 水平居中 wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 垂直居中 wcfFC.setBorder(Border.ALL, BorderLineStyle.THIN);// 线条 int ioNor = 0; // 订单列记录值标识 Integer addUpTo = 0; for(InterfaceCost interfaceCost:interfaceCostListNew){ jxl.write.Label optType = new jxl.write.Label(0, ioNor + 1, interfaceCost.getInterfaceName(), wcfFC); ws.addCell(optType); jxl.write.Number totlecount = new jxl.write.Number(1, ioNor + 1, interfaceCost.getTotleCount(), wcfFC); ws.addCell(totlecount); jxl.write.Number totle = new jxl.write.Number(2, ioNor + 1, interfaceCost.getTotleCost(), wcfFC); ws.addCell(totle); addUpTo += interfaceCost.getTotleCost(); ioNor++; //ws.mergeCells(0, ioNor, 1, ioNor);//合并单元格 } ioNor=ioNor+1; jxl.write.Label numberName = new jxl.write.Label(0, ioNor, "合计消费 "+addUpTo.toString()+" 点", wcfFC); ws.addCell(numberName); ws.mergeCells(0, ioNor, 2, ioNor);//合并单元格 wb.write(); wb.close(); // out.close(); } catch (Exception ex) { ex.printStackTrace(); }finally{ if(out!=null){ out.close(); } } //从磁盘读取 InputStream inStream = null; OutputStream outs = null; try { response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment; filename=" + file_name + ""); inStream = new FileInputStream(uploadDir+file_name); // String lineTxt = null; outs = response.getOutputStream(); byte[] buf = new byte[4096]; int readLength; while (((readLength = inStream.read(buf)) != -1)) { outs.write(buf, 0, readLength); } // inStream.close(); outs.flush(); // outs.close(); } catch (Exception e) { log.error("读取文件内容出错"); e.printStackTrace(); }finally{ if(out!=null){ out.close(); } if(inStream!=null){ inStream.close(); } } return null; }
标签:
原文地址:http://www.cnblogs.com/jinzhiming/p/4826852.html