Java Web使用POI导出Excel的方法详解
本文实例讲述了JavaWeb使用POI导出Excel的方法。分享给大家供大家参考,具体如下:
采用Springmvc架构:
Controller层代码如下
@Controller publicclassStudentExportController{ @Autowired privateStudentExportServicestudentExportService; @RequestMapping(value="/excel/export") publicvoidexportExcel(HttpServletRequestrequest,HttpServletResponseresponse) throwsException{ Listlist=newArrayList (); list.add(newStudent(1000,"zhangsan","20")); list.add(newStudent(1001,"lisi","23")); list.add(newStudent(1002,"wangwu","25")); HSSFWorkbookwb=studentExportService.export(list); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition","attachment;filename=student.xls"); OutputStreamouputStream=response.getOutputStream(); wb.write(ouputStream); ouputStream.flush(); ouputStream.close(); } }
Service层代码如下:
@Service publicclassStudentExportService{ String[]excelHeader={"Sno","Name","Age"}; publicHSSFWorkbookexport(Listlist){ HSSFWorkbookwb=newHSSFWorkbook(); HSSFSheetsheet=wb.createSheet("Campaign"); HSSFRowrow=sheet.createRow((int)0); HSSFCellStylestyle=wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); for(inti=0;i 前台的js代码如下:
设置Excel样式以及注意点:
String[]excelHeader={"所属区域(地市)","机房","机架资源情况","","","","", "","端口资源情况","","","","","","机位资源情况","","","设备资源情况", "","","IP资源情况","","","","","网络设备数"}; String[]excelHeader1={"","","总量(个)","空闲(个)","预占(个)","实占(个)", "自用(个)","其它(个)","总量(个)","在用(个)","空闲(个)","总带宽(M)", "在用带宽(M)","空闲带宽(M)","总量(个)","在用(个)","空闲(个)","设备总量(个)", "客户设备(个)","电信设备(个)","总量(个)","空闲(个)","预占用(个)","实占用(个)", "自用(个)",""}; //单元格列宽 int[]excelHeaderWidth={150,120,100,100,100,100,100,100,100, 100,100,120,120,120,120,120,120,150,150,150,120, 120,150,150,120,150}; HSSFWorkbookwb=newHSSFWorkbook(); HSSFSheetsheet=wb.createSheet("机房报表统计"); HSSFRowrow=sheet.createRow((int)0); HSSFCellStylestyle=wb.createCellStyle(); //设置居中样式 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 //设置合计样式 HSSFCellStylestyle1=wb.createCellStyle(); Fontfont=wb.createFont(); font.setColor(HSSFColor.RED.index); font.setBoldweight(Font.BOLDWEIGHT_BOLD);//粗体 style1.setFont(font); style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 //合并单元格 //firstrow(0-based)lastrow(0-based)firstcolumn(0-based)last //column(0-based) sheet.addMergedRegion(newCellRangeAddress(0,1,0,0)); sheet.addMergedRegion(newCellRangeAddress(0,1,1,1)); sheet.addMergedRegion(newCellRangeAddress(0,0,2,7)); sheet.addMergedRegion(newCellRangeAddress(0,0,8,13)); sheet.addMergedRegion(newCellRangeAddress(0,0,14,16)); sheet.addMergedRegion(newCellRangeAddress(0,0,17,19)); sheet.addMergedRegion(newCellRangeAddress(0,0,20,24)); sheet.addMergedRegion(newCellRangeAddress(0,1,25,25)); //设置列宽度(像素) for(inti=0;i注意点1:合并单元格 newCellRangeAddress(int,int,int,int)
firstrow(0-based),lastrow(0-based),firstcolumn(0-based),lastcolumn(0-based)注意点2:合并单元格
String[]excelHeader={"所属区域(地市)","机房","机架资源情况","","","","","","端口资源情况","","","","","","机位资源情况","","","设备资源情况","","","IP资源情况","","","","","网络设备数"};合并以后的单元格虽然是一个,但是仍然要保留其单元格内容,此处用空字符串代替,否则后续表头显示不出
注意点3:填充单元格
正确写法:
HSSFCellcell=row.createCell(i); cell.setCellValue(excelHeader1[i]); cell.setCellStyle(style);错误写法:
row.createCell(i).setCellValue(excelHeader1[i]); row.createCell(i).setCellStyle(style);本人为了省一个HSSFCell对象,使用了错误写法,导致HSSFCell对象创建了2次,最后只保留了样式,而内容无法显示
更多关于java相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java文件与目录操作技巧汇总》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。