国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP導出Excel文件的方法

2024-09-05 00:22:23
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了JSP導出Excel文件的方法,涉及JSP表格操作及excel文件的生成技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了JSP導出Excel文件的方法。分享給大家供大家參考,具體如下:

 

 
  1. <%@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"%> 
  2. <%@page language="java" contentType="text/html; charset=UTF-8"%> 
  3. <%@taglib uri="/WEB-INF/CTP.tld" prefix="ctp"%> 
  4. <jsp:directive.page import="java.util.List"/> 
  5. <jsp:directive.page import="java.util.ArrayList"/> 
  6. <%@ include file="http://www.xxxx.com/head.jsp" %> 
  7. <% 
  8. Context context = (Context)request.getAttribute("context");String flag ="交易日期,交易時間,摘要,借方發生額,貸方發生額,對方賬號,對方戶名,單位卡號,主卡標識,用途,備注"
  9. IndexedCollection indexedCollection = (IndexedCollection)context.getDataElement("parentCardDetails"); 
  10. String[] sourceStrArray=flag.split(","); 
  11. List selValList = new ArrayList(); 
  12. selValList.add("序號#"); 
  13. for(int i=0;i<sourceStrArray.length;i++){ 
  14. selValList.add(sourceStrArray[i]+"#"); 
  15. WritableWorkbook book = null
  16. if (indexedCollection == null || indexedCollection.size()<0)  
  17. {%> 
  18. <script language="javascript"
  19. alert('對不起!當前條件沒有數據,不能導出!'); 
  20. //history.go(-1); 
  21. </script> 
  22. <% 
  23. else 
  24. response.reset(); 
  25. response.setContentType("charset=UTF-8"); 
  26. response.setContentType("application/vnd.ms-excel"); 
  27. String filename = "regularInform.xls"
  28. response.setHeader("Content-Disposition","attachment; filename=" + filename); 
  29. //解決HTTPS不能下載的問題 
  30. response.setHeader("Cache-Control","public"); 
  31. book = Workbook.createWorkbook(response.getOutputStream()); 
  32. //表頭字體 
  33. WritableCellFormat writableCellFormat = new WritableCellFormat(); 
  34. writableCellFormat.setAlignment(Alignment.CENTRE); 
  35. writableCellFormat.setVerticalAlignment(VerticalAlignment.CENTRE); 
  36. writableCellFormat.setWrap(true); 
  37. WritableFont cellFont = new WritableFont(WritableFont.createFont("宋體"), 14, WritableFont.BOLD, false
  38. UnderlineStyle.NO_UNDERLINE, Colour.BLACK); 
  39. writableCellFormat.setFont(cellFont); 
  40. writableCellFormat.setBorder(Border.ALL, 
  41. BorderLineStyle.THIN); 
  42. writableCellFormat.setBackground(Colour.SKY_BLUE, 
  43. Pattern.SOLID); 
  44. //標題字體(左對齊) 
  45. WritableCellFormat writableCellFormat1 = new WritableCellFormat(); 
  46. writableCellFormat1.setAlignment(Alignment.CENTRE); 
  47. writableCellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE); 
  48. writableCellFormat1.setWrap(false); 
  49. WritableFont cellFont1 = new WritableFont(WritableFont.createFont("宋體"), 14, WritableFont.NO_BOLD, false
  50. UnderlineStyle.NO_UNDERLINE, Colour.BLACK); 
  51. writableCellFormat1.setFont(cellFont1); 
  52. //數據字體(居中) 
  53. WritableCellFormat writableCellFormat2 = new WritableCellFormat(); 
  54. writableCellFormat2.setAlignment(Alignment.CENTRE); 
  55. writableCellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE); 
  56. writableCellFormat2.setWrap(true); 
  57. WritableFont cellFont2 = new WritableFont(WritableFont.createFont("宋體"), 10, WritableFont.NO_BOLD, false
  58. UnderlineStyle.NO_UNDERLINE, Colour.BLACK); 
  59. writableCellFormat2.setFont(cellFont2); 
  60. writableCellFormat2.setBorder(Border.ALL,BorderLineStyle.THIN); 
  61. WritableSheet sheet = book.createSheet("Sheet1", 0); 
  62. sheet = setSheetHeader(sheet, writableCellFormat, selValList); 
  63. int startRow = 2; 
  64. Label label1 = null
  65. int m = 0; 
  66. sheet.mergeCells(0, 0, selValList.size()-1, 0); 
  67. if(indexedCollection!=null && indexedCollection.size()>0) 
  68. {  
  69. for (int i = 0; i < indexedCollection.size(); i++){  
  70. KeyedCollection keyedCollection = (KeyedCollection) indexedCollection.get(i); 
  71. String TranDate=(String)keyedCollection.getDataValue("TranDate");//交易日期 
  72. String tt = (String)keyedCollection.getDataValue("TranTime"); 
  73. tt = tt.substring(0,2)+":"+tt.substring(2,4);//交易時間 
  74. String narr=utb.nvl(keyedCollection.getDataValue("narr"), "--");//摘要 
  75. String entrAmt1=utb.formatMoney("DEBIT".equals((String)(keyedCollection.getDataValue("DCFlag")))?((String)keyedCollection.getDataValue("entrAmt")):"0.00");//借方發生額 
  76. String entrAmt2=utb.formatMoney("DEBIT".equals((String)(keyedCollection.getDataValue("DCFlag")))?"0.00":((String)keyedCollection.getDataValue("entrAmt")));//貸方發生額 
  77. String OppAc=utb.nvl(keyedCollection.getDataValue("OppAc"), "--");//對方賬號 
  78. String OppacName=utb.nvl(keyedCollection.getDataValue("OppacName"), "--");//對方戶名 
  79. String TransCardNo=utb.nvl(keyedCollection.getDataValue("TransCardNo"), "--");//單位卡號 
  80. String stdflag=(String)context.getDataValue("stdflag"); 
  81. String cardNo=(String)context.getDataValue("parentCardNo"); 
  82. String mainflag = "附";if(!"parent".equals(stdflag)){}else{mainflag = cardNo.equals((String)(keyedCollection.getDataValue("TransCardNo")))?"主":"附";}//主卡標識 
  83. String Usage=utb.nvl(keyedCollection.getDataValue("Usage"), "--");//用途 
  84. String ReMark=utb.nvl(keyedCollection.getDataValue("ReMark"), "--");//備注 
  85. int k = 0; 
  86. sheet.setRowView(startRow, 450); 
  87. label1 = new Label(k, startRow, (startRow-1)+"", writableCellFormat2); 
  88. sheet.addCell(label1); 
  89. k++; 
  90. //(列,行,值,格式) 
  91. label1 = new Label(k, startRow,TranDate, writableCellFormat2); 
  92. sheet.addCell(label1); 
  93. k++; 
  94. label1 = new Label(k, startRow,tt, writableCellFormat2); 
  95. sheet.addCell(label1); 
  96. k++; 
  97. label1 = new Label(k, startRow,narr, writableCellFormat2); 
  98. sheet.addCell(label1); 
  99. k++; 
  100. label1 = new Label(k, startRow,entrAmt1, writableCellFormat2); 
  101. sheet.addCell(label1); 
  102. k++; 
  103. label1 = new Label(k, startRow,entrAmt2, writableCellFormat2); 
  104. sheet.addCell(label1); 
  105. k++; 
  106. label1 = new Label(k, startRow,OppAc, writableCellFormat2); 
  107. sheet.addCell(label1); 
  108. k++; 
  109. label1 = new Label(k, startRow,OppacName, writableCellFormat2); 
  110. sheet.addCell(label1); 
  111. k++; 
  112. label1 = new Label(k, startRow,TransCardNo, writableCellFormat2); 
  113. sheet.addCell(label1); 
  114. k++; 
  115. label1 = new Label(k, startRow,mainflag, writableCellFormat2); 
  116. sheet.addCell(label1); 
  117. k++; 
  118. label1 = new Label(k, startRow,Usage, writableCellFormat2); 
  119. sheet.addCell(label1); 
  120. k++; 
  121. label1 = new Label(k, startRow,ReMark, writableCellFormat2); 
  122. sheet.addCell(label1); 
  123. k++; 
  124. startRow = startRow+1; 
  125. book.write(); 
  126. try { 
  127. book.close(); 
  128. response.flushBuffer(); 
  129. catch (Exception e) { 
  130. System.out.println("Excel is not exist!"); 
  131. out = pageContext.pushBody(); 
  132. %><%!private static WritableSheet setSheetHeader(WritableSheet sheet,WritableCellFormat writableCellFormat, List selValList) throws Exception  
  133. int i = 0; 
  134. int j = 1; 
  135. Label label = new Label(i, j, "序號", writableCellFormat); 
  136. sheet.addCell(label); 
  137. sheet.setColumnView(i, "序號".length() * 4); 
  138. for(int w =1; w < selValList.size(); w++) 
  139. label = new Label(w, j, ((String)selValList.get(w)).split("#")[0], writableCellFormat); 
  140. sheet.addCell(label); 
  141. sheet.setColumnView(w, ((String)selValList.get(w)).split("#")[0].length() * 5); 
  142. return sheet; 
  143. %> 

希望本文所述對大家JSP程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台北县| 张家港市| 得荣县| 宝兴县| 依安县| 阿拉善右旗| 曲沃县| 桂阳县| 阜平县| 常宁市| 永川市| 景东| 桃园市| 崇信县| 黔南| 临夏县| 噶尔县| 贵定县| 高淳县| 襄汾县| 鄄城县| 汪清县| 商都县| 唐河县| 台中市| 泸定县| 白沙| 安国市| 石泉县| 松桃| 普洱| 华阴市| 江城| 江门市| 元谋县| 西乌珠穆沁旗| 济源市| 阜新| 湘乡市| 云安县| 大荔县|