java EasyUI导出当前页和导出模板

环境:easyUi框架和ssm集成框架,java语言 ,maven管理jar包,tomcat服务器版本8.5 jdk1.8

案例:

导入poi依赖

<dependency>

         <groupId>org.apache.poi</groupId>

         <artifactId>poi</artifactId>

         <version>3.10.1</version>

      </dependency>

controller

 @RequestMapping("/excel")
   public ModelAndView exportUser(@RequestParam(value="page",defaultValue="1")Integer page,
		   @RequestParam(value="rows",defaultValue="5") Integer rows) {
	   System.out.println("aaa");
	   ModelAndView mv = new ModelAndView();
	   EasyUIpage uipage = userService.queryAll(page,rows);
	   List<User>users = (List<User>)uipage.getRows();
	   mv.addObject("userList",users);
	   mv.setViewName("export-user");
	   return mv;
   }

自定义excel 视图

public class UserExcelView extends AbstractExcelView {
    /**
     * 以下完成视图的渲染
     */
    @Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 从model对象中获取userList
        @SuppressWarnings("unchecked")
        List<User> userList = (List<User>) model.get("userList");
        // 创建Excel的sheet
        HSSFSheet sheet = workbook.createSheet("会员列表");

        // 创建标题行
        HSSFRow header = sheet.createRow(0);
        header.createCell(0).setCellValue("ID");
        header.createCell(1).setCellValue("用户名");
        header.createCell(2).setCellValue("姓名");
        header.createCell(3).setCellValue("年龄");
        header.createCell(4).setCellValue("性别");
        header.createCell(5).setCellValue("出生日期");
        header.createCell(6).setCellValue("创建时间");
        header.createCell(7).setCellValue("更新时间");

        // 填充数据
        int rowNum = 1;
        for (User user : userList) {
            HSSFRow row = sheet.createRow(rowNum);
            row.createCell(0).setCellValue(user.getId());
            row.createCell(1).setCellValue(user.getUserName());
            row.createCell(2).setCellValue(user.getName());
            row.createCell(3).setCellValue(user.getAge());
            String sexStr;
            if (user.getSex() == 1) {
                sexStr = "男";
            } else if (user.getSex() == 2) {
                sexStr = "女";
            } else {
                sexStr = "未知";
            }
            row.createCell(4).setCellValue(sexStr);
            row.createCell(5).setCellValue(new DateTime(user.getBirthday()).toString(Constants.DATE));
            row.createCell(6).setCellValue(new DateTime(user.getCreated()).toString(Constants.DATE_TIME));
            row.createCell(7).setCellValue(new DateTime(user.getUpdated()).toString(Constants.DATE_TIME));

            rowNum++;
        }
        
        // 设置相应头信息,以附件形式下载并且指定文件名
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String("会员列表.xls".getBytes(), "ISO-8859-1"));
    }

}

分页实体

public class EasyUIpage {
private Long total;
private List<?> rows;
public Long getTotal() {
	return total;
}
public void setTotal(Long total) {
	this.total = total;
}
public List<?> getRows() {
	return rows;
}
public void setRows(List<?> rows) {
	this.rows = rows;
}
@Override
public String toString() {
	return "EasyUIpage [total=" + total + ", rows=" + rows + "]";
}

}

