jxl实现excel导出

阅读更多
/**
*list:数据源,check搜索条件,用于显示在excel第一行,path:存放路径
*@return:返回文件名;
*/
public String writeExcel(List list,String check,String path) throws Exception{
WritableWorkbook workbook;//建立工作薄
    String filename="paylist"+(int)(Math.random()*999999)+".xls";//文件名
    String targetfile = path+filename;
    File file = new File(targetfile);
workbook = Workbook.createWorkbook(file);
WritableSheet sheet = workbook.createSheet("报销单统计表", 0);//工作表单,0表示第一个sheet
//format1用于标题格式
WritableFont font1 = new WritableFont(WritableFont.TIMES,16,WritableFont.BOLD);
WritableCellFormat format1 = new WritableCellFormat(font1);
format1.setAlignment(jxl.format.Alignment.CENTRE); //水平居中
//format2:查询条件格式
WritableFont font2 = new WritableFont(WritableFont.TIMES,10, WritableFont.BOLD);
WritableCellFormat format2 = new WritableCellFormat(font2);
format2.setAlignment(jxl.format.Alignment.CENTRE); //水平居中
format2.setWrap(true);//自动换行
//format:表单里面的字符串格式
WritableCellFormat format = new WritableCellFormat();
    format.setAlignment(jxl.format.Alignment.CENTRE); //水平居中
    //日期格式
    DateFormat customDateFormat = new DateFormat ("yyyy-MM-dd hh:mm:ss");
    WritableCellFormat dateformat = new WritableCellFormat(customDateFormat);
    dateformat.setAlignment(jxl.format.Alignment.CENTRE);
    //浮点数格式
    WritableCellFormat floatFormat = new WritableCellFormat (NumberFormats.FLOAT);
    floatFormat.setAlignment(jxl.format.Alignment.CENTRE);
    //定义3中标签
Label l;
Number n;
DateTime dt;
String name = "员工报销单表格统计";
sheet.mergeCells(0, 0, 7, 0);//合并单元格左上列,左上行,右上列,右上行。
sheet.mergeCells(0, 1, 7, 1);
l = new Label(0,0,name,format1);
sheet.addCell(l);
l = new Label(0,1,check,format2);
sheet.addCell(l);
String[] title = {"序号","报销单号","报销单位","报销人","上个提交人","报销日期","总金额(元)","状态"};
for(int i = 0; i l = new Label(i, 2, title[i],format2);
sheet.addCell(l);
}
int row = 3;//正文从第二行开始
double totalMoney = 0;
for (ListResult lr : list) {
totalMoney = totalMoney+lr.getMoney();
for(int i = 0; i if("序号".equals(title[i])){
n = new jxl.write.Number(i, row, row-2,format);
sheet.addCell(n);
}else if("报销单号".equals(title[i])){
n = new jxl.write.Number(i, row, lr.getId(),format);
sheet.addCell(n);
}else if("总金额(元)".equals(title[i])){
n = new jxl.write.Number(i, row, lr.getMoney(),floatFormat);
sheet.addCell(n);
}else if("报销日期".equals(title[i])){
dt =new DateTime(i, row, lr.getDate(),dateformat);
sheet.addCell(dt);
}else if("报销单位".equals(title[i])){
l = new Label(i,row,lr.getDepName(),format);
sheet.addCell(l);
}else if("报销人".equals(title[i])){
l = new Label(i,row,lr.getUserName(),format);
sheet.addCell(l);
}else if("上个提交人".equals(title[i])){
l = new Label(i,row,lr.getLastUser(),format);
sheet.addCell(l);
}else if("状态".equals(title[i])){
l = new Label(i,row,lr.getStatus(),format);
sheet.addCell(l);
}
}
row++;
}
sheet.mergeCells(0, row, 5, row);
l = new Label(0,row,"金额合计:",format);
n = new Number(6,row,totalMoney,floatFormat);
sheet.addCell(l);
sheet.addCell(n);
//设置行高
if(check.length()>81){
sheet.setRowView(1,550);
}
//设置列宽
sheet.setColumnView(1,15);
sheet.setColumnView(2,20);
sheet.setColumnView(4,15);
sheet.setColumnView(5,20);
sheet.setColumnView(6,15);
workbook.write();
    workbook.close();
    return filename;
}

你可能感兴趣的:(excel,jxl)