Export Excel files using Java jxl

Source: Internet
Author: User

Two days ago, my colleagues wrote a piece of Excel export code using Java jxl, which is worth learning. The Code is as follows: (export a list to iterator)
First export jxl. jar, and then use the following method:
/**
* @ Param Mapping
* @ Param form
* @ Param request
* @ Param response
* @ Return actionforward: Obtain the entered start and end customer code, query the settlement list, and return the result.
*/
Public void doexportasxls (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response ){
String usercodebegin = request. getparameter ("ucodebegin ");
String usercodeend = request. getparameter ("ucodeend ");
Log.info ("usercodebegin" + usercodebegin. tostring ());
Log.info ("usercodeend" + usercodeend. tostring ());
If (usercodebegin = NULL | usercodebegin. Equals ("")){
Usercodebegin = usercodeend;
}
If (usercodeend = NULL | usercodeend. Equals ("")){
Usercodeend = usercodebegin;
}
List balancelist = settleservice. getsettlebill (usercodebegin,
Usercodeend );

Outputstream OS = NULL;
Writableworkbook WWB = NULL;
Date = new date ();
Format = new simpledateformat ("yyyymmdd ");
String strdate = format. Format (date );
Try {

OS = new fileoutputstream (strdate + "settle.xls ");
WWB = Workbook. createworkbook (OS );
Iterator it = balancelist. iterator ();
Doconfigexcel (it, WWB );
} Catch (exception e ){

} Finally {
Try {
WWB. Write ();
WWB. Close ();
OS. Close ();
} Catch (exception e ){
E. printstacktrace ();
}

}

Response. setheader ("content-disposition", "attachment; filename ="
+ Strdate + "settle.xls /"");
Response. setcontenttype ("application/MSExcel ");
Try {
Fileinputstream = new fileinputstream (strdate
+ "Settle.xls ");
Outputstream out = response. getoutputstream ();
Int I = 0;
While (I = fileinputstream. Read ())! =-1 ){
Out. Write (I );
}
Fileinputstream. Close ();
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}

}

