POI jar包下载 : http://poi.apache.org/download.html

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="${pageContext.request.contextPath}/lib/easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/lib/easyui/themes/icon.css" />
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/lib/easyui/demo/demo.css" />
<script type="text/javascript"
src="${pageContext.request.contextPath}/lib/easyui/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/lib/easyui/jquery.easyui.min.js"></script>
<style>
html,body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
} .easyui-datagrid {
position: absolute;
visibility: hidden;
}
</style> </head>
<body>
<table id="dg" class="easyui-datagrid"
style="width: 100%; height: auto"
data-options="
iconCls: 'icon-edit',
singleSelect: true,
toolbar: '#tb',
rownumbers:true,
fit:true,
pagination:true,
pageSize:10">
<thead>
<tr>
<th data-options="field:'ID',width:100,hidden:'true'">ID</th>
<th data-options="field:'userName',width:100,halign: 'center'">用户名</th>
<th data-options="field:'userCode',width:80,halign: 'center'">登陆名</th>
<th
data-options="field:'userRole',width:80,align:'right',halign: 'center'">角色</th>
</tr>
</thead>
</table> <div id="tb" style="padding: 5px; height: auto">
<div>
用户名: <input class="easyui-textbox" id="query-UserName"
style="width: 80px"> 登陆名: <input class="easyui-textbox"
id="query-UserCode" style="width: 80px"> 角色: <select
class="easyui-combobox" id="query-UserRole" name="state"
style="width: 100px;">
<option value="ALL">ALL</option>
<option value="管理员">管理员</option>
<option value="会员">会员</option>
<option value="收文员">收文员</option>
<option value="发文员">发文员</option>
</select> <a href="#" class="easyui-linkbutton" iconCls="icon-search"
onclick="query()">查询</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-export"
onclick="userExport()">导出</a>
</div>
</div> <script type="text/javascript">
$(function() {
query();
var p = $('#dg').datagrid('getPager');
$(p).pagination({
beforePageText : '第',//页数文本框前显示的汉字
afterPageText : '页 共 {pages} 页',
displayMsg : '第{from}到{to}条,共{total}条',
onSelectPage : function() {
//问题在这里,已经选择过的页数,再次选择不会触发onSelectPage事件,怎么取消这个缓存???
query();
}
});
});
</script>
<script type="text/javascript">
function userExport() {
var userName = $('#query-UserName').textbox('getValue');
var userCode = $('#query-UserCode').textbox('getValue');
var userRole = $('#query-UserRole').combobox('getText');
window.location.href = "/gwsfw/user/userExport?userName="
+ userName + "&userCode=" + userCode + "&userRole="
+ userRole;
} function query() {
var options = $("#dg").datagrid("getPager").data("pagination").options;
var curr = options.pageNumber;
// alert(curr);
var size = options.pageSize;
var userName = $('#query-UserName').textbox('getValue');
var userCode = $('#query-UserCode').textbox('getValue');
var userRole = $('#query-UserRole').combobox('getText');
$.ajax({
type : "POST",
url : "/gwsfw/user/getList",
dataType : "json",
data : JSON.stringify({
pageNumber : curr,
pageSize : size,
userName : userName,
userCode : userCode,
userRole : userRole
}),
contentType : "application/json", success : function(data) { $('#dg').datagrid('loadData', data);
},
error : function(err) {
alert("error");
}
}); } </script>
</body>
</html>

Controller 代码

 @RequestMapping(value = "/userExport")
public void userExport(
HttpServletRequest request,
@RequestParam(value = "userName", required = false) String userName,
@RequestParam(value = "userCode", required = false) String userCode,
@RequestParam(value = "userRole", required = false) String userRole,
HttpServletResponse response) throws Exception {
String strWhere = " where UserStatus=0 ";
if (!userName.equals(""))
strWhere += " and UserName='" + userName + "'";
if (!userRole.equals("ALL"))
strWhere += " and UserRole='" + userRole + "'";
if (!userCode.equals(""))
strWhere += " and UserCode='" + userCode + "'"; String[] titles = { "用户编号", "用户名", "登录名", "用户角色" };
HSSFWorkbook workbook = bll.createExcel(titles, strWhere); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); // 定义文件名格式 try {
// 定义excle名称 ISO-8859-1防止名称乱码
String msg = new String(
("用户信息_" + format.format(new Date()) + ".xls").getBytes(),
"ISO-8859-1");
// 以导出时间作为文件名
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment;filename="
+ msg);
workbook.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}

