1.构造excel表格:
// 创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(new File(ywlxS+".xls"));// 创建新的一页
WritableSheet sheet = workbook.createSheet(ywlxS+"信息", 0);
// 横向打印
sheet.setPageSetup(PageOrientation.LANDSCAPE, PaperSize.A4,0.5d, 0.5d);
// 构造表头
sheet.mergeCells(0, 0, 8, 0);// 添加合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行
WritableFont bold = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD);// 设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示
WritableCellFormat titleFormate = new WritableCellFormat(bold);// 生成一个单元格样式控制对象
titleFormate.setAlignment(jxl.format.Alignment.CENTRE);// 单元格中的内容水平方向居中
titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 单元格的内容垂直方向居中
Label title = new Label(0, 0, "业务管理信息", titleFormate);
sheet.setRowView(0, 600, false);// 设置第一行的高度
sheet.addCell(title);
// 创建要显示的具体数据
WritableFont color = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD);// 选择字体
WritableCellFormat colorFormat = new WritableCellFormat(color);
sheet.setColumnView(0, 10);
sheet.setColumnView(2, 13);
sheet.setColumnView(4, 14);
sheet.setColumnView(5, 25);
sheet.addCell(new Label(0, 1, "卡号", colorFormat));
sheet.addCell(new Label(1, 1, "患者姓名", colorFormat));
sheet.addCell(new Label(2, 1, "身份证号", colorFormat));
sheet.addCell(new Label(3, 1, "病区", colorFormat));
sheet.addCell(new Label(4, 1, "床号", colorFormat));
sheet.addCell(new Label(5, 1, "业务类型", colorFormat));
sheet.addCell(new Label(6, 1, "信息来源", colorFormat));
sheet.addCell(new Label(7, 1, "办理时间", colorFormat));
String keyWord = " where 1 = 1 and YWLX = '"+ywlxE+"' ORDER BY OPERATIME DESC";
List<Ywgl> ywglList = ywglService.getAll(keyWord);
int rowCount = 0; // 行数
String ywlx = null ; //业务类型(0-血液办理、1-退血、2-护理申请、3-订餐、4-退餐、5-器具租赁、6-出院代办、7-出院用车、8-产品预定)
String xxly = null ; //信息来源(0-服务端、1-移动端、2-大屏端)
for (int i = 0; i < ywglList.size(); i++) {
sheet.addCell(new Label(0, i + 2, ywglList.get(i).getKhid().getKnum().toString()));
sheet.addCell(new Label(1, i + 2, ywglList.get(i).getHzid().getXm().toString()));
sheet.addCell(new Label(2, i + 2, ywglList.get(i).getHzid().getSfzh().toString()));
sheet.addCell(new Label(3, i + 2, ywglList.get(i).getHzid().getBq().getBqmc().toString()));
sheet.addCell(new Label(4, i + 2, ywglList.get(i).getHzid().getCh().getChmc().toString()));
sheet.addCell(new Label(5, i + 2, String.valueOf(ywglList.get(i).getYwlx())));
if(String.valueOf(ywglList.get(i).getYwlx()).equals("0")){
ywlx = "血液办理" ;
}else if(String.valueOf(ywglList.get(i).getYwlx()).equals("1")){
ywlx = "退血" ;
}else if(String.valueOf(ywglList.get(i).getYwlx()).equals("2")){
ywlx = "护理申请" ;
}else if(String.valueOf(ywglList.get(i).getYwlx()).equals("3")){
ywlx = "订餐" ;
}else if(String.valueOf(ywglList.get(i).getYwlx()).equals("4")){
ywlx = "退餐" ;
}else if(String.valueOf(ywglList.get(i).getYwlx()).equals("5")){
ywlx = "器具租赁" ;
}else if(String.valueOf(ywglList.get(i).getYwlx()).equals("6")){
ywlx = "出院代办" ;
}else if(String.valueOf(ywglList.get(i).getYwlx()).equals("7")){
ywlx = "出院用车" ;
}else if(String.valueOf(ywglList.get(i).getYwlx()).equals("8")){
ywlx = "产品预定" ;
}
sheet.addCell(new Label(5, i + 2, ywlx));
if(String.valueOf(ywglList.get(i).getXxly()).equals("0")){
xxly = "服务端" ;
}else if(String.valueOf(ywglList.get(i).getXxly()).equals("1")){
xxly = "移动端" ;
}else if(String.valueOf(ywglList.get(i).getXxly()).equals("2")){
xxly = "大屏端" ;
}
sheet.addCell(new Label(6, i + 2, xxly));
sheet.addCell(new Label(7, i + 2, ywglList.get(i).getOperatime().toString().substring(0, 10)));
rowCount = rowCount + 1;
}
// 构造表底
sheet.setRowView(0, 600, false);// 设置第一行的高度
// 把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
DownloadFile.download(ywlxS+".xls", response);
2.下载文件
public static HttpServletResponse download(String path,
HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
filename = new String(filename.replaceAll(" ", "").getBytes("UTF-8"), "ISO8859-1");
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
// response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes()));
response.addHeader("Content-Disposition", "attachment;filename=" +filename);
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}