问题描述
哪个大神能帮我看看哪里有问题,为什么导不出来啊@RequestMapping(value="/dwHdmXls")@ResponseBodypublicStringuploadTempXls(HttpServletRequestreq,HttpServletResponseresp)throwsException{OutputStreamos=resp.getOutputStream();resp.setHeader("Content-Disposition","attachment;filename=DB-"+FORMATE.format(newDate())+".xls");resp.setCharacterEncoding("UTF-8");List<String>columns=newArrayList<String>();columns.add("ID");columns.add("Project");columns.add("Model");List<Object[]>data=newArrayList<Object[]>();List<StuInfo>stuList=stuService.findById(null);if(stuList!=null&&stuList.size()>0){for(StuInfostu:stuList){Object[]obj={stu.getId(),stu.getProjectName(),stu.getModelName};data.add(obj);}}ExcelUtil.createExcel(null,data,columns,os);returnnull;}
下面是创建Excel的方法publicstaticvoidcreateExcel(Stringtitle,List<Object[]>data,List<String>columns,OutputStreamos)throwsIOException{HSSFWorkbookworkbook=newHSSFWorkbook();HSSFSheetsheet=workbook.createSheet();/*设置表头的格式*/HSSFCellStylecellStyle=workbook.createCellStyle();cellStyle.setFillForegroundColor(HSSFColor.YELLOW.index);cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);cellStyle.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);cellStyle.setBorderBottom(HSSFBorderFormatting.BORDER_THIN);cellStyle.setBorderLeft((short)1);cellStyle.setBorderRight((short)1);cellStyle.setBorderTop((short)1);HSSFFontfont=workbook.createFont();font.setFontHeightInPoints((short)10);font.setFontName("宋体");font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);cellStyle.setFont(font);/*内容的样式*/HSSFCellStylecontentStyle=workbook.createCellStyle();//contentStyle.setFillForegroundColor(HSSFColor.YELLOW.index);//contentStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);contentStyle.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);contentStyle.setBorderBottom(HSSFBorderFormatting.BORDER_THIN);contentStyle.setBorderLeft((short)1);contentStyle.setBorderRight((short)1);contentStyle.setBorderTop((short)1);HSSFFontcontentfont=workbook.createFont();contentfont.setFontHeightInPoints((short)10);contentfont.setFontName("宋体");intindexRow=0;if(!isEmpty(title)){HSSFCellStyletitleStyle=workbook.createCellStyle();titleStyle.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);titleStyle.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);titleStyle.setBorderBottom(HSSFBorderFormatting.BORDER_THIN);titleStyle.setBorderLeft((short)1);titleStyle.setBorderRight((short)1);titleStyle.setBorderTop((short)1);titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);HSSFFonttitleFont=workbook.createFont();titleFont.setFontHeightInPoints((short)18);titleFont.setFontName("宋体");titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);titleStyle.setFont(titleFont);HSSFRowtitleRow=sheet.createRow(0);titleRow.setHeight((short)30);titleRow.setHeightInPoints(30);sheet.addMergedRegion(newCellRangeAddress(0,0,0,!isEmpty(columns)?(columns.size()>1)?columns.size()-1:1:1));Cellcell=titleRow.createCell(0);cell.setCellValue(newHSSFRichTextString(title));cell.setCellStyle(titleStyle);indexRow=1;}/*设置表头的行*/HSSFRowfirstRow=sheet.createRow(indexRow);/*设置表头*/if(columns!=null&&columns.size()>0){for(inti=0;i<columns.size();i++){HSSFCellcell=firstRow.createCell(i);cell.setCellType(HSSFCell.ENCODING_UTF_16);cell.setCellStyle(cellStyle);cell.setCellValue(newHSSFRichTextString(columns.get(i)));}}DateFormatdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");/*输出数据*/if(data!=null&&data.size()>0){for(inti=0;i<data.size();i++){HSSFRowrow=sheet.createRow(i+indexRow+1);Object[]objs=data.get(i);if(objs!=null&&objs.length>0){for(intj=0,k=objs.length;j<k;j++){HSSFCellcells=row.createCell(j);cells.setCellType(HSSFCell.ENCODING_UTF_16);if(!isEmpty(objs[j])&&objs[j]instanceofTimestamp){Timestamptime=(Timestamp)objs[j];cells.setCellStyle(contentStyle);cells.setCellValue(newHSSFRichTextString(df.format(time)));}elseif(!isEmpty(objs[j])&&(objs[j]instanceofInteger||objs[j]instanceofDouble)){contentStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);cells.setCellStyle(contentStyle);cells.setCellValue(Double.valueOf(objs[j].toString()));}else{cells.setCellStyle(contentStyle);cells.setCellValue(newHSSFRichTextString(!isEmpty(objs[j])?objs[j].toString():null));}sheet.autoSizeColumn(j);//调整每一列的宽度}}}}workbook.write(os);os.close();}
解决方案
本帖最后由 t261705606 于 2013-12-23 11:37:28 编辑
解决方案二:
有没有异常信息?断点跟进去看看啥情况
解决方案三:
报错误信息啊
解决方案四:
同上
解决方案五:
没有你的环境,你发一堆代码,什么说明也没有,别人怎么看。。
解决方案六:
createExcel把这个方法直接写到uploadTempXls方法里面去就好了,你调用得不对
解决方案七:
引用1楼defonds的回复:
有没有异常信息?断点跟进去看看啥情况
好了,这个方法和代码没有问题,是我的提交方法弄错了。本来是get的我写成post了,所以没反应。谢谢
解决方案八:
楼主客气。恭喜。
解决方案九:
什么是POI。