使用jxl.jar将html中的表格导出为Excel形式,后台内容已取得,但下载下来的excel表为空是怎么回事?

晴儿2014 2016-03-08 04:33:53
后台代码
@RequestMapping(value = "/excel")
private void excel(HttpServletRequest request, HttpServletResponse response)
throws IOException{
request.setCharacterEncoding("UTF-8");
String content = request.getParameter("c");
if(content == null){
Writer out = response.getWriter();
response.setCharacterEncoding("UTF-8");
out.write("No Content");
return;
}
try{
export(content,response);
}catch(Exception ex){
ex.printStackTrace();
}
}

private void export(String content, HttpServletResponse response) throws
IOException,RowsExceededException,WriteException {
response.setContentType("application/ms-excel");
String sheetName = getCaption(content);
if(sheetName == null){
sheetName = "Sheet1";
}
sheetName = sheetName.replaceAll(":", "").replaceAll("[)]", "").replaceAll("[(]", "");
response.addHeader("Content-Disposition", "attachment; filename=" + new String(sheetName.getBytes("UTF-8"),"ISO-8859-1")+".xls");
OutputStream os = response.getOutputStream();
WritableWorkbook wwb = Workbook.createWorkbook(os);
wwb.setProtected(true);
WritableSheet sheet = wwb.createSheet(sheetName, 0);
int row = 0;
int col = 0;
Label label = null;
if(sheetName.trim().length() > 30){
label = new Label(col,row,sheetName);
sheet.addCell(label);
row++;
}
List<TD> listBody = getContent(content);
Map map = new HashMap();
for(TD td: listBody){
if(td == null){
row ++;
col = 0;
continue;
}
while (map.get(col + "-" + row) != null){
col++;
}
if(td.colspan >1 || td.rowspan >1){
sheet.mergeCells(col, row, col+ td.colspan-1, row+td.rowspan-1);
for(int i=col;i<=col+td.colspan-1;i++){
for(int j=row;j<=row+td.rowspan-1;j++){
map.put(i + "-" +j ,true);
}
}
}
label = new Label(col,row,td.content);
sheet.addCell(label);
map.put(col + "-"+row,true);
col += td.colspan;
}
wwb.write();
wwb.close();
}
private String getCaption(String content){
int begin = content.indexOf("<caption"); int end= content.indexOf("</caption>");
//int begin = content.indexOf("");
//int end = content.indexOf("");
if(begin == -1|| end == -1){
return null;
}
begin = content.indexOf(">", begin);
if(begin == -1){
return null;
}
return content.substring(begin+1, end);

}
public List<TD> getContent(String content)throws UnsupportedEncodingException{
int begin = -1;
int end = -1;
int index = -1;
String numberStr;
int number;
String[] tables = content.split("</table>");
List<TD> list = new ArrayList<TD>();
for(String table:tables){
String[] trs = table.split("</tr>");
for(String tr : trs){
number = 1;
String[] ss = tr.split("</td>");
for(String s : ss){
begin = s.indexOf("<td");
if(begin== -1)
{continue;}
s=s.substring(begin+3);
index=s.indexOf(">");
TD td = new TD();
begin = s.indexOf("rowSpan=");
if(begin != -1){
end = s.indexOf("",begin);
if(end == -1){
end = index;
}
numberStr = s.substring(begin+8, end).replace('"', ' ').replace('\'', ' ').trim();
number = Integer.parseInt(numberStr);
td.rowspan = number;
}
begin = s.indexOf("colSpan=");
if(begin != -1){
end = s.indexOf("", begin);
index = s.indexOf(">", begin);
if(end == -1){
end = index;
}
if(end > index){
end = index;
}
numberStr = s.substring(begin+8, end).replace('"', ' ').replace('\'', ' ').trim();
number = Integer.parseInt(numberStr);
td.colspan = number;
}
td.content = s.substring(index+1).replaceAll("<.*?> ", "").replaceAll("", "").trim();
list.add(td);
}
list.add(null);
}
list.add(null);
list.add(null);
}
return list;
}
public class TD {
int rowspan = 1;
int colspan = 1;
String content;
}

前台代码:
<script type="text/javascript">
function exportExcel(id){
var forum = document.forms["ExcelExportor"];
forum.c.value = eval(id+".innerHTML");
forum.submit();
}
</script>
<table border="1" id="MAIN_TABLE" class="altrowstable" style="width: 20em">
<tbody>
<tr><td>111111</td>
<td>222222</td>
</tr>
</tbody>
</table>
<form action="/repairs/excel" name="ExcelExportor" method="post">
<input type="hidden" name="c">
<input class='btn-style-02' type="button" value="导出Excel" onclick="exportExcel('MAIN_TABLE')">
</form>

...全文
250 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