SSM和EasyUI导出表格

导出表格是我参观了别人的博客写出来的,有不好的地方请多多指教
这里只是导出普通的表格,没有设置样式,后期我会进一步的进行补充,同时,需要EasyUI框架,请留言,后期我会发出来
整个项目地址

  1. 首先,导出表格需要有jar包
    这些是需要导出表格的jar包
  2. 第二步就是搭建SSM框架,至于搭建框架的代码我就不发了,这里只发JSP页面的代码,和后台的代码,如需补充,请说明
  3. 这个是首页的样式,和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>
  1. 后台代码,用的是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导出表格,后期代码我会打包将整个项目发出来

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SSM框架中使用EasyUI作为首页,你可以按照以下步骤进行操作: 1. 首先,在pom.xml文件中添加EasyUI的依赖项: ```xml <dependency> <groupId>com.easyui</groupId> <artifactId>easyui</artifactId> <version>1.8.5</version> </dependency> ``` 2. 在Spring MVC的配置文件(通常是springmvc.xml)中配置EasyUI的资源路径: ```xml <mvc:resources mapping="/static/**" location="/static/" /> ``` 3. 在webapp目录下创建static文件夹,并将EasyUI的相关静态资源文件(包括js、css和图片)放入该文件夹中。 4. 在你的首页页面上引入EasyUI的资源文件,例如: ```html <link rel="stylesheet" type="text/css" href="/static/easyui/themes/default/easyui.css"> <link rel="stylesheet" type="text/css" href="/static/easyui/themes/icon.css"> <script type="text/javascript" src="/static/easyui/jquery.min.js"></script> <script type="text/javascript" src="/static/easyui/jquery.easyui.min.js"></script> ``` 5. 然后就可以使用EasyUI组件进行页面布局和交互了,例如: ```html <div id="dg" class="easyui-datagrid" style="width:700px;height:250px" url="getData.php" toolbar="#toolbar" pagination="true" rownumbers="true" fitColumns="true" singleSelect="true"> <thead> <tr> <th field="name" width="50">Name</th> <th field="email" width="50">Email</th> <th field="phone" width="50">Phone</th> </tr> </thead> </div> <div id="toolbar"> <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a> <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a> </div> <script type="text/javascript"> function newUser() { // TODO: Add new user logic here } function editUser() { // TODO: Edit user logic here } $(function() { $('#dg').datagrid(); }); </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值