作为一个刚工作不久的开发者来说,积累自己的代码很有必要。下面是总结jxl的导出excel文件的一个实例:
1、首先需要导入jxl.jar到自己的项目中
try {
//期刊
List
//业务员
List
//结算日期
String date = getPara("date", time.getTimeBymonth());
//获取选中的期刊id
Integer pid = getParaToInt("pid", plist.get(0).getInt("pub_id"));
Publish pub = Publish.dao.findById(pid);
//获取业务员id
Integer uid = getParaToInt("uid", ulist.get(0).getInt("user_id"));
User user = User.dao.findById(uid);
String[] riqi = date.split("-");//riqi[0] 年份
List
Issue iss = Issue.dao.findFirst("select * from issue where iss_name=" + riqi[1] + " and pub_id=" + pid);
List
+ iss.getInt("iss_id") + " and a.pub_id=" + pid + " and a.acc_year='" + riqi[0] + "'");
List
+ " FROM accounts a LEFT JOIN customers c ON a.cus_id=c.cus_id " + "WHERE a.user_id=" + uid + " and a.iss_id="
+ iss.getInt("iss_id") + " and a.pub_id=" + pid + " and a.acc_year='" + riqi[0] + "' GROUP BY a.cus_id");
//打开文件
WritableWorkbook book = Workbook.createWorkbook(new File(PathKit.getWebRootPath() + "/temp/" + riqi[0] + "年"
+ user.getStr("user_xingming") + "《"+ pub.getStr("pub_name")+"》" + iss.getStr("iss_name") + "期销售结算表.xls"));
//生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheetOne = book.createSheet("结算表", 0);
//在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
/**
* 定义单元格样式
*/
WritableFont wf_title = new WritableFont(WritableFont.ARIAL, 11,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // 单元格定义
wcf_title.setBackground(jxl.format.Colour.WHITE); // 设置单元格的背景颜色
wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //设置边框
//以及单元格内容为test
Label Title = new Label(0, 0, "" + riqi[0] + "年" + user.getStr("user_xingming") +"《"+ pub.getStr("pub_name")+"》" + iss.getStr("iss_name")
+ "期销售结算表",wcf_title);//第一行
sheetOne.setColumnView(sheetOne.getColumns(), 20); // 设置列的宽度
Label title1 = new Label(0, 1, "客户");//第二行
for (int i = 0; i < elist.size(); i++) {
if(i==0){
sheetOne.setColumnView(sheetOne.getColumns()+1, 20); // 设置列的宽度
sheetOne.addCell(new Label(sheetOne.getColumns()+1, 1, elist.get(i).getStr("edi_name") + "订刊"));
}else{
sheetOne.setColumnView(sheetOne.getColumns(), 20); // 设置列的宽度
sheetOne.addCell(new Label(sheetOne.getColumns(), 1, elist.get(i).getStr("edi_name") + "订刊"));
}
sheetOne.setColumnView(sheetOne.getColumns(), 20); // 设置列的宽度
sheetOne.addCell(new Label(sheetOne.getColumns(), 1, elist.get(i).getStr("edi_name") + "赠刊"));
}
Label title2 = new Label(elist.size()*2 + 1, 1, "单价");
Label title3 = new Label(elist.size()*2 + 2, 1, "码洋");
Label title4 = new Label(elist.size()*2 + 3, 1, "折扣");
Label title5 = new Label(elist.size()*2 + 4, 1, "金额");
//将定义好的单元格添加到工作表中
sheetOne.addCell(Title);
sheetOne.addCell(title1);
sheetOne.addCell(title2);
sheetOne.addCell(title3);
sheetOne.addCell(title4);
sheetOne.addCell(title5);
//循环数据
float my=0.0f;
float jine=0.0f;
for(int i=0;i
for (int j = 0; j < elist.size(); j++) {
int flag=0;
for(int z=0;z
&&aList.get(i).getInt("cus_id")==acclist.get(z).getInt("cus_id")){
sheetOne.addCell(new Label(j*2+1,i+2,""+acclist.get(z).getInt("acc_dnum")+""));
sheetOne.addCell(new Label(j*2+2,i+2,""+acclist.get(z).getInt("acc_znum")+""));
flag=1;
}
}
if(flag==0){
sheetOne.addCell(new Label(j*2+1,i+2,""));
sheetOne.addCell(new Label(j*2+2,i+2,""));
}
}
sheetOne.addCell(new Label(elist.size()*2+1,i+2,""+iss.getFloat("iss_price")+""));//单价
sheetOne.addCell(new Label(elist.size()*2+2,i+2,""+aList.get(i).getDouble("my").floatValue()+""));//码洋
sheetOne.addCell(new Label(elist.size()*2+3,i+2,""+(aList.get(i).getDouble("jine").floatValue()/aList.get(i).getDouble("my").floatValue())+""));//折扣
sheetOne.addCell(new Label(elist.size()*2+4,i+2,""+aList.get(i).getDouble("jine").floatValue()+""));//金额
my=my+aList.get(i).getDouble("my").floatValue();
jine=jine+aList.get(i).getDouble("jine").floatValue();
}
sheetOne.addCell(new Label(0,aList.size()+2, "合计"));
for (int j = 0; j < elist.size(); j++) {
int flag=0;
int d=0;
int z=0;
for(int i=0;i
d=d+acclist.get(i).getInt("acc_dnum");
z=z+acclist.get(i).getInt("acc_znum");
flag=1;
}
}
if(flag==0){
sheetOne.addCell(new Label(j*2+1,aList.size()+2,"0"));
sheetOne.addCell(new Label(j*2+2,aList.size()+2,"0"));
}else{
sheetOne.addCell(new Label(j*2+1,aList.size()+2,""+d+""));
sheetOne.addCell(new Label(j*2+2,aList.size()+2,""+z+""));
}
}
sheetOne.addCell(new Label(elist.size()*2+1,aList.size()+2,""+iss.getFloat("iss_price")+""));//单价
sheetOne.addCell(new Label(elist.size()*2+2,aList.size()+2,""+my+""));//码洋
sheetOne.addCell(new Label(elist.size()*2+3,aList.size()+2,""+jine/my+""));//折扣
sheetOne.addCell(new Label(elist.size()*2+4,aList.size()+2,""+jine+""));//金额
//合: 第1列第1行
sheetOne.mergeCells(0, 0, 4+elist.size()*2, 0);
//写入数据并关闭文件
book.write();
book.close();
// getResponse().setContentType("application/vnd.ms-excel");
// String filename = "http://127.0.0.l:8080/hebei_read/temp/"+riqi[0] + "年" + user.getStr("user_xingming") + pub.getStr("pub_name") + iss.getStr("iss_name") + "期销售结算表";
// getResponse().setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gb2312"), "ISO8859-1") + ".xls");
renderFile(new File(PathKit.getWebRootPath() + "/temp/" + riqi[0] + "年" + user.getStr("user_xingming") +"《"+ pub.getStr("pub_name")+"》"
+ iss.getStr("iss_name") + "期销售结算表.xls"));
} 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();
}
}