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. 【转】HDU-6035-Colorful Tree

    转自http://blog.csdn.net/Bahuia/article/details/76141574 题意: 题目链接:http://acm.hdu.edu.cn/showproblem.ph ...

  2. git远程相关

    git remote add origin git仓库地址 // 添加了远程仓库 git remote remove origin // 移除远程仓库 git push -u origin maste ...

  3. 决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:

    决策树python建模中的坑 代码 #coding=utf-8 from sklearn.feature_extraction import DictVectorizerimport csvfrom ...

  4. C++入门经典-例6.6-字符串复制

    1:字符串复制函数strcpy的格式如下: strcpy(字符数组名,字符串) 其作用是把字符串中的字符串复制到字符数组中.需要注意的是,字符串结束标志'\0'也一同被复制. 注意是将后面的内容复制给 ...

  5. 删除oracle居家必备

  6. EBS 页面影藏“关于此页”

    EBS环境: R12.1.3 问题:要影藏EBS登录页面左下角的“关于此页” 方法: 修改的配置文件参数:FND:诊断 , 由 是 改为 否 个性化自助定义 ,由 是 改为 否参数说明:‘FND:诊断 ...

  7. gcc编译错误

    使用boost的时候遇到一个链接错误 undefined reference to `boost::system::detail::generic_category_instance 出现这个问题的有 ...

  8. mysql replaceinto VS insertinto

    https://blog.csdn.net/t8116189520/article/details/78908975 所以,若想基于插入值累加是不行的,需要insert into https://ww ...

  9. Molecular Dynamics

    First]前处理 Second]运行MD Third]后处理 一.获得结构文件-来自实验数据或者某些化学软件工具 1]第一步: 获取并处理pdb文件 从Protein Data Bank下载小肽的p ...

  10. 【转】微信接口$GLOBALS['HTTP_RAW_POST_DATA']接收不到值

    $xml = $GLOBALS['HTTP_RAW_POST_DATA']; 打印$xml出来,结果是null 使用的是php7,原因是php7已移除这个全局变量,改成 $xml = file_get ...