主頁 > 知識庫 > JSP導(dǎo)出Excel文件的方法

JSP導(dǎo)出Excel文件的方法

熱門標(biāo)簽:科大訊飛語音識別系統(tǒng) 團(tuán)購網(wǎng)站 阿里云 Mysql連接數(shù)設(shè)置 Linux服務(wù)器 服務(wù)器配置 電子圍欄 銀行業(yè)務(wù)

本文實(shí)例講述了JSP導(dǎo)出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 ="交易日期,交易時(shí)間,摘要,借方發(fā)生額,貸方發(fā)生額,對方賬號,對方戶名,單位卡號,主卡標(biāo)識,用途,備注";
 IndexedCollection indexedCollection = (IndexedCollection)context.getDataElement("parentCardDetails");
 String[] sourceStrArray=flag.split(",");
 List selValList = new ArrayList();
 selValList.add("序號#");
 for(int i=0;isourceStrArray.length;i++){
  selValList.add(sourceStrArray[i]+"#");
 }
 WritableWorkbook book = null;
 if (indexedCollection == null || indexedCollection.size()0) 
 {%>
 script language="javascript">
  alert('對不起!當(dāng)前條件沒有數(shù)據(jù),不能導(dǎo)出!');
  //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);
  //標(biāo)題字體(左對齊)
  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);
  //數(shù)據(jù)字體(居中)
  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);//交易時(shí)間
        String narr=utb.nvl(keyedCollection.getDataValue("narr"), "--");//摘要
        String entrAmt1=utb.formatMoney("DEBIT".equals((String)(keyedCollection.getDataValue("DCFlag")))?((String)keyedCollection.getDataValue("entrAmt")):"0.00");//借方發(fā)生額
        String entrAmt2=utb.formatMoney("DEBIT".equals((String)(keyedCollection.getDataValue("DCFlag")))?"0.00":((String)keyedCollection.getDataValue("entrAmt")));//貸方發(fā)生額
        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")))?"主":"附";}//主卡標(biāo)識
        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程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • jsp頁面中顯示word/excel格式的文檔的方法
  • JSP上傳excel及excel插入至數(shù)據(jù)庫的方法
  • JSP實(shí)現(xiàn)從數(shù)據(jù)庫導(dǎo)出數(shù)據(jù)到Excel下載的方法
  • jsp實(shí)現(xiàn)針對excel及word文檔的打印方法
  • jsp導(dǎo)出excel并支持分sheet導(dǎo)出的方法
  • jsp利用POI生成Excel并在頁面中導(dǎo)出的示例

標(biāo)簽:蚌埠 衢州 大理 廣元 棗莊 萍鄉(xiāng) 衡水 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP導(dǎo)出Excel文件的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266