日期:2014-05-16  浏览次数:20467 次

JSP 无模板导出功能实现
HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/octet-stream");
		exportName = "PIS-品牌重合度查询结果"+DateUtils.formatDate(DateUtils.getCurrentDate(), "yyyy-MM-dd");
		String encodeName=URLEncoder.encode(exportName,"UTF-8");
		response.setHeader("Content-Disposition",
				"attachment;filename="+encodeName+".xls");
		HSSFWorkbook book = new HSSFWorkbook();
		HSSFSheet sheet = book.createSheet("brandStatisticsResult");
		
		HSSFCellStyle cellStyle_text = book.createCellStyle();
		cellStyle_text.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle_text.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle_text.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle_text.setBorderBottom(HSSFCellStyle.BORDER_THIN);	
		
		int rowCount = 0;
		int colCount = 0;
		for (List<String> xList : colAll) {
			HSSFRow row = sheet.createRow(rowCount);
			colCount = 0;
			for(String ceString : xList){
				HSSFCell cell = row.createCell( colCount );
				cell.setCellValue(ceString);
				colCount++;
			}
			rowCount++;
		}
		
		book.write(response.getOutputStream());
		}catch (Exception e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		return null;

?其中 List<List<String>> list =new ArrayList<List<String>>();

标题可以先设置

?

List<String> title = new List<String>;

title.add("title1");

title.add("title2");

title.add("title3");

list.addAll(title)

在页面上document.openUrl("actionurl")

?

就可以得到下载的内容了

?