jsp页面(直接附上整个小案例页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>会员管理系统</title>
<link rel="stylesheet" type="text/css" href="../js/jquery-easyui-1.4/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../js/jquery-easyui-1.4/themes/icon.css" />
<script type="text/javascript" src="../js/jquery-easyui-1.4/jquery.min.js"></script>
<script type="text/javascript" src="../js/jquery-easyui-1.4/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../js/jquery-easyui-1.4/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../js/common.js"></script>
</head>
<body>
	<div>
    <table class="easyui-datagrid" id="userList" title="会员列表" 
	       data-options="singleSelect:false,collapsible:true,pagination:true,url:'/Integration/userController/select',method:'post',pageSize:5,toolbar:toolbar,pageList:[2,5,10]">
	    <thead>
	        <tr>
	        	<th data-options="field:'ck',checkbox:true"></th>
	        	<th data-options="field:'id',width:60">ID</th>
	            <th data-options="field:'userName',width:200">用户名</th>
	            <th data-options="field:'name',width:100">姓名</th>
	            <th data-options="field:'age',width:100">年龄</th>
	            <th data-options="field:'sex',width:80,align:'right',formatter:formatSet">性别</th>
	            <th data-options="field:'birthday',width:80,align:'right',formatter:formatBirthday">出生日期</th>
	            <th data-options="field:'created',width:130,align:'center',formatter:formatDate">创建日期</th>
	            <th data-options="field:'updated',width:130,align:'center',formatter:formatDate">更新日期</th>
	        </tr>
	    </thead>
	</table>
	</div>
<div id="userAdd" class="easyui-window" title="新增会员" data-options="modal:true,closed:true,iconCls:'icon-save',href:'/Integration/to/user-add'" style="width:800px;height:600px;padding:10px;">
</div>
<div id="userUpadte"  title="修改会员" style="width:800px;height:600px;padding:10px;">
</div>
<!-- <div id="delete" class="easyui-window" title="删除会员" data-options="modal:true,closed:true,iconCls:'icon-save',href:'/Integration/to/delete'" style="width:800px;height:600px;padding:10px;">
        The window content.
</div> -->
<script type="text/javascript">
function formatDate(val,row){
	var now = new Date(val);
	return now.format("yyyy-MM-dd hh:mm:ss");
}
function formatBirthday(val,row){
	var now = new Date(val);
	return now.format("yyyy-MM-dd");
}
function formatSet(val,row){
	if(val == 1){
		return "男";
	}else if(val == 2){
		return "女";
	}else{
		return "未知";
	}
}
function getSelectionsIds(){
	var userList = $("#userList");
	var sels = userList.datagrid("getSelections");
	var ids = [];
	for(var i in sels){
		ids.push(sels[i].id);
	}
	ids = ids.join(",");
	return ids;
}
var toolbar = [{
    text:'新增',
    iconCls:'icon-add',
    handler:function(){
    	$('#userAdd').window('open');
    }
},{
text:'编辑',
iconCls:'icon-edit',
handler:function(){
	var ids = getSelectionsIds();
	if(ids.length == 0){
		$.messager.alert('提示','未选中用户!');
		return ;
	}
	if(ids.indexOf(',')>0){
		$.messager.alert('提示','不能选中多个用户');
		return ;
	}
	$('#userUpadte').window({
		href:"/Integration/to/update",
		onLoad:function(){
			var data=$('#userList').datagrid('getSelections')[0];
			$('#userUpadte').form('load',data);
		}
	}).window('open');
	
	
}
},{
    text:'删除',
    iconCls:'icon-cancel',
    handler:function(){
    	var ids = getSelectionsIds();
    	alert(ids);
    	if(ids.length == 0){
    		$.messager.alert('提示','未选中用户!');
    		return ;
    	}
    	$.messager.confirm('确认','确定删除ID为 '+ids+' 的会员吗?',function(r){
    	    if (r){
            	$.post("/Integration/userController/delete",{'ids':ids}, function(data){
        			if(data.status == 200){
        				$.messager.alert('提示','删除会员成功!',undefined,function(){
        					$("#userList").datagrid("reload");
        				});
        			}
        		});
    	    }
    	});
    }
},'-',{
    text:'导出',
    iconCls:'icon-remove',
    handler:function(){
    	var optins = $("#userList").datagrid("getPager").data("pagination").options;
    	var page = optins.pageNumber;
    	var rows = optins.pageSize;
    	$("<form>").attr({
    		"action":"${pageContext.request.contextPath}/userController/excel",
    		"method":"POST"
    	}).append("<input type='hidden' name='page' value='"+page+"'/>")//导出当前页
    	.append("<input type='hidden' name='rows' value='"+rows+"'/>").appendTo("body").submit();
    }
}];
</script>
</body>
</html>

service

public EasyUIpage queryAll(Integer pageNum,Integer pageSize ) {
		EasyUIpage uiPage = new EasyUIpage();
		PageHelper.startPage(pageNum,pageSize);
		List<User> users = userMapper.selectAll();
		PageInfo<User> pageinfo = new PageInfo<User>(users);
		uiPage.setRows(pageinfo.getList());
		uiPage.setTotal(pageinfo.getTotal());
		return uiPage;
	}

mapper

@Select("select * from tb_user")
	public List<User> selectAll()

导出模板

只需要将自定义视图稍作修改即可

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值