java数据导出成Excel文件

2019-08-09 00:00:00 数据 文件 导出
HSSFWorkbook workbook = new HSSFWorkbook();

HSSFSheet sheet = workbook.createSheet("综合信息");
HSSFRow row = sheet.createRow(0);
HSSFCell c0 = row.createCell(0);
//设置格式
HSSFCellStyle cellStyle = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
font.setFontName("黑体");
cellStyle.setFont(font);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
c0.setCellStyle(cellStyle);

c0.setCellValue(new HSSFRichTextString(startTime1));
sheet.addMergedRegion(new CellRangeAddress(0,0,0,7));
HSSFRow row1 = sheet.createRow(1);
HSSFCell c1 = row1.createCell(0);
c1.setCellValue(new HSSFRichTextString("门诊人数"));
HSSFCell c3 = row1.createCell(2);
c3.setCellValue(new HSSFRichTextString("门急诊总人数"));
HSSFCell c4 = row1.createCell(4);
c4.setCellValue(new HSSFRichTextString("出诊医生总人数"));
HSSFCell c5 = row1.createCell(6);
c5.setCellValue(new HSSFRichTextString("医生平均门急诊量"));

HSSFRow row2 = sheet.createRow(2);
HSSFCell c2 = row2.createCell(0);
c2.setCellValue(new HSSFRichTextString("急诊人数"));

HSSFRow row3 = sheet.createRow(3);
HSSFCell c6 = row3.createCell(0);
c6.setCellValue(new HSSFRichTextString("处方数量"));
HSSFCell c7 = row3.createCell(2);
c7.setCellValue(new HSSFRichTextString("最大处方"));
HSSFCell c8 = row3.createCell(4);
c8.setCellValue(new HSSFRichTextString("平均处方金额"));
HSSFCell c9 = row3.createCell(6);
c9.setCellValue(new HSSFRichTextString("医生平均处方数量"));

HSSFRow row4 = sheet.createRow(4);
HSSFCell c10 = row4.createCell(0);
c10.setCellValue(new HSSFRichTextString("处方总额"));
HSSFCell c11 = row4.createCell(2);
c11.setCellValue(new HSSFRichTextString("最小处方"));

HSSFRow row5 = sheet.createRow(5);
HSSFCell c12 = row5.createCell(0);
c12.setCellValue(new HSSFRichTextString("入院人数"));
HSSFCell c13 = row5.createCell(2);
c13.setCellValue(new HSSFRichTextString("出院人数"));
HSSFCell c14 = row5.createCell(4);
c14.setCellValue(new HSSFRichTextString("在院人数"));
HSSFCell c15 = row5.createCell(6);
c15.setCellValue(new HSSFRichTextString("危重人数"));

HSSFRow row6 = sheet.createRow(6);
HSSFCell c16 = row6.createCell(0);
c16.setCellValue(new HSSFRichTextString("额定床位"));
HSSFCell c17 = row6.createCell(2);
c17.setCellValue(new HSSFRichTextString("使用床位"));
HSSFCell c18 = row6.createCell(4);
c18.setCellValue(new HSSFRichTextString("床位使用率"));
HSSFCell c19 = row6.createCell(6);
c19.setCellValue(new HSSFRichTextString("死亡人数"));

HSSFRow row7 = sheet.createRow(7);
HSSFCell c20 = row7.createCell(0);
c20.setCellValue(new HSSFRichTextString("医疗收入"));
HSSFCell c21 = row7.createCell(2);
c21.setCellValue(new HSSFRichTextString("药品收入"));
HSSFCell c22 = row7.createCell(4);
c22.setCellValue(new HSSFRichTextString("其他收入"));
HSSFCell c23 = row7.createCell(6);
c23.setCellValue(new HSSFRichTextString("合计收入"));

HSSFRow row8 = sheet.createRow(8);
HSSFCell c24 = row8.createCell(0);
c24.setCellValue(new HSSFRichTextString("今日手术"));
HSSFCell c25 = row8.createCell(1);
c25.setCellValue(new HSSFRichTextString("安排数量"));
HSSFCell c26 = row8.createCell(4);
c26.setCellValue(new HSSFRichTextString("明日手术安排数量"));

HSSFRow row9 = sheet.createRow(9);
HSSFCell c27 = row9.createCell(1);
c27.setCellValue(new HSSFRichTextString("实际数量"));

