JSP export Excel file Method _jsp programming

Source: Internet
Author: User

The example in this article describes how JSP exports Excel files. Share to everyone for your reference, specific as follows:

<% @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) Request.getattribute (" context ");
 String flag = "Transaction date, transaction time, summary, debit occurrence, credit occurrence, other account, username, unit card number, main card identification, use, remark";
 Indexedcollection indexedcollection = (indexedcollection) context.getdataelement ("Parentcarddetails");
 String[] Sourcestrarray=flag.split (",");
 List selvallist = new ArrayList ();
 Selvallist.add ("Serial number #");
 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 (' Sorry! The current condition has no data, cannot be exported! ');
 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);
  Resolve issues that HTTPS cannot download Response.setheader ("Cache-control", "public");
  Book = Workbook.createworkbook (Response.getoutputstream ());
  Header font Writablecellformat Writablecellformat = new Writablecellformat ();
  Writablecellformat.setalignment (Alignment.centre);
  Writablecellformat.setverticalalignment (Verticalalignment.centre);
  Writablecellformat.setwrap (TRUE);
  Writablefont Cellfont = new Writablefont (Writablefont.createfont ("XXFarEastFont-Arial"), Writablefont.bold, False,
  Underlinestyle.no_underline, Colour.black);
  Writablecellformat.setfont (Cellfont);
  Writablecellformat.setborder (Border.all,Borderlinestyle.thin);
  Writablecellformat.setbackground (Colour.sky_blue, pattern.solid);
  Title Font (left-aligned) Writablecellformat WRITABLECELLFORMAT1 = new Writablecellformat ();
  Writablecellformat1.setalignment (Alignment.centre);
  Writablecellformat1.setverticalalignment (Verticalalignment.centre);
  Writablecellformat1.setwrap (FALSE);
  Writablefont cellFont1 = new Writablefont (Writablefont.createfont ("XXFarEastFont-Arial"), Writablefont.no_bold, False,
  Underlinestyle.no_underline, Colour.black);
  Writablecellformat1.setfont (CELLFONT1);
  Data font (centered) Writablecellformat writableCellFormat2 = new Writablecellformat ();
  Writablecellformat2.setalignment (Alignment.centre);
  Writablecellformat2.setverticalalignment (Verticalalignment.centre);
  Writablecellformat2.setwrap (TRUE);
  Writablefont CellFont2 = new Writablefont (Writablefont.createfont ("XXFarEastFont-Arial"), 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");//transaction date String TT = (String) keyedcollection.getdat
        Avalue ("Trantime"); tt = tt.substring (0,2) + ":" +tt.substring (2,4);//Transaction Time String NARR=UTB.NVL (Keyedcollection.getdatavalue ("Narr"), "--" )//Summary String Entramt1=utb.formatmoney ("DEBIT". Equals ((String) (Keyedcollection.getdatavalue ("Dcflag"))? ( (string) keyedcollection.getdatavalue ("Entramt")): "0.00")//Debit occurrence String Entramt2=utb.formatmoney ("DEBIT". equALS ((String) (Keyedcollection.getdatavalue ("Dcflag"))? " 0.00 ":((String) keyedcollection.getdatavalue (" Entramt "));//Credit Occurrence String OPPAC=UTB.NVL ( Keyedcollection.getdatavalue ("Oppac"), "--");//Offset account String OPPACNAME=UTB.NVL (Keyedcollection.getdatavalue ("OppacN Ame "),"--")//The other user name String TRANSCARDNO=UTB.NVL (Keyedcollection.getdatavalue (" Transcardno "),"--");//unit card number S
        Tring stdflag= (String) context.getdatavalue ("Stdflag");
        String cardno= (String) context.getdatavalue ("Parentcardno"); String Mainflag = "attached"; if (!) Parent ". Equals (Stdflag)) {}else{mainflag = Cardno.equals ((String) (Keyedcollection.getdatavalue (" Transcardno "))?" Main ":" Attached ";} The main card identifies string Usage=utb.nvl (Keyedcollection.getdatavalue ("Usage"), "--");/use string REMARK=UTB.NVL (Keyedc
       Ollection.getdatavalue ("remark"), "--");/Note int k = 0;
       Sheet.setrowview (StartRow, 450);
       Label1 = new Label (k, StartRow, (startRow-1) + "", WRITABLECELLFORMAT2); Sheet.addcell (Label1);
       k++;
       (column, row, value, format) 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, "ordinal", Writablecellformat);
  Sheet.addcell (label);
  Sheet.setcolumnview (i, "serial number". Length () * 4); for (int w =1 W < selvallist.size (); w++) {label = new label (W, J, ((String) Selvallist.get (w)). Split ("#") [0], WRI
   Tablecellformat);
   Sheet.addcell (label); Sheet.setcolumnview (W, (String) selVallist.get (W)). Split ("#") [0].length () * 5);
 return sheet;

 }%>

I hope this article will help you with JSP programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.