本文实例讲述了JSP导出Excel文件的方法。分享给大家供大家参考,具体如下:
<%@page import="jxl.Workbook,com.ecc.emp.core.*,com.ecc.emp.data.*,com.ecc.emp.jdbc.ConnectionManager,jxl.format.VerticalAlignment,java.sql.*,jxl.write.*,jxl.format.UnderlineStyle,javax.sql.DataSource"%> <%@page language="java" contentType="text/html; charset=UTF-8"%> <%@taglib uri="/WEB-INF/CTP.tld" prefix="ctp"%> <jsp:directive.page import="java.util.List"/> <jsp:directive.page import="java.util.ArrayList"/> <%@ include file="http://www.xxxx.com/head.jsp" %> <% Context context = (Context)request.getAttribute("context");String flag ="交易日期,交易时间,摘要,借方发生额,贷方发生额,对方账号,对方户名,单位卡号,主卡标识,用途,备注"; IndexedCollection indexedCollection = (IndexedCollection)context.getDataElement("parentCardDetails"); String[] sourceStrArray=flag.split(","); List selValList = new ArrayList(); selValList.add("序号#"); for(int i=0;i<sourceStrArray.length;i++){ selValList.add(sourceStrArray[i]+"#"); } WritableWorkbook book = null; if (indexedCollection == null || indexedCollection.size()<0) {%> <script language="javascript"> alert('对不起!当前条件没有数据,不能导出!'); //history.go(-1); </script> <% } else { response.reset(); response.setContentType("charset=UTF-8"); response.setContentType("application/vnd.ms-excel"); String filename = "regularInform.xls"; response.setHeader("Content-Disposition","attachment; filename=" + filename); //解决HTTPS不能下载的问题 response.setHeader("Cache-Control","public"); book = Workbook.createWorkbook(response.getOutputStream()); //表头字体 WritableCellFormat writableCellFormat = new WritableCellFormat(); writableCellFormat.setAlignment(Alignment.CENTRE); writableCellFormat.setVerticalAlignment(VerticalAlignment.CENTRE); writableCellFormat.setWrap(true); WritableFont cellFont = new WritableFont(WritableFont.createFont("宋体"), 14, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); writableCellFormat.setFont(cellFont); writableCellFormat.setBorder(Border.ALL, BorderLineStyle.THIN); writableCellFormat.setBackground(Colour.SKY_BLUE, Pattern.SOLID); //标题字体(左对齐) WritableCellFormat writableCellFormat1 = new WritableCellFormat(); writableCellFormat1.setAlignment(Alignment.CENTRE); writableCellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE); writableCellFormat1.setWrap(false); WritableFont cellFont1 = new WritableFont(WritableFont.createFont("宋体"), 14, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); writableCellFormat1.setFont(cellFont1); //数据字体(居中) WritableCellFormat writableCellFormat2 = new WritableCellFormat(); writableCellFormat2.setAlignment(Alignment.CENTRE); writableCellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE); writableCellFormat2.setWrap(true); WritableFont cellFont2 = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); writableCellFormat2.setFont(cellFont2); writableCellFormat2.setBorder(Border.ALL,BorderLineStyle.THIN); WritableSheet sheet = book.createSheet("Sheet1", 0); sheet = setSheetHeader(sheet, writableCellFormat, selValList); int startRow = 2; Label label1 = null; int m = 0; sheet.mergeCells(0, 0, selValList.size()-1, 0); if(indexedCollection!=null && indexedCollection.size()>0) { for (int i = 0; i < indexedCollection.size(); i++){ KeyedCollection keyedCollection = (KeyedCollection) indexedCollection.get(i); String TranDate=(String)keyedCollection.getDataValue("TranDate");//交易日期 String tt = (String)keyedCollection.getDataValue("TranTime"); tt = tt.substring(0,2)+":"+tt.substring(2,4);//交易时间 String narr=utb.nvl(keyedCollection.getDataValue("narr"), "--");//摘要 String entrAmt1=utb.formatMoney("DEBIT".equals((String)(keyedCollection.getDataValue("DCFlag")))"entrAmt")):"0.00");//借方发生额 String entrAmt2=utb.formatMoney("DEBIT".equals((String)(keyedCollection.getDataValue("DCFlag")))"0.00":((String)keyedCollection.getDataValue("entrAmt")));//贷方发生额 String OppAc=utb.nvl(keyedCollection.getDataValue("OppAc"), "--");//对方账号 String OppacName=utb.nvl(keyedCollection.getDataValue("OppacName"), "--");//对方户名 String TransCardNo=utb.nvl(keyedCollection.getDataValue("TransCardNo"), "--");//单位卡号 String stdflag=(String)context.getDataValue("stdflag"); String cardNo=(String)context.getDataValue("parentCardNo"); String mainflag = "附";if(!"parent".equals(stdflag)){}else{mainflag = cardNo.equals((String)(keyedCollection.getDataValue("TransCardNo")))"主":"附";}//主卡标识 String Usage=utb.nvl(keyedCollection.getDataValue("Usage"), "--");//用途 String ReMark=utb.nvl(keyedCollection.getDataValue("ReMark"), "--");//备注 int k = 0; sheet.setRowView(startRow, 450); label1 = new Label(k, startRow, (startRow-1)+"", writableCellFormat2); sheet.addCell(label1); k++; //(列,行,值,格式) label1 = new Label(k, startRow,TranDate, writableCellFormat2); sheet.addCell(label1); k++; label1 = new Label(k, startRow,tt, writableCellFormat2); sheet.addCell(label1); k++; label1 = new Label(k, startRow,narr, writableCellFormat2); sheet.addCell(label1); k++; label1 = new Label(k, startRow,entrAmt1, writableCellFormat2); sheet.addCell(label1); k++; label1 = new Label(k, startRow,entrAmt2, writableCellFormat2); sheet.addCell(label1); k++; label1 = new Label(k, startRow,OppAc, writableCellFormat2); sheet.addCell(label1); k++; label1 = new Label(k, startRow,OppacName, writableCellFormat2); sheet.addCell(label1); k++; label1 = new Label(k, startRow,TransCardNo, writableCellFormat2); sheet.addCell(label1); k++; label1 = new Label(k, startRow,mainflag, writableCellFormat2); sheet.addCell(label1); k++; label1 = new Label(k, startRow,Usage, writableCellFormat2); sheet.addCell(label1); k++; label1 = new Label(k, startRow,ReMark, writableCellFormat2); sheet.addCell(label1); k++; startRow = startRow+1; } } book.write(); try { book.close(); response.flushBuffer(); } catch (Exception e) { System.out.println("Excel is not exist!"); } out = pageContext.pushBody(); } %><%!private static WritableSheet setSheetHeader(WritableSheet sheet,WritableCellFormat writableCellFormat, List selValList) throws Exception { int i = 0; int j = 1; Label label = new Label(i, j, "序号", writableCellFormat); sheet.addCell(label); sheet.setColumnView(i, "序号".length() * 4); for(int w =1; w < selValList.size(); w++) { label = new Label(w, j, ((String)selValList.get(w)).split("#")[0], writableCellFormat); sheet.addCell(label); sheet.setColumnView(w, ((String)selValList.get(w)).split("#")[0].length() * 5); } return sheet; } %>
希望本文所述对大家JSP程序设计有所帮助。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月23日
2024年11月23日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]