您的位置:首页 > 其它

导出excel

2016-02-29 16:15 267 查看

———-

String ExcelFilePath = exportExcelService.exportExcelFile("hosDocVitalityStats", "医院,总医生数,登录医生数,活跃度 ", "30,20,20,20", list, total.get(0));// 返回导出的Excel文件路径


/**
* 导出excel
*
* @param statsType
* @param headers
* @param widths
* @param pageList
* @param object
* @param path
*/
public String exportExcelFile(String statsType, String headers, String widths, List list, Object object) {
String filename = "";
try {
filename = createFile(statsType, headers, widths, list, object);
} catch (SysRuntimeException e) {
throw new SysRuntimeException(e.getCode(), e.getMsg());
} catch (Exception e) {
e.printStackTrace();
}
return filename;
}


/*创建excel文件*/
private String createFile(String statsType, String headers, String widths, List list, Object object) throws IOException, RowsExceededException, WriteException {
String separator = File.separator;
String path = this.getClass().getResource("/").getPath();
path = path.replace("WEB-INF/classes/", "downloadFile" + separator);
System.out.println("----------------------------------------------------------" + path);
String filename = statsType + "_" + DateUtil.dateFormate(new Date(), "yyyy-MM-dd") + ".xls";
FileOutputStream fo = new FileOutputStream(path + filename);
WritableWorkbook book = Workbook.createWorkbook(fo);
WritableSheet sheet = book.createSheet("AA", 0);
try {
addSheetTitle(sheet, statsType);
addSheetHead(sheet, headers, widths);
addSheetBody(sheet, list, object, statsType);
} catch (SysRuntimeException e) {
throw new SysRuntimeException(e.getCode(), e.getMsg());
} catch (Exception e) {
e.printStackTrace();
} finally {
book.write();
book.close();
fo.close();
}
return filename;
}


protected void addSheetTitle(WritableSheet sheet, String statsType) throws WriteException {
WritableCellFormat headerFormat = new WritableCellFormat(NumberFormats.TEXT);
// 添加字体设置
WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);
headerFormat.setFont(font);
// 设置单元格背景色:表头为黄色
headerFormat.setBackground(Colour.YELLOW);
// 设置表头表格边框样式
headerFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.GRAY_25);
// 表头内容水平居中显示
headerFormat.setAlignment(Alignment.CENTRE);
try {
sheet.addCell(new Label(0, 0, statsType, headerFormat));
sheet.setColumnView(0, 30);
sheet.addCell(new Label(1, 0, "制单日期:", headerFormat));
sheet.addCell(new Label(2, 0, DateUtil.dateFormate(new Date(), "yyyy-MM-dd HH:mm:ss".toString()), headerFormat));
sheet.setColumnView(2, 30);
} catch (Exception e) {
e.printStackTrace();
throw new SysRuntimeException("-1", "创建标题内容失败");
}
}


private void addSheetHead(WritableSheet sheet, String headers, String widths) {
String[] headdata = headers.split(",");
String[] widthdata = widths.split(",");
WritableFont fBlue = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat wcfF = new jxl.write.WritableCellFormat(fBlue);
try {
for (int i = 0; i < headdata.length; i++) {
sheet.addCell(new Label(i, 1, headdata[i], wcfF));
int width = Integer.parseInt(StringUtils.isBlank(widthdata[i].toString()) ? "" : widthdata[i].toString());
sheet.setColumnView(i, width);
}
} catch (Exception e) {
e.printStackTrace();
throw new SysRuntimeException("-1", "创建字段名行失败");
}

}


private void addSheetBody(WritableSheet sheet, List list, Object object, String statsType) {
int r = 1;
try {
if (list != null) {
Iterator it = list.iterator();
while (it.hasNext()) {
r++;
addRow(sheet, r, it, null, statsType);
}
r++;
addRow(sheet, r, null, object, statsType);
}
} catch (Exception e) {
e.printStackTrace();
throw new SysRuntimeException("-1", "创建内容行失败");
}

}


private void addRow(WritableSheet sheet, int r, Iterator it, Object object, String statsType) throws WriteException, RowsExceededException {
WritableFont fRed = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat wcfF = new jxl.write.WritableCellFormat(fRed);
if ("hosDocVitalityStats".equals(statsType)) {
if (it != null) {
SaHosDocVitalityStats ob = (SaHosDocVitalityStats) it.next();
sheet.addCell(new Label(0, r, ob.getHosName()));
sheet.addCell(new Label(1, r, ob.getTotalDocQuantity().toString()));
sheet.addCell(new Label(2, r, ob.getLoginDocQuantity().toString()));
sheet.addCell(new Label(3, r, ob.getVitalityRate().toString()));
}
if (object != null) {
SaHosDocVitalityStats total = (SaHosDocVitalityStats) object;
sheet.addCell(new Label(0, r, "汇总", wcfF));
sheet.addCell(new Label(1, r, total.getAlltotalDocQuantity().toString(), wcfF));
sheet.addCell(new Label(2, r, total.getAllloginDocQuantity().toString(), wcfF));
sheet.addCell(new Label(3, r, total.getAllvitalityRate().toString(), wcfF));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: