jsp页面导出Excel 导出内容为当前页面table里的内容,谢谢!急用,在线等。
1个回答
2017-06-01
展开全部
看看下面的代码,应该对你有帮助:0){buffout.write(buffer,0,count);}buffin.close();buffout.close();finput.close();output.close();}catch(Exceptione){e.printStackTrace();}finally{FilesrcExcel=newFile(excelBean.getPath());srcExcel.delete();}%>=============CreateExcelST.java======================packagenet.createExcel;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.apache.poi.hssf.usermodel.*;importorg.apache.poi.hssf.util.Region;importorg.apache.poi.hssf.util.HSSFColor;importjava.util.List;importjava.io.FileOutputStream;importnet.ExcelBean;/***User:marlboroDate:2008-2-15Time:10:15:38**@authorMarlboro*@mailtomarlboro027@gmail.com*/publicclassCreateExcelST{privateLoglog=LogFactory.getFactory().getInstance(this.getClass().getName());@SuppressWarnings("unchecked")publicvoidcreateExcelFile(ExcelBeanexcelBean)throwsException{//创建工作薄HSSFWorkbookwb=newHSSFWorkbook();HSSFSheetsheet=wb.createSheet(excelBean.getSheetName());//打印页面设置HSSFPrintSetupps=sheet.getPrintSetup();sheet.setMargin(HSSFSheet.BottomMargin,0.5);//页边距sheet.setMargin(HSSFSheet.LeftMargin,0.1);sheet.setMargin(HSSFSheet.RightMargin,0.1);sheet.setMargin(HSSFSheet.TopMargin,0.5);ps.setLandscape(true);//打印方向,true:横向,false:纵向ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);//纸张//设置列宽this.setColumnWidth(sheet,excelBean.getColumnWidth());//标题栏设置字体HSSFFontcellFontReport=wb.createFont();cellFontReport.setFontHeightInPoints((short)16);//字号cellFontReport.setBoldweight(HSSFFont.U_SINGLE);//加粗cellFontReport.setFontName("CourierNew");//字段栏设置字体HSSFFontcellFontColumn=wb.createFont();cellFontColumn.setFontHeightInPoints((short)12);//字号cellFontColumn.setBoldweight(HSSFFont.U_SINGLE);//加粗cellFontColumn.setFontName("CourierNew");cellFontColumn.setColor(HSSFFont.SS_NONE);//设置字体HSSFFontcellFont=wb.createFont();cellFont.setFontHeightInPoints((short)10);//字号cellFont.setBoldweight(HSSFFont.U_SINGLE);//加粗cellFont.setFontName("CourierNew");//自定义颜色HSSFPalettepalette=wb.getCustomPalette();palette.setColorAtIndex(HSSFColor.AQUA.index,(byte)252,(byte)254,(byte)236);//设置标题栏单元格格式HSSFCellStylecellStyleReport=wb.createCellStyle();cellStyleReport.setFont(cellFontReport);cellStyleReport.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中cellStyleReport.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中cellStyleReport.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框cellStyleReport.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框cellStyleReport.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框cellStyleReport.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框cellStyleReport.setWrapText(true);//自动换行cellStyleReport.setFillForegroundColor(HSSFColor.AQUA.index);cellStyleReport.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置标题栏单元格格式HSSFCellStylecellStyleColumn=wb.createCellStyle();cellStyleColumn.setFont(cellFontColumn);cellStyleColumn.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中cellStyleColumn.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中cellStyleColumn.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框cellStyleColumn.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框cellStyleColumn.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框cellStyleColumn.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框cellStyleColumn.setWrapText(true);//自动换行cellStyleColumn.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);cellStyleColumn.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置单元格格式HSSFCellStylecellStyle=wb.createCellStyle();cellStyle.setFont(cellFont);cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框cellStyle.setWrapText(true);//自动换行ListdataList=excelBean.getDataList();if(null!=dataList){//报表的标题ListtitleList=excelBean.getTitleList();//创建行设置报表名称HSSFRowrowReportName=sheet.createRow(0);HSSFCellcellReportName=rowReportName.createCell((short)0);sheet.addMergedRegion(newRegion(0,(short)0,0,(short)(titleList.size()-1)));HSSFRichTextStringreportString=newHSSFRichTextString(excelBean.getReportName());cellReportName.setCellValue(reportString);cellReportName.setCellStyle(cellStyleReport);HSSFCellendReportName=rowReportName.createCell((short)(titleList.size()-1));endReportName.setCellStyle(cellStyleReport);//创建行HSSFRowrowTitle=sheet.createRow(1);for(inti=0;i>>>>>>>>>>>>>>>.===>>>>>>"+excelBean.getPath());wb.write(fileOut);fileOut.close();}catch(Exceptione){thrownewException("文件已经打开,请关闭后再生成");}}publicvoidsetColumnWidth(HSSFSheetsheet,int[]width){for(inti=0;i<width.length;i++){sheet.setColumnWidth((short)i,(short)(width[i]*100));}}}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询