java数据导出成Excel文件
程序员文章站
2023-11-04 11:35:22
HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet("综合信息");HSSFRow row = sheet.createRow(0);HSSFCell c0 = row.createCel ......
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()
;
推荐阅读