for (int i=0;i<list1.size();i++){
row1.createCell(1).setCellValue(list1.get(0));
row1.createCell(3).setCellValue(sum1);
row1.createCell(5).setCellValue(list1.get(2));
row1.createCell(7).setCellValue(avg1);
row2.createCell(1).setCellValue(list1.get(1));
row3.createCell(1).setCellValue(list1.get(3));
row3.createCell(3).setCellValue(list1.get(4));
row3.createCell(5).setCellValue(avg2);
row3.createCell(7).setCellValue(avg3);
row4.createCell(1).setCellValue(list1.get(5));
row4.createCell(3).setCellValue(list1.get(6));
row5.createCell(1).setCellValue(list1.get(7));
row5.createCell(3).setCellValue(list1.get(8));
row5.createCell(5).setCellValue(list1.get(9));
row5.createCell(7).setCellValue(list1.get(10));
row6.createCell(1).setCellValue(list1.get(11));
row6.createCell(3).setCellValue(list1.get(12));
row6.createCell(5).setCellValue(cwsyl);
row6.createCell(7).setCellValue(list1.get(13));
row7.createCell(1).setCellValue(list1.get(14));
row7.createCell(3).setCellValue(list1.get(15));
row7.createCell(5).setCellValue(list1.get(16));
row7.createCell(7).setCellValue(sum2);
row8.createCell(2).setCellValue(list1.get(17));
row8.createCell(6).setCellValue(list1.get(19));
row9.createCell(2).setCellValue(list1.get(18));
}
sheet.addMergedRegion(new CellRangeAddress(1,2,2,2));
sheet.addMergedRegion(new CellRangeAddress(1,2,3,3));
sheet.addMergedRegion(new CellRangeAddress(1,2,4,4));
sheet.addMergedRegion(new CellRangeAddress(1,2,5,5));
sheet.addMergedRegion(new CellRangeAddress(1,2,6,6));
sheet.addMergedRegion(new CellRangeAddress(1,2,7,7));
sheet.addMergedRegion(new CellRangeAddress(3,4,4,4));
sheet.addMergedRegion(new CellRangeAddress(3,4,5,5));
sheet.addMergedRegion(new CellRangeAddress(3,4,6,6));
sheet.addMergedRegion(new CellRangeAddress(3,4,7,7));
sheet.addMergedRegion(new CellRangeAddress(8,9,0,0));
sheet.addMergedRegion(new CellRangeAddress(8,8,2,3));
sheet.addMergedRegion(new CellRangeAddress(9,9,2,3));
sheet.addMergedRegion(new CellRangeAddress(8,9,4,5));
sheet.addMergedRegion(new CellRangeAddress(8,9,6,7));

c1.setCellStyle(cellStyle);
c2.setCellStyle(cellStyle);
c3.setCellStyle(cellStyle);
c4.setCellStyle(cellStyle);
c5.setCellStyle(cellStyle);
c6.setCellStyle(cellStyle);
c7.setCellStyle(cellStyle);
c8.setCellStyle(cellStyle);
c9.setCellStyle(cellStyle);
c10.setCellStyle(cellStyle);
c11.setCellStyle(cellStyle);
c12.setCellStyle(cellStyle);
c13.setCellStyle(cellStyle);
c14.setCellStyle(cellStyle);
c15.setCellStyle(cellStyle);
c16.setCellStyle(cellStyle);
c17.setCellStyle(cellStyle);
c18.setCellStyle(cellStyle);
c19.setCellStyle(cellStyle);
c20.setCellStyle(cellStyle);
c21.setCellStyle(cellStyle);
c22.setCellStyle(cellStyle);
c23.setCellStyle(cellStyle);
c24.setCellStyle(cellStyle);
c25.setCellStyle(cellStyle);
c26.setCellStyle(cellStyle);
c27.setCellStyle(cellStyle);

for (int i=0;i<7;i++){
sheet.autoSizeColumn(i);
}
String str = "门急诊总人数门诊";
String str1 = "初诊医生总人数门诊";
String str2 = "医生平均门急诊量门诊";
int length = str.getBytes().length;
int length1 = str1.getBytes().length;
int length2 = str2.getBytes().length;
sheet.setColumnWidth(2,length*256);
sheet.setColumnWidth(4,length1*256);
sheet.setColumnWidth(6,length2*256);

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String("综合信息".getBytes(),"iso-8859-1") + ".xls");OutputStream ouputStream = response.getOutputStream()
;workbook.write(ouputStream)
;ouputStream.flush()
;ouputStream.close()
;

相关文章