Private void doconfigexcel (iterator it, writableworkbook WWB ){


Try {


While (it. hasnext ()){
Label lchild;
Settlevo SV = (settlevo) it. Next ();
String usercode = SV. getusercode ();
Int I = 2;
Int J = 0;
Writablefont WF = new writablefont (writablefont. Times, 18, writablefont. Bold, true );
Writablecellformat wcff = new writablecellformat (WF );

Writablesheet Ws = WWB. createsheet (usercode, 0 );
Label labelmain = new label (4, 0, "China corn starch network-customer settlement list", wcff );

Label labelmain1 = new label (4, 1, "customer settlement list", wcff );
WS. addcell (labelmain );
WS. addcell (labelmain1 );
Lchild = new label (0, 2, "Customer Code ");


WS. addcell (lchild );

Lchild = new label (0, 3, usercode );
WS. addcell (lchild );

String code = SV. getcode ();
Lchild = new label (2, 2, "booth Code ");
WS. addcell (lchild );
Lchild = new label (2, 3, Code );
WS. addcell (lchild );

String fdate = SV. getformatdate ();
Lchild = new label (4, 2, "date ");
WS. addcell (lchild );
Lchild = new label (4, 3, fdate );
WS. addcell (lchild );

Lchild = new label (4, 4, "daily transaction list", wcff );
WS. addcell (lchild );

Iterator its = SV. getorders (). iterator ();
Lchild = new label (0, 5, "Sn ");
WS. addcell (lchild );
Lchild = new label (2, 5, "time of delivery ");
WS. addcell (lchild );
Lchild = new label (4, 5, "market ");
WS. addcell (lchild );
Lchild = new label (6, 5, "contract name ");
WS. addcell (lchild );
Lchild = new label (8, 5, "Deal time ");
WS. addcell (lchild );
Lchild = new label (10, 5, "Internet access time ");
WS. addcell (lchild );
Lchild = new label (12, 5, "price ");
WS. addcell (lchild );
Lchild = new label (14, 5, "quantity ");
WS. addcell (lchild );
Lchild = new label (16, 5, "class ");
WS. addcell (lchild );
Int II = 6;

Bigdecimal totaldealprice = new bigdecimal (0 );
Long totalamount = new long (0 );
While (its. hasnext ()){
II ++;
Tspotorder order = (tspotorder) Its. Next ();

String ordercode = order. getordercode ();
Lchild = new label (0, II, ordercode );
WS. addcell (lchild );

String tradetime = order. getlasttradetime (). tostring ();
Lchild = new label (2, II, tradetime );
WS. addcell (lchild );

String exchange = order. getexchange ();
Lchild = new label (4, II, exchange );
WS. addcell (lchild );

String stockname = order. getfuturesstockbase ()
. Getstockname ();
Lchild = new label (6, II, stockname );
WS. addcell (lchild );

String dealtime = order. getformatedealtime ();
Lchild = new label (8, II, dealtime );
WS. addcell (lchild );

Lchild = new label (10, II ,"-");
WS. addcell (lchild );

String dealprice = order. getdealprice (). tostring ();

Lchild = new label (12, II, dealprice );
WS. addcell (lchild );

Totaldealprice = totaldealprice. Add (order. getdealprice ());

String amount = string. valueof (order. getamount ());

Lchild = new label (14, II, amount );
WS. addcell (lchild );

Totalamount = totalamount + order. getamount ();

String buy = order. getbuyformat ();

Lchild = new label (16, II, buy );
WS. addcell (lchild );

}
Lchild = new label (0, II + 1, "Total ");
WS. addcell (lchild );
Lchild = new label (12, II + 1, totaldealprice. tostring ());
WS. addcell (lchild );
Lchild = new label (14, II + 1, totalamount. tostring ());
WS. addcell (lchild );


Int JJ = II + 3;
Int jjj = JJ + 2;
Lchild = new label (4, II + 2, "Order table", wcff );
WS. addcell (lchild );

Lchild = new label (0, JJ, "month of delivery ");
WS. addcell (lchild );
Lchild = new label (2, JJ, "market ");
WS. addcell (lchild );
Lchild = new label (4, JJ, "contract name ");
WS. addcell (lchild );
Lchild = new label (6, JJ, "Buy/sell ");
WS. addcell (lchild );
Lchild = new label (8, JJ, "average price ");
WS. addcell (lchild );
Lchild = new label (10, JJ, "quantity ");
WS. addcell (lchild );
Lchild = new label (12, JJ, "deposit ");
WS. addcell (lchild );
Lchild = new label (14, JJ, "floating loss ");
WS. addcell (lchild );

Bigdecimal totalavuplice = new bigdecimal (0 );
Long totalavgamount = new long (0 );
Bigdecimal totaldeposit = new bigdecimal (0 );
Bigdecimal totalproloss = new bigdecimal (0 );

Iterator _ its = SV. getholds (). iterator ();
While (its. hasnext ()){
Jjj ++;
Holdvo4settle hold = (holdvo4settle) _ its. Next ();

String month = hold. getdelivermonth ();
Lchild = new label (0, jjj, month );
WS. addcell (lchild );

String market = hold. getmarket ();
Lchild = new label (2, jjj, market );
WS. addcell (lchild );

String ordername = hold. getordername ();
Lchild = new label (4, jjj, ordername );
WS. addcell (lchild );

String buyformat = hold. getbuyformat ();
Lchild = new label (6, jjj, buyformat );
WS. addcell (lchild );

String avuplice = hold. getprice (). tostring ();
Lchild = new label (8, jjj, avuplice );
WS. addcell (lchild );

Totalavuplice = totalavuplice. Add (hold. getprice ());

String avgamount = hold. getamount (). tostring ();
Lchild = new label (10, jjj, avgamount );
WS. addcell (lchild );
 
Totalavgamount = totalavgamount + hold. getamount ();

String deposit = hold. getdeposit (). tostring ();
Lchild = new label (12, jjj, deposit );
WS. addcell (lchild );

Totaldeposit = totaldeposit. Add (hold. getdeposit ());

String proloss = hold. getpro_loss (). tostring ();
Lchild = new label (14, jjj, proloss );
WS. addcell (lchild );

Totalproloss = totalproloss. Add (hold. getpro_loss ());

}

Lchild = new label (0, jjj, "Total ");
WS. addcell (lchild );

Lchild = new label (8, jjj, totalavuplice. tostring ());
WS. addcell (lchild );

Lchild = new label (10, jjj, totalavgamount. tostring ());
WS. addcell (lchild );

Lchild = new label (12, jjj, totaldeposit. tostring ());
WS. addcell (lchild );

Lchild = new label (14, jjj, totalproloss. tostring ());
WS. addcell (lchild );


Lchild = new label (4, jjj + 2, "Fund Statement", wcff );
WS. addcell (lchild );

Bigdecimal capital = SV. getaddcapital ();
Lchild = new label (0, jjj + 3, "+ save on the same day ");
WS. addcell (lchild );
Lchild = new label (6, jjj + 3, "-daily expenditure ");
WS. addcell (lchild );
Lchild = new label (0, jjj + 4, "+ trading profit and loss of the Day ");
WS. addcell (lchild );
Lchild = new label (6, jjj + 4, "-Transaction Service Fee ");
WS. addcell (lchild );

If (capital! = NULL ){
Lchild = new label (2, jjj + 3, capital. tostring ());
WS. addcell (lchild );
}

Bigdecimal redcapital = SV. getequalcecaploud ();
If (redcapital! = NULL ){
Lchild = new label (8, jjj + 3, redcapital. tostring ());
WS. addcell (lchild );
}

Bigdecimal pro_loss = SV. getpro_loss ();
If (pro_loss! = NULL ){
Lchild = new label (2, jjj + 4, pro_loss.tostring ());
WS. addcell (lchild );

}

Bigdecimal numeric = SV. getnumeric ();
If (response! = NULL ){
Lchild = new label (8, jjj + 4, response. tostring ());
WS. addcell (lchild );
}


}
} Catch (rowsexceededexception e ){

E. printstacktrace ();
} Catch (writeexception e ){
E. printstacktrace ();
}
}

 

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.