日期:2014-05-17  浏览次数:20698 次

jsp中导出excle ,求大神指导
<body>
<form action="/BookInfoAction" method="post">
<table border="1" id="tb">
<tr>
<td>
书号
</td>
<td>
书名
</td>
<td>
作者
</td>
<td>
出版社
</td>
<td>
出版日期
</td>
<td>
操作
</td>
</tr>
<c:forEach var="book" items="${list}" begin="0" step="1">
<tr>
<td>
${book.bookId }
</td>
<td>
${book.bookName }
</td>
<td>
${book.bookAuthor }
</td>
<td>
${book.publishName }
</td>
<td>
${book.publishDate }
</td>
<td>
<input type="button" value="删除"
onclick="javascript:window.location='<%=basePath%>BookInfoAction.do?method=delete&bookId=${book.bookId}';">
<input type="button" value="更新"
onclick="javascript:window.location='<%=basePath%>BookInfoAction.do?method=findById&bookId=${book.bookId}';">
</td>
</tr>
</c:forEach>
</table>
</form>
以上是我的jsp代码,要求就是把action 传过来显示的table中的内容,导出到一个excle。。或者给点列子参考下

------解决方案--------------------
用POI很方便
将数据传到JSP页面
JSP页面代码大致如下
response.setContentType("application/vnd.ms-excel");
String fileName = java.net.URLEncoder.encode("报表名字.xls","UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + fileName);

//读取导出模板文件
    FileInputStream input = new FileInputStream(request.getRealPath("/files/export/模板名称.xls"));
   HSSFWorkbook wb = new HSSFWorkbook(input);
   HSSFSheet sheet = null;
   HSSFRow row = null;
   HSSFCell cell = null;
   int sheetIndex = 0;
   int starRow = 1;
   List<Map<String,String>> list = 数据
   sheet = wb.cloneSheet(0);
   wb.setSheetName(sheetIndex + 1, "Sheet" + sheetIndex);
   for(int i = 0; i < list.size(); i++){
   Map map = (Map)list.get(i);
starRow = starRow+1;
          sheet.shiftRows(starRow, sheet.getLastRowNum(), 1,true,false);
row = sheet.getRow(starRow);
cell = row.createCell(0);
cell.setCellValue(map.get("ordergamename")==null?"":map.get("ordergamename").toString());
cell = row.createCell(1);
cell.setCellValue(map.get("US") == null ? "" : map.get("US").toString());
cell = row.createCell(2);
cell.setCellValue(map.get("EU") == null ? "" : map.get("EU").toString());
cell = row.createCell(3);
cell.setCellValue(map.get("SAIA") == null ? "" : map.get("SAIA").toString());
cell = row.createCell(4);
cell.setCellValue(map.get("num") == null ? ""&nb