导出表格是我参观了别人的博客写出来的,有不好的地方请多多指教
这里只是导出普通的表格,没有设置样式,后期我会进一步的进行补充,同时,需要EasyUI框架,请留言,后期我会发出来
整个项目地址
- 首先,导出表格需要有jar包
- 第二步就是搭建SSM框架,至于搭建框架的代码我就不发了,这里只发JSP页面的代码,和后台的代码,如需补充,请说明
- 这个是首页的样式,和jsp页面的代码,页面采用了EasyUI。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css"
href="jquery-easyui-1.5.5.2/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="jquery-easyui-1.5.5.2/themes/icon.css">
<script type="text/javascript" src="jquery-easyui-1.5.5.2/jquery.min.js"></script>
<script type="text/javascript"
src="jquery-easyui-1.5.5.2/jquery.easyui.min.js"></script>
</head>
<body align="center">
<div id="p" class="easyui-panel" title="学生数据"
style="width: 800px; height: 300px; padding: 10px; background: #fafafa;"
data-options="iconCls:'icon-save',closable:true,
collapsible:true,minimizable:true,maximizable:true">
<div id="dd" style="width: 700px; height: 200px;">
<table class="easyui-datagrid" style="">
<thead>
<tr>
<th data-options="field:'code'">学号</th>
<th data-options="field:'name'">姓名</th>
<th data-options="field:'price'">性别</th>
<th data-options="field:'cao'">操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list }" var="li">
<tr>
<td>${li.id }</td>
<td>${li.name }</td>
<td>${li.sex }</td>
<td><a href="${pageContext.request.contextPath }/biao.do">导出Excel表格</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<script>
$('#p').panel({
width : 800,
height : 300,
title : '学生信息表',
tools : [ {
iconCls : 'icon-add',
handler : function() {
alert('new')
}
}, {
iconCls : 'icon-save',
handler : function() {
alert('save')
}
} ]
});
</script>
</div>
</body>
</html>
- 后台代码,用的是SSM框架
package controller;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Students;
import services.StuService;
@Controller
@RequestMapping("/")
public class Control {
@Resource(name="ss")
StuService s;
//导出表格
@RequestMapping(value = "/biao")
public void getAll(HttpServletResponse response) throws IOException {
System.out.println("导出表格");
//1.获取数据
List<Students> list=s.getAll();
System.out.println(list);
//2.创建excel表格
HSSFWorkbook wb=new HSSFWorkbook();
//3.创建sheet页
HSSFSheet sheet=wb.createSheet("员工信息表");
//4.创建标题行
HSSFRow titleRow=sheet.createRow(0);
titleRow.createCell(0).setCellValue("学号");
titleRow.createCell(1).setCellValue("姓名");
titleRow.createCell(2).setCellValue("性别");
//5.编列将数据放到Excel列中
for(Students sd:list) {
HSSFRow dataRow =sheet.createRow(sheet.getLastRowNum()+1);
dataRow.createCell(0).setCellValue(sd.getId());
dataRow.createCell(1).setCellValue(sd.getName());
dataRow.createCell(2).setCellValue(sd.getSex());
}
//6.设置下载时客户端Excal的名称
response.setContentType("application/octet-stream;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String("员工名单".getBytes(),"iso-8859-1") + ".xls");
OutputStream out=response.getOutputStream();
wb.write(out);
out.flush();
out.close();//关闭连接
}
//查询
@RequestMapping(value="/cha",method=RequestMethod.GET)
public String cha(Model mo){
System.out.println("进来");
List<Students> list=s.getAll();
mo.addAttribute("list", list);
return "/NewFile.jsp";
}
}
以上就是ssm导出表格,后期代码我会打包将整个项目发出来