您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > java实现导出excel
采用Springmvc架构:Controller层代码如下Java代码@ControllerpublicclassStudentExportController{@AutowiredprivateStudentExportServicestudentExportService;@RequestMapping(value=/excel/export)publicvoidexportExcel(HttpServletRequestrequest,HttpServletResponseresponse)throwsException{ListStudentlist=newArrayListStudent();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层代码如下:Java代码@ServicepublicclassStudentExportService{String[]excelHeader={Sno,Name,Age};publicHSSFWorkbookexport(ListCampaignlist){HSSFWorkbookwb=newHSSFWorkbook();HSSFSheetsheet=wb.createSheet(Campaign);HSSFRowrow=sheet.createRow((int)0);HSSFCellStylestyle=wb.createCellStyle();style.setAlignment(HSSFCellStyle.ALIGN_CENTER);for(inti=0;iexcelHeader.length;i++){HSSFCellcell=row.createCell(i);cell.setCellValue(excelHeader[i]);cell.setCellStyle(style);sheet.autoSizeColumn(i);}for(inti=0;ilist.size();i++){row=sheet.createRow(i+1);Studentstudent=list.get(i);row.createCell(0).setCellValue(student.getSno());row.createCell(1).setCellValue(student.getName());row.createCell(2).setCellValue(student.getAge());}returnwb;}}前台的js代码如下:Javascript代码scriptfunctionexportExcel(){location.href=excel/export;!--这里不能用ajax请求,ajax请求无法弹出下载保存对话框--}/script//------------------------------------------------------------------------//文件流信息privateInputStreamexcelFile;//文件名称privateStringdownloadFileName;//getter&setterpublicStringgetDownloadFileName(){SimpleDateFormatsf=newSimpleDateFormat(yyyy-MM-dd);StringdownloadFileName=(sf.format(newDate()).toString())+项目信息.xls;try{downloadFileName=newString(downloadFileName.getBytes(),utf-8);Filefile=newFile(C:\\text_shiyan);if(!file.exists()){file.mkdirs();}}catch(UnsupportedEncodingExceptione){e.printStackTrace();}returndownloadFileName;}publicvoidsetDownloadFileName(StringdownloadFileName){this.downloadFileName=downloadFileName;}publicInputStreamgetExcelFile(){returnexcelFile;}publicvoidsetExcelFile(InputStreamexcelFile){this.excelFile=excelFile;}//------------------------------------------------------------------------6.publicStringCreateExcel()throwsException{Teacherteacher=(Teacher)ServletActionContext.getRequest().getSession().getAttribute(Teacher_LOG);courseList=courseService.searchByTid(teacher.getId());scoreList=scoreService.searchUserG(course.getId());HSSFWorkbookworkbook=exportExcel(scoreList);//创建字节数组流对象ByteArrayOutputStreamoutput=newByteArrayOutputStream();workbook.write(output);byte[]ba=output.toByteArray();excelFile=newByteArrayInputStream(ba);output.flush();output.close();returnexcel;}publicHSSFWorkbookexportExcel(ListScoredataList)throwsException{HSSFWorkbookworkbook=null;try{//这里的数据即时你要从后台取得的数据//创建工作簿实例workbook=newHSSFWorkbook();//创建工作表实例并设置工作表名称HSSFSheetsheet=workbook.createSheet(TscExcel);//设置列宽this.setSheetColumnWidth(sheet);//表头样式HSSFCellStylestyle=this.createTitleStyle(workbook);//表格样式左对齐HSSFCellStylestyle1=this.createLeftStyle(workbook);//添加表头信息if(dataList!=null&&dataList.size()0){//创建第一行标题,标题名字的本地信息通过resources从资源文件中获取HSSFRowrow=sheet.createRow((short)0);//建立新行this.createCell(row,0,style,HSSFCell.CELL_TYPE_STRING,序号);this.createCell(row,1,style,HSSFCell.CELL_TYPE_STRING,学号);this.createCell(row,2,style,HSSFCell.CELL_TYPE_STRING,姓名);this.createCell(row,3,style,HSSFCell.CELL_TYPE_STRING,成绩);//给excel填充数据for(inti=0;idataList.size();i++){//将dataList里面的数据取出来,假设这里取出来的是Model,也就是某个javaBean的意思啦Scoremodel=(Score)dataList.get(i);HSSFRowrow1=sheet.createRow((short)(i+1));//建立新行this.createCell(row1,0,style1,HSSFCell.CELL_TYPE_STRING,i+1);//设置序号if(model.getUser().getId()!=null){this.createCell(row1,1,style1,HSSFCell.CELL_TYPE_STRING,model.getUser().getId());//设置ID}if(model.getUser().getUname()!=null){this.createCell(row1,2,style1,HSSFCell.CELL_TYPE_STRING,model.getUser().getUname());//设置名称}if(model.getScore()!=null){this.createCell(row1,3,style1,HSSFCell.CELL_TYPE_STRING,model.getScore());//设置类别}}}else{this.createCell(sheet.createRow(0),0,style,HSSFCell.CELL_TYPE_STRING,查无资料);}}catch(Exceptione){e.printStackTrace();}returnworkbook;}privatevoidsetSheetColumnWidth(HSSFSheetsheet){//根据你数据里面的记录有多少列,就设置多少列sheet.setColumnWidth(0,3000);sheet.setColumnWidth(1,8000);sheet.setColumnWidth(2,8000);sheet.setColumnWidth(3,8000);}//设置excel的title表头样式privateHSSFCellStylecreateTitleStyle(HSSFWorkbookwb){//头部标题样式HSSFFontheadFont=wb.createFont();//设置字号headFont.setFontHeightInPoints((short)16);//设置字体headFont.setFontName(Arial);//设置加粗headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);HSSFCellStylestyle0=wb.createCellStyle();style0.setFont(headFont);//水平居中style0.setAlignment(HSSFCellStyle.ALIGN_CENTER_SEL
本文标题:java实现导出excel
链接地址:https://www.777doc.com/doc-3351512 .html