jxl 创建excel

public static void performanceExcel(String sCurrPath,List<Record> list, String begin,String end) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat(("yyyy-MM-dd HH:mm:ss"));
try {
//** **********创建工作簿************ *//*
WritableWorkbook workbook = Workbook.createWorkbook(new File(sCurrPath));
//** **********创建工作表************ *//*
WritableSheet sheet = workbook.createSheet("test", 0);
//** **********设置页眉、页脚***************** *//*
sheet.setHeader("页眉", "", "第 &P 页,共 &N 页"); // 设置页眉
sheet.setFooter("", "", "&D &T"); // 设置页脚
//** ****注:以上这些&P、&D等参数可以从EXCEL的宏录制取得******* *//*
//** *********设置列宽**************** *//*
sheet.setColumnView(0, 25); // 第1列
sheet.setColumnView(1, 20);// 第2列
sheet.setColumnView(2, 20);// 第3列
sheet.setColumnView(3, 20);// 第4列
sheet.setColumnView(4, 25); // 第5列
sheet.setColumnView(5, 15); // 第6列
sheet.setColumnView(6, 15); // 第7列
sheet.setColumnView(7, 15); // 第8列
sheet.setColumnView(8, 20);
sheet.setColumnView(9, 20);
sheet.setColumnView(10, 20);
sheet.setColumnView(11, 20);

sheet.setColumnView(12, 20);
sheet.setColumnView(13, 20);
sheet.setColumnView(14, 20);
sheet.setColumnView(15, 20);
sheet.setColumnView(16, 20);
sheet.setColumnView(17, 20);
sheet.setColumnView(18, 20);
sheet.setColumnView(19, 20);
sheet.setColumnView(20, 20);
sheet.getSettings().setVerticalFreeze(2);//冻结前两行
// sheet.getSettings().setHorizontalFreeze(2);//冻结前两列





//** ************设置单元格字体************** *//*
WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
WritableFont BoldFont12 = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD);//加粗12号字体
WritableFont BoldFont20 = new WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD);////加粗18号字体

//** ************以下设置几种格式的单元格************ *//*
//用于台头
WritableCellFormat wcf_heading = new WritableCellFormat(BoldFont20);
wcf_heading.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_heading.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_heading.setAlignment(Alignment.CENTRE); // 水平对齐
wcf_heading.setWrap(false); // 是否换行
wcf_heading.setBackground(Colour.GRAY_25);

// 用于标题
WritableCellFormat wcf_title = new WritableCellFormat(BoldFont12);
wcf_title.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_title.setAlignment(Alignment.CENTRE); // 水平对齐
wcf_title.setWrap(false); // 是否换行


// 用于跨行
WritableCellFormat wcf_merge = new WritableCellFormat(NormalFont);
wcf_merge.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_merge.setVerticalAlignment(VerticalAlignment.TOP); // 垂直对齐
wcf_merge.setAlignment(Alignment.LEFT);
wcf_merge.setWrap(true); // 是否换行
// 用于正文
NumberFormat nf = new jxl.write.NumberFormat("0.00"); //设定带小数点数字格式
WritableCellFormat wcfN = new jxl.write.WritableCellFormat(NormalFont, nf);//设定带小数数字单元格格式
wcfN.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcfN.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcfN.setAlignment(Alignment.CENTRE); // 水平对齐
wcf_merge.setWrap(false); // 是否换行
//** ************单元格格式设置完成****************** *//*
// 用于正文
WritableCellFormat wcf_text = new WritableCellFormat(NormalFont);
wcf_text.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_text.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_text.setAlignment(Alignment.CENTRE); // 水平对齐
wcf_text.setWrap(true); // 是否换行
//** ***************以下是内容************************** *//*
WritableFont wcf_font=(WritableFont) wcf_text.getFont();
WritableCellFormat wcf_numberfomart = new WritableCellFormat(wcf_font,NumberFormats.TEXT);//设置数字格式类型按照文本格式输出
wcf_numberfomart.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_numberfomart.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_numberfomart.setAlignment(Alignment.LEFT); // 水平对齐
wcf_numberfomart.setWrap(true); // 是否换行
//** ***************以下是标题************************** *//*
sheet.setRowView(0, 800); // 设置行高
sheet.setRowView(1, 1600); // 设置行高
sheet.addCell(new Label(0, 0, "业绩销售报表", wcf_heading));
sheet.mergeCells(0, 0, 20, 0);


sheet.addCell(new Label(0, 1, "业务员", wcf_title));
sheet.addCell(new Label(1, 1, "所属部门", wcf_title));
sheet.addCell(new Label(2, 1, "预算收入合计", wcf_title));
sheet.addCell(new Label(3, 1, "预算支出合计", wcf_title));//支出
sheet.addCell(new Label(4, 1, "预算利润合计", wcf_title));//收入
sheet.addCell(new Label(5, 1, "实际收入合计", wcf_title));
sheet.addCell(new Label(6, 1, "手续费合计", wcf_title));
sheet.addCell(new Label(7, 1, "实际支出合计", wcf_title));
sheet.addCell(new Label(8, 1, "实际利润合计", wcf_title));
sheet.addCell(new Label(9, 1, "退款利润合计", wcf_title));
sheet.addCell(new Label(10, 1, "总利润合计", wcf_title));

sheet.addCell(new Label(11,1,"团名称",wcf_title));
sheet.addCell(new Label(12,1,"团号",wcf_title));
sheet.addCell(new Label(13,1,"目的地",wcf_title));
sheet.addCell(new Label(14,1,"类型",wcf_title));
sheet.addCell(new Label(15,1,"渠道",wcf_title));
sheet.addCell(new Label(16,1,"结算时间",wcf_title));
sheet.addCell(new Label(17,1,"实际收入",wcf_title));
sheet.addCell(new Label(18,1,"手续费",wcf_title));
sheet.addCell(new Label(19,1,"实际支出",wcf_title));
sheet.addCell(new Label(20,1,"利润",wcf_title));


