<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。。或者给点列子参考下
<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。。或者给点列子参考下
OutputStream os = response.getOutputStream(); CsvWriter csv = new CsvWriter(os, ',', Charset.forName("GBK")); csv.writeRecord(new String[]
{ "操作人", "日志类型", "操作", "操作对象", "操作时间", "登陆ip" });
将数据传到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 ? "" : map.get("num").toString());
cell = row.createCell(5);
cell.setCellValue(map.get("complete") == null ? 0 : Double.valueOf(map.get("complete").toString()));
cell = row.createCell(6);
cell.setCellValue(map.get("unfinished")== null ? 0 : Double.valueOf(map.get("unfinished").toString()));
cell = row.createCell(7);
cell.setCellValue(map.get("money") == null ? "" : map.get("money").toString());
cell = row.createCell(8);
cell.setCellValue(map.get("beizhu")== null ? "" : map.get("beizhu").toString());
}
}
//选中第二个Sheet
wb.getSheetAt(1).setSelected(true);
//移除第一个Sheet,第一个Sheet为模板Sheet,需要移除
wb.removeSheetAt(0);
//以文件的形式输出到页面
out.clear();
out = pageContext.pushBody();
ServletOutputStream ouput = response.getOutputStream();
wb.write(ouput);
//关闭文件流等操作
wb = null;
sheet = null;
row = null;
cell = null;
input.close();
ouput.flush();
ouput.close();
<%@ page language="java" contentType="application/vnd.ms-excel;charset=UTF-8"%>
代码比较冗余。思路很简单。