Package com.lizi.admin.controller.platform.excel;
Import java.util.List;
Import Java.util.Map;
Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Org.apache.poi.hssf.usermodel.HSSFCellStyle;
Import Org.apache.poi.hssf.usermodel.HSSFFont;
Import Org.apache.poi.hssf.usermodel.HSSFRow;
Import Org.apache.poi.hssf.usermodel.HSSFSheet;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class Excelutil {
Create a Hssfworkbook Workbook object
public static Hssfworkbook Export_text (list<map<string,object>> list,string data) {
try {
Create a Workbook object
Hssfworkbook wb = new Hssfworkbook ();
Create a header row style
Hssfcellstyle Headstyle = Headstyle (WB);
Create a content line style
Hssfcellstyle Contentstyle = Contentstyle (WB);
Create a table
Hssfsheet sheet_1 = wb.createsheet (data+ "Reconciliation Information");
Set the default column width for a table
Sheet_1.setdefaultcolumnwidth (30);
Create a header row
Hssfrow Headrow = sheet_1.createrow (0);
Hssfcell head_cell_1 = Headrow.createcell (0); Create header row first column
Head_cell_1.setcellvalue ("Reconciliation Time"); First column content
Head_cell_1.setcellstyle (Headstyle); Add a header row style
Hssfcell head_cell_2 = Headrow.createcell (1);
Head_cell_2.setcellvalue ("Merchant Total Flow");
Head_cell_2.setcellstyle (Headstyle);
Hssfcell head_cell_3 = Headrow.createcell (2);
Head_cell_3.setcellvalue ("Total number of business pens");
Head_cell_3.setcellstyle (Headstyle);
Hssfcell head_cell_4 = Headrow.createcell (3);
Head_cell_4.setcellvalue ("Actual total flow of receipts");
Head_cell_4.setcellstyle (Headstyle);
Hssfcell head_cell_5 = Headrow.createcell (4);
Head_cell_5.setcellvalue ("Total number of actual receipts");
Head_cell_5.setcellstyle (Headstyle);
Hssfcell head_cell_6 = Headrow.createcell (5);
Head_cell_6.setcellvalue ("Variance amount");
Head_cell_6.setcellstyle (Headstyle);
Hssfcell head_cell_7 = Headrow.createcell (6);
Head_cell_7.setcellvalue ("Difference in number of pens");
Head_cell_7.setcellstyle (Headstyle);
Hssfcell head_cell_8 = Headrow.createcell (7);
Head_cell_8.setcellvalue ("Adjustment amount");
Head_cell_8.setcellstyle (Headstyle);
Hssfcell head_cell_9 = Headrow.createcell (8);
Head_cell_9.setcellvalue ("Number of adjustment pens");
Head_cell_9.setcellstyle (Headstyle);
Hssfcell head_cell_10 = Headrow.createcell (9);
Head_cell_10.setcellvalue ("statement status");
Head_cell_10.setcellstyle (Headstyle);
Add data and styles to a content row
for (int i = 1; I <= list.size (); i++) {
Hssfrow Contentrow = Sheet_1.createrow (i);
Map<string, object> map=list.get (i-1);
String accountstime=map.get ("Accountstime"). ToString ();
String sysmoney=map.get ("Sysmoney"). ToString ();
String sysnum=map.get ("Sysnum"). ToString ();
String passagewaymoney=map.get ("Passagewaymoney"). ToString ();
String passagewaynum=map.get ("Passagewaynum"). ToString ();
String differencemoney=map.get ("Differencemoney"). ToString ();
String differencenumber=map.get ("Differencenumber"). ToString ();
String adjustmentmoney=map.get ("Adjustmentmoney"). ToString ();
String adjustmentnumber=map.get ("Adjustmentnumber"). ToString ();
String state= "";
if (Map.get ("state")!=null) {
if (Map.get ("state"). ToString (). Equals ("0")) {
State= "Normal";
}
if (Map.get ("state"). ToString (). Equals ("1")) {
State= "Not reconciled";
}
if (Map.get ("state"). ToString (). Equals ("2")) {
state= "Reconciled";
}
}
Hssfcell content_cell_1 = Contentrow.createcell (0);
Content_cell_1.setcellvalue (Accountstime);
Content_cell_1.setcellstyle (Contentstyle);
Hssfcell content_cell_2 = Contentrow.createcell (1);
Content_cell_2.setcellvalue (Sysmoney);
Content_cell_2.setcellstyle (Contentstyle);
Hssfcell content_cell_3 = Contentrow.createcell (2);
Content_cell_3.setcellvalue (Sysnum);
Content_cell_3.setcellstyle (Contentstyle);
Hssfcell content_cell_4 = Contentrow.createcell (3);
Content_cell_4.setcellvalue (Passagewaymoney);
Content_cell_4.setcellstyle (Contentstyle);
Hssfcell content_cell_5 = Contentrow.createcell (4);
Content_cell_5.setcellvalue (Passagewaynum);
Content_cell_5.setcellstyle (Contentstyle);
Hssfcell content_cell_6 = Contentrow.createcell (5);
Content_cell_6.setcellvalue (Differencemoney);
Content_cell_6.setcellstyle (Contentstyle);
Hssfcell content_cell_7 = Contentrow.createcell (6);
Content_cell_7.setcellvalue (Differencenumber);
Content_cell_7.setcellstyle (Contentstyle);
Hssfcell content_cell_8 = Contentrow.createcell (7);
Content_cell_8.setcellvalue (Adjustmentmoney);
Content_cell_8.setcellstyle (Contentstyle);
Hssfcell content_cell_9 = Contentrow.createcell (8);
Content_cell_9.setcellvalue (Adjustmentnumber);
Content_cell_9.setcellstyle (Contentstyle);
Hssfcell content_cell_10 = Contentrow.createcell (9);
Content_cell_10.setcellvalue (state);
Content_cell_10.setcellstyle (Contentstyle);
}
return WB;
} catch (Exception e) {
E.getstacktrace ();
}
return null;
}
Create a Hssfworkbook Workbook object
public static Hssfworkbook Export_day (list<map<string,object>> list,string data) {
try {
Create a Workbook object
Hssfworkbook wb = new Hssfworkbook ();
Create a header row style
Hssfcellstyle Headstyle = Headstyle (WB);
Create a content line style
Hssfcellstyle Contentstyle = Contentstyle (WB);
Create a table
Hssfsheet sheet_1 = wb.createsheet (data+ "Reconciliation Information");
Set the default column width for a table
Sheet_1.setdefaultcolumnwidth (30);
Create a header row
Hssfrow Headrow = sheet_1.createrow (0);
Hssfcell head_cell_1 = Headrow.createcell (0); Create header row first column
Head_cell_1.setcellvalue ("Reconciliation Time"); First column content
Head_cell_1.setcellstyle (Headstyle); Add a header row style
Hssfcell head_cell_2 = Headrow.createcell (1);
Head_cell_2.setcellvalue ("Merchant name");
Head_cell_2.setcellstyle (Headstyle);
Hssfcell head_cell_3 = Headrow.createcell (2);
Head_cell_3.setcellvalue ("System order Number");
Head_cell_3.setcellstyle (Headstyle);
Hssfcell head_cell_4 = Headrow.createcell (3);
Head_cell_4.setcellvalue ("System Order Amount");
Head_cell_4.setcellstyle (Headstyle);
Hssfcell head_cell_5 = Headrow.createcell (4);
Head_cell_5.setcellvalue ("Channel order Number");
Head_cell_5.setcellstyle (Headstyle);
Hssfcell head_cell_6 = Headrow.createcell (5);
Head_cell_6.setcellvalue ("Channel Order Amount");
Head_cell_6.setcellstyle (Headstyle);
Hssfcell head_cell_7 = Headrow.createcell (6);
Head_cell_7.setcellvalue ("Order Status");
Head_cell_7.setcellstyle (Headstyle);
Hssfcell head_cell_8 = Headrow.createcell (7);
Head_cell_8.setcellvalue ("order type");
Head_cell_8.setcellstyle (Headstyle);
Hssfcell head_cell_9 = Headrow.createcell (8);
Head_cell_9.setcellvalue ("Trading hours");
Head_cell_9.setcellstyle (Headstyle);
Add data and styles to a content row
for (int i = 1; I <= list.size (); i++) {
Hssfrow Contentrow = Sheet_1.createrow (i);
Map<string, object> map=list.get (i-1);
String accountstime=map.get ("Accountstime"). ToString ();
String shopname=map.get ("Shopname"). ToString ();
String sysno= "";
if (Map.get ("Sysno")!=null) {
Sysno=map.get ("Sysno"). ToString ();
}
String sysmoney= "";
if (Map.get ("Sysmoney")!=null) {
Sysmoney=map.get ("Sysmoney"). ToString ();
}
String passagewayno= "";
if (Map.get ("Passagewayno")!=null) {
Passagewayno=map.get ("Passagewayno"). ToString ();
}
String passagewaymoney= "";
if (Map.get ("Passagewaymoney")!=null) {
Passagewaymoney=map.get ("Passagewaymoney"). ToString ();
}
String ordertype= "";
if (Map.get ("OrderType")!=null) {
int type=integer.valueof (Map.get ("OrderType"). ToString ()). Intvalue ();
Switch (type) {//0 normal 1 order missing 2 channel missing 3 amount incorrect
Case 0:
Ordertype= "Normal";
Break
Case 1:
Ordertype= "Order Missing";
Break
Case 2:
Ordertype= "channel missing";
Break
Default
Ordertype= "The amount is incorrect";
Break
}
}
String orderstate= "";
if (Map.get ("Orderstate")!=null) {
int state=integer.valueof (Map.get ("Orderstate"). ToString ()). Intvalue ();
Switch (state) {//0 Normal 1 unposted 2 reconciled 3 adjusted
Case 0:
Orderstate= "Normal";
Break
Case 1:
Orderstate= "Not reconciled";
Break
Case 2:
orderstate= "Reconciled";
Break
Default
Orderstate= "Adjusted account";
Break
}
}
String transactiontime=map.get ("Transactiontime"). ToString ();
Hssfcell content_cell_1 = Contentrow.createcell (0);
Content_cell_1.setcellvalue (Accountstime);
Content_cell_1.setcellstyle (Contentstyle);
Hssfcell content_cell_2 = Contentrow.createcell (1);
Content_cell_2.setcellvalue (Shopname);
Content_cell_2.setcellstyle (Contentstyle);
Hssfcell content_cell_3 = Contentrow.createcell (2);
Content_cell_3.setcellvalue (SYSNO);
Content_cell_3.setcellstyle (Contentstyle);
Hssfcell content_cell_4 = Contentrow.createcell (3);
Content_cell_4.setcellvalue (Sysmoney);
Content_cell_4.setcellstyle (Contentstyle);
Hssfcell content_cell_5 = Contentrow.createcell (4);
Content_cell_5.setcellvalue (Passagewayno);
Content_cell_5.setcellstyle (Contentstyle);
Hssfcell content_cell_6 = Contentrow.createcell (5);
Content_cell_6.setcellvalue (Passagewaymoney);
Content_cell_6.setcellstyle (Contentstyle);
Hssfcell content_cell_7 = Contentrow.createcell (6);
Content_cell_7.setcellvalue (OrderType);
Content_cell_7.setcellstyle (Contentstyle);
Hssfcell content_cell_8 = Contentrow.createcell (7);
Content_cell_8.setcellvalue (orderstate);
Content_cell_8.setcellstyle (Contentstyle);
Hssfcell content_cell_9 = Contentrow.createcell (8);
Content_cell_9.setcellvalue (Transactiontime);
Content_cell_9.setcellstyle (Contentstyle);
}
return WB;
} catch (Exception e) {
E.getstacktrace ();
}
return null;
}
/**
* Create header row style
* @param WB
* @return
*/
public static Hssfcellstyle Headstyle (Hssfworkbook WB) {
Hssfcellstyle Headstyle = Wb.createcellstyle (); Create a Style object
Hssffont Headfont = Wb.createfont (); Creating fonts
Headfont.setfontname ("Microsoft Jas Black");
Headfont.setboldweight (Hssffont.boldweight_bold);
Headfont.setcolor (hssffont.color_red);
Headstyle.setalignment (Hssfcellstyle.align_center);
Headstyle.setfont (Headfont);
return headstyle;
}
/**
* Create a content line style
* @param WB
* @return
*/
public static Hssfcellstyle Contentstyle (Hssfworkbook WB) {
Hssfcellstyle Contentstyle = Wb.createcellstyle ();
Hssffont Contentfont = Wb.createfont ();
Contentfont.setfontname ("Microsoft Jas Black");
Contentfont.setboldweight (Hssffont.boldweight_normal);
Contentfont.setcolor (Hssffont.color_normal);
Contentstyle.setalignment (Hssfcellstyle.align_center);
Contentstyle.setfont (Contentfont);
return contentstyle;
}
}
/**
* Statement of the date of export
* @param model
* @param request
* @param response
* @param session
* @param paytype
* @param date
* @param type
* @return
*/
@ResponseBody
@RequestMapping (value = "/financialcenter/exportbyym")
Public Modelandview Exportbyym (model model, HttpServletRequest request,httpservletresponse response, HttpSession Session
@RequestParam (value = "PayType", required = True) String PayType,
@RequestParam (value = "date", required = true), String date,
@RequestParam (value = "type", required = True) String type) {
try {
if (Stringutils.isnotblank (PayType)) {
if (Paytype.equals ("1")) {
Paytype= "7";
}
if (Paytype.equals ("2")) {
Paytype= "6";
}
if (Paytype.equals ("3")) {
Paytype= "4";
}
}
criteria = new criteria ();
Criteria.getcondition (). Put ("state", "");
Criteria.getcondition (). Put ("type", PayType);
Sessionuser user = Getsessionuser (session);
TUSER TUSER = This.tUSERService.queryById (User.getuserid ());
Integer agentid=0;
if (Tuser.gettype (). Equals (systemconfig.user_type_agent_t)) {
Agentid=tuser.getagentid ();
}
Criteria.getcondition (). Put ("Agentid", Agentid);
List<map<string,object>> Maps=null;
if (Type.equals ("1")) {//year
Criteria.getcondition (). Put ("year", date);
Maps=this.adjustservice.getorderbyyearandtype (criteria);
}
if (Type.equals ("2")) {//month
Criteria.getcondition (). Put ("date", date);
Maps=this.adjustservice.getorderbymonthandtype (criteria);
}
if (Type.equals ("3")) {//day
Criteria.getcondition (). Put ("Day", date);
Maps=this.adjustservice.getorderbydayandtype (criteria);
}
if (Maps!=null&&maps.size () >0) {
Calling a method to create a Hssfworkbook workbook object
Hssfworkbook wb = null;
if (Type.equals ("3")) {//day
WB = Excelutil.export_day (maps,date);
}else{
WB = Excelutil.export_text (maps,date);
}
try {
Define the name of the export file, do not understand the students can first understand the file download
Random r = new Random ();
int n2 = r.nextint (1000);
String name=date+ "_" +n2+ ". xls";
String fileName = new String (Name.getbytes ("UTF-8"), "iso-8859-1");
Response.setcontenttype ("application/vnd.ms-excel");
Response.setheader ("Content-disposition", "attachment; filename=" +filename);
OutputStream OS = Response.getoutputstream ();
Writing a workbook to the output stream
Wb.write (OS);
Os.close ();
} catch (Exception e) {
E.getstacktrace ();
}
}else{
Model.addattribute (Errormsg.key_code, Errormsg.call_fail);
Model.addattribute (Errormsg.key_message, date+ "No transaction data for this date");
return new Modelandview (new Mappingjacksonjsonview ());
}
} catch (Exception e) {
E.printstacktrace ();
Model.addattribute (Errormsg.key_code, Errormsg.call_fail);
Model.addattribute (Errormsg.key_message), "The Inquiry failed!" ");
}
return new Modelandview (new Mappingjacksonjsonview ());
}
/**
* Export Excel
*/
Exports.exportexcel = function (Me) {
var year = $CMT _year_details.param.year;
var paytype = $CMT _year_details.paytype;
var type = 1;
Confirmdialog ("Export hint", "OK to export excel?") ", {
Okaction:function () {
$ ("#" + $CMT _year_details.page_id). Loading (' show ');
location.href= $cmt _year_details.config.exportbyym+ ' &date= ' +year+ ' &paytype= ' +payType+ ' &type= ' +type;
$ ("#" + $CMT _year_details.page_id). Loading (' hide ');
}
});
}
Java data is imported into exc and downloaded