int currRow = 2;
Map<String,String> map= CrmSystem.commonMap("workflowMap");
if (list != null && list.size() > 0) {
for (Record cr : list) {
String createuser = cr.get("createuser").toString();
String creatname = "";
if(StrKit.notBlank(createuser)){
CrmUser user = CrmUser.dao.findById(createuser);
creatname = user.getNickname();
}
System.out.println("creatname:"+creatname);
String depart = cr.get("depart_id").toString();
String departname = "";
if(StrKit.notBlank(depart)){
CrmDepartment department = CrmDepartment.dao.findById(depart);
departname = department.getDepartmentName();
}
List<CrmTeam> teamList = CrmTeam.getTeamList(begin, end, createuser);
int num = 1;
if(teamList != null){
if(teamList.size() >1){
num = teamList.size();
}else{
num = 0;
}
}else {
num = 0;
}
//合并行
sheet.mergeCells(0, currRow, 0, num + currRow-1);
sheet.mergeCells(1, currRow, 1, num + currRow-1);
sheet.mergeCells(2, currRow, 2, num+currRow-1);
sheet.mergeCells(3, currRow, 3, num+currRow-1);
sheet.mergeCells(4, currRow, 4, num+currRow-1);
sheet.mergeCells(5, currRow, 5, num+currRow-1);
sheet.mergeCells(6, currRow, 6, num+currRow-1);
sheet.mergeCells(7, currRow, 7, num+currRow-1);
sheet.mergeCells(8, currRow, 8, num+currRow-1);
sheet.mergeCells(9, currRow, 9, num+currRow-1);
sheet.mergeCells(10, currRow, 10, num + currRow-1);
if(teamList != null && teamList.size() >0 ){
for (CrmTeam team:teamList) {
sheet.setRowView(currRow, 400); // 设置行高

sheet.addCell(new Label(0, currRow, checkBlank(creatname), wcf_text));

sheet.addCell(new Label(1, currRow, checkBlank(departname), wcf_text));
Double yushou = Double.valueOf(cr.get("yushou").toString());

sheet.addCell(new Label(2, currRow, checkBlank(yushou), wcf_text));
Double yufu = Double.valueOf(cr.get("yufu").toString());

sheet.addCell(new Label(3,currRow,checkBlank(yufu),wcf_text));
Double bugetprofit = HelpUtil.sub(yushou, yufu);

sheet.addCell(new Label(4,currRow,checkBlank(bugetprofit),wcf_text));
Double shishou = Double.valueOf(cr.get("shishou").toString());

sheet.addCell(new Label(5, currRow, checkBlank(shishou), wcf_text));
Double shouxufei = Double.valueOf(cr.get("shouxufei").toString());

sheet.addCell(new Label(6,currRow,checkBlank(shouxufei),wcf_text));
Double shifu = Double.valueOf(cr.get("shifu").toString());

sheet.addCell(new Label(7, currRow, checkBlank(shifu), wcf_text));
Double profit = HelpUtil.sub(HelpUtil.sub(shishou, shouxufei), shifu);

sheet.addCell(new Label(8, currRow, checkBlank(profit), wcf_text));
Double tuizhi = Double.valueOf(cr.get("tuizhi").toString());
Double tuishou = Double.valueOf(cr.get("tuishou").toString());
Double refundProfit = HelpUtil.sub(tuizhi, tuishou);

sheet.addCell(new Label(9, currRow, checkBlank(refundProfit), wcf_text));
Double totalprofit = HelpUtil.add(profit,refundProfit);

sheet.addCell(new Label(10, currRow, checkBlank(totalprofit), wcf_text));

sheet.addCell(new Label(11,currRow,checkBlank(team.getTeamname()),wcf_text));
sheet.addCell(new Label(12,currRow,checkBlank(team.getTeamcode()),wcf_text));
sheet.addCell(new Label(13,currRow,checkBlank(team.getDestination()),wcf_text));
sheet.addCell(new Label(14,currRow,checkBlank(CrmTeam.teamtypeMap.get(team.getTeamtype().toString())),wcf_text));
sheet.addCell(new Label(15,currRow,checkBlank(CrmTeam.channelMap.get(team.getChannel().toString())),wcf_text));
sheet.addCell(new Label(16, currRow, checkBlank(DateUtil.dateToString(team.getSettletime(),"yyyy-MM-dd HH:mm:ss")), wcf_text));
Double comin = CrmCash.dao.getComein(Integer.valueOf(team.get("id").toString()));
sheet.addCell(new Label(17, currRow, checkBlank(comin), wcf_text));
Double fee = CrmCash.dao.getFeeprice(Integer.valueOf(team.get("id").toString()));
sheet.addCell(new Label(18,currRow,checkBlank(fee),wcf_text));
Double pay = CrmCash.dao.getPayment1(Integer.valueOf(team.get("id").toString()));
sheet.addCell(new Label(19,currRow,checkBlank(pay),wcf_text));
Double mporfit = HelpUtil.sub(HelpUtil.sub(comin, fee), pay);
sheet.addCell(new Label(20,currRow,checkBlank(mporfit),wcf_text));
currRow++;
}
}


}
}
/** **********以上所写的内容都是写在缓存中的,下一句将缓存的内容写到文件中******** */
workbook.write();
/** *********关闭文件************* */
workbook.close();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

String sCurrPath = System.getProperty("java.io.tmpdir") + "\\xlsx"+ (new java.util.Date().getTime())+".xls";
posted @ 2016-04-28 10:06  码农行天下  阅读(294)  评论(0编辑  收藏  举报