bll 代码 createExcel

 public HSSFWorkbook createExcel(String[] titles, String strWhere, String... parameter) throws Exception {
try {
// 第一步,创建一个workbook,对应一个Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet hssfSheet = workbook.createSheet("sheet1");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow hssfRow = hssfSheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
// 居中样式
hssfCellStyle.setAlignment(HorizontalAlignment.CENTER); HSSFCell hssfCell = null;
for (int i = 0; i < titles.length; i++) {
hssfCell = hssfRow.createCell(i);// 列索引从0开始
hssfCell.setCellValue(titles[i]);// 列名1
hssfCell.setCellStyle(hssfCellStyle);// 列居中显示
} // 第五步,写入实体数据 从数据库中获取List<User>
List<User> users = dao.queryUser(strWhere, parameter); if (users != null && !users.isEmpty()) {
for (int i = 0; i < users.size(); i++) {
hssfRow = hssfSheet.createRow(i + 1);
User user = users.get(i); // 第六步,创建单元格,并设置值
int userid = 0;
if (user.getID() != 0) {
userid = user.getID();
}
hssfRow.createCell(0).setCellValue(userid);
String username = "";
if (user.getUserName() != null) {
username = user.getUserName();
}
hssfRow.createCell(1).setCellValue(username);
String userCode = "";
if (user.getUserCode() != null) {
userCode = user.getUserCode();
}
hssfRow.createCell(2).setCellValue(userCode);
String userRole = "";
if (user.getUserRole() != null) {
userRole = user.getUserRole();
}
hssfRow.createCell(3).setCellValue(userRole);
}
}
return workbook; } catch (Exception e) {
e.printStackTrace();
throw new Exception("导出信息失败!");
} }

java springmvc poi 导出Excel,先简单记录,后期会详细描写的更多相关文章

  1. POI导出excel的简单demo

    目前使用过两种导出excel的方式,一种是如题所示的使用POI的方式进行数据的导出,这种方式一般只有在处理比较多的数据或者说需要导出的excel表格中有图片之类的需要特殊处理的文件的时候使用:还有一种 ...

  2. Java之POI导出Excel(一):单sheet

    相信在大部分的web项目中都会有导出导入Excel的需求,今天我们就来看看如何用Java代码去实现 用POI导出Excel表格. 一.pom引用 pom文件中,添加以下依赖 查看代码  <!-- ...

  3. java解决poi导出excel文字水印,导出excel不可操作问题

    首先需求是用户提出导出excel数据需使用水印备注其用途: 其实就是在导出excel的同时带有自定义文字水印的导出. 那么我们首先想到的肯定是以一个什么样的思路去解决该问题,首先查找poi导出exce ...

  4. java使用poi导出excel

    继上一篇导出pdf,这篇导出excel. 1.导入依赖 <dependency> <groupId>org.apache.poi</groupId> <art ...

  5. Java使用POI导出excel(下)——实例与小技巧

    [更新]:thinkgem的导出工具类: /** * Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesit ...

  6. Java之POI导出Excel(二):多个sheet

    相信在大部分的web项目中都会有导出导入Excel的需求,之前我也写过一篇导出单个sheet工作表的文章,没看过的小伙伴可以去看哈,链接也给大家放出来了:导出单个sheet 但是在我们日常的工作中,需 ...

  7. Java使用POI导出excel(上)——基本操作

    相关的介绍参考自:http://zc985552943.iteye.com/blog/1491546 一.概述 1.概念 受上文博文博主的启发,有必要先对excel的各个概念先做了解! //上述基本都 ...

  8. Java之Poi导出Excel文档

    一.Poi简介 在后台管理系统中,我们经常要做的导出操作,通常导出为Excel文档的形式,而Poi则提供了这种需要的支持. 二.Workbook/HSSFWorkbook/XSSFWorkbook 1 ...

  9. java用POI导出Excel

    架构SSM + Maven 一.添加依赖: <dependency> <groupId>org.apache.poi</groupId> <artifactI ...

随机推荐

  1. KMP的妙用(利用next数组寻找字符串的循环节)

    利用KMP的next数组的性质,我们可以找到next数组的循环节. 先说结论: 设字符串长n,则若其  i % ( i – next[n] ) == 0 ,则其有循环节(循环节数目大于1),其循环节数 ...

  2. python学习之路(17)

    sorted 排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个dict呢?直接比较数学上的大 ...

  3. C++入门经典-例2.4-使用scanf格式输入函数得到用户输入的数据

    1:puts函数可以输出提示信息的字符串. 2:代码如下: // 2.4.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" int main( ...

  4. 190707Python-MySQL

    一.Python连接MySQL import pymysql conn = pymysql.connect(host='192.168.100.4', port=3306, user='dongfei ...

  5. weka数据导入

    每一行代表一条数据,用逗号分开属性,最后一列为分类标签 将后缀名改为csv,用excel打开,为每一列加上属性名称,直接导入weka即可

  6. MyBatis如何传入多个参数

    一.单个参数 mapper public List<Test> getTestList(String id); xml <select id = "getTestList& ...

  7. 如何解决错误【selenium.common.exceptions.SessionNotCreatedException】

    如何解决错误[selenium.common.exceptions.SessionNotCreatedException]   [问题起因] 2018年12月26日晚,启动我的pycharm准备学习s ...

  8. demo-website配置记录

    demo-website环境配置, 主要是下载flask相关的模块: 1. python安装的是2.7版本. 2. pip install flask pip install flask-httpau ...

  9. 阶段3 3.SpringMVC·_01.SpringMVC概述及入门案例_06.入门案例的流程总结

    配置了load-on-startup等于1 表示启动了服务器就会去创建DispatcherServlet 如果不配置load-on-startup为1 那么第一次发送请求才会去创建Dispatcher ...

  10. Linux日志筛选命令

    (1)Linux目录操作命令 cd ..退出当前目录,返回上一级目录:cd / 退出当前目录,返回根目录: mkdir命令用于创建一个新的目录:rmdir命令功能删除指定的空目录. (2)Linux筛 ...