首先需要导入支持jar包
poi-3.11-20141221.jar
poi-ooxml-3.11-20141221.jar
poi-ooxml-schemas-3.11-20141221.jar
xmlbeans-2.6.0.jar
user类
数据表user
User.hbmxml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.user.entity.User" table="user">
<id name="id" type="java.lang.String">
<column name="id" length="32" />
<generator class="uuid.hex" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true" />
</property>
<property name="dept" type="java.lang.String">
<column name="dept" length="20" not-null="true" />
</property>
<property name="account" type="java.lang.String">
<column name="account" length="50" not-null="true" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="50" not-null="true" />
</property>
<property name="headImg" type="java.lang.String">
<column name="headImg" length="100" />
</property>
<property name="gender" type="character">
<column name="gender" length="1" />
</property>
<property name="email" type="java.lang.String">
<column name="email" length="50" />
</property>
<property name="mobile" type="java.lang.String">
<column name="mobile" length="20" />
</property>
<property name="birthday" type="java.util.Date">
<column name="birthday" length="10" />
</property>
<property name="state" type="java.lang.String">
<column name="state" length="1" />
</property>
<property name="memo" type="java.lang.String">
<column name="memo" length="200" />
</property>
</class>
</hibernate-mapping>
不懂的先来测试了解一下excel的用法
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
/*excel基础元素
工作簿
工作表(属于工作簿)
行(属于工作表)
单元格(属于行;由行和列确定)
-------------操作excel
1、创建/读取工作簿
2、创建/读取工作表
3、创建/读取行
4、创建/读取单元格
-----------excel样式
合并单元格对象(CellRangeAddress)属于工作簿;运用于工作表
CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) 起始行号,结束行号,起始列号,结束列号
样式是属于工作簿的;运用于单元格
字体是属于工作簿的;加载于样式;通用样式运用于单元格*/
public class TestPOI2Eecel {
//03版本创建工作簿excel设置内容
@Test
public void testWrite03() throws Exception{
//1、创建工作簿
HSSFWorkbook workbook=new HSSFWorkbook();
//2、创建工作表
HSSFSheet sheet=workbook.createSheet("hello word");
//3、创建行 第三行
HSSFRow row=sheet.createRow(2);
//4、创建单元格 第三行第三列
HSSFCell cell= row.createCell(2);
cell.setCellValue("hello word");
//输出到硬盘
FileOutputStream outputStream= new FileOutputStream("E:\\测试.xls");
//把Excel输出到具体地址
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
//03版本读取工作簿excel内容
@Test
public void testRead03() throws Exception{
//1、读取工作簿
FileInputStream inputStream= new FileInputStream("E:\\测试.xls");
HSSFWorkbook workbook=new HSSFWorkbook(inputStream);
//2、读取工作表
HSSFSheet sheet=workbook.getSheetAt(0);
//3、读取行 第三行
HSSFRow row=sheet.getRow(2);
//4、读取单元格 第三行第三列
HSSFCell cell= row.getCell(2);
System.out.println("03获取单元格内容:"+cell.getStringCellValue());
workbook.close();
inputStream.close();
}
//07版本创建工作簿excel设置内容
@Test
public void testWrite07() throws Exception{
//1、创建工作簿
XSSFWorkbook workbook=new XSSFWorkbook();
//2、创建工作表
XSSFSheet sheet=workbook.createSheet("hello word");
//3、创建行 第三行
XSSFRow row=sheet.createRow(2);
//4、创建单元格 第三行第三列
XSSFCell cell= row.createCell(2);
cell.setCellValue("hello word");
//输出到硬盘
FileOutputStream outputStream= new FileOutputStream("E:\\测试.xlsx");
//把Excel输出到具体地址
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
//07版本读取工作簿excel内容
@Test
public void testRead07() throws Exception{
//1、读取工作簿
FileInputStream inputStream= new FileInputStream("E:\\测试.xlsx");
XSSFWorkbook workbook=new XSSFWorkbook(inputStream);
//2、读取工作表
XSSFSheet sheet=workbook.getSheetAt(0);
//3、读取行 第三行
XSSFRow row=sheet.getRow(2);
//4、读取单元格 第三行第三列
XSSFCell cell= row.getCell(2);
System.out.println("07获取单元格内容:"+cell.getStringCellValue());
workbook.close();
inputStream.close();
}
//03 07版本读取工作簿excel内容 .xls后缀表示03版本 .xlsx后缀表示07版本
@Test
public void testRead03And07() throws Exception{
//1、读取工作簿
String fileName="E:\\测试.xls";
//是否是excel ^开始 $结束 ?i 忽略大小写 xls |xlsx 后缀名 \\.表示最后.
if (fileName.matches("^.+\\.(?i)((xls)|(xlsx))$"));
{ //用正则表达式判断是03版本
boolean is03Excel=fileName.matches("^.+\\.(?i)(xls)$");
FileInputStream inputStream=new FileInputStream(fileName);
Workbook workbook=is03Excel? new HSSFWorkbook(inputStream):new XSSFWorkbook(inputStream);
//2、读取工作表
Sheet sheet=workbook.getSheetAt(0);
//3、读取行 第三行
Row row=sheet.getRow(2);
//4、读取单元格 第三行第三列
Cell cell= row.getCell(2);
System.out.println("第三行第三列获取单元格内容:"+cell.getStringCellValue());
workbook.close();
inputStream.close();
}
}
//03版本创建工作簿excel设置内容样式
@Test
public void testWriteStyle() throws Exception{
//1、创建工作簿
HSSFWorkbook workbook=new HSSFWorkbook();
//1.1合并单元格 第三行3到5列 应用到工作表
CellRangeAddress cellRangeAddress= new CellRangeAddress(2,2,2,4); //起始行号,结束行号,起始列号,结束列号
//1.2样式 应用到单元格
HSSFCellStyle style=workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
//1.3创建字体 字体是属于工作簿的;加载于样式;通用样式运用于单元格
HSSFFont font= workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗字体
//font.setFontHeight((short)280); //设置字体大小 1/20
font.setFontHeightInPoints((short)16);
style.setFont(font);//加载于样式
// 1.4设置单元格背景
//设置背景模式 SOLID_FOREGROUND 背景以设置前景颜色一样
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//设置填充背景颜色
style.setBottomBorderColor(HSSFColor.YELLOW.index);
//设置填充前景颜色
//style.setFillForegroundColor(HSSFColor.YELLOW.index);//黄色背景
style.setFillForegroundColor(HSSFColor.RED.index);//红色背景
//2、创建工作表
HSSFSheet sheet=workbook.createSheet("hello word");
sheet.addMergedRegion(cellRangeAddress);
//3、创建行 第三行
HSSFRow row=sheet.createRow(2);
//4、创建单元格 第三行第三列
HSSFCell cell= row.createCell(2);
cell.setCellStyle(style);
cell.setCellValue("hello word");
//输出到硬盘
FileOutputStream outputStream= new FileOutputStream("E:\\测试.xls");
//把Excel输出到具体地址
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
}
搞懂上面就开始弄导出导入
前端页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户管理</title>
<%@include file="/common/header.jsp" %>
<script type="text/javascript">
//全选、全反选
function doSelectAll(){
// jquery 1.6 前
//$("input[name=selectedRow]").attr("checked", $("#selAll").is(":checked"));
//prop jquery 1.6+建议使用
$("input[name=selectedRow]").prop("checked", $("#selAll").is(":checked"));
}
//新增
function doAdd(){
document.forms[0].action = "${basePath}nsfw/user_addUI.action";
document.forms[0].submit();
}
//编辑
function doEdit(id){
document.forms[0].action = "${basePath}nsfw/user_editUI.action?user.id=" + id;
document.forms[0].submit();
}
//删除
function doDelete(id){
document.forms[0].action = "${basePath}nsfw/user_delete.action?user.id=" + id;
document.forms[0].submit();
}
//批量删除
function doDeleteAll(){
document.forms[0].action = "${basePath}nsfw/user_deleteSelected.action";
document.forms[0].submit();
}
//导出用户列表
function doExportExcel(){
window.open("${basePath}nsfw/user_exportExcel.action");
}
//导入
function doImportExcel(){
document.forms[0].action = "${basePath}nsfw/user_importExcel.action";
document.forms[0].submit();
}
</script>
</head>
<body class="rightBody">
<form name="form1" action="" method="post" enctype="multipart/form-data">
<div class="p_d_1">
<div class="p_d_1_1">
<div class="content_info">
<div class="c_crumbs"><div><b></b><strong>用户管理</strong></div> </div>
<div class="search_art">
<li>
用户名:<s:textfield name="user.name" cssClass="s_text" id="userName" cssStyle="width:160px;"/>
</li>
<li><input type="button" class="s_button" value="搜 索" onclick="doSearch()"/></li>
<li style="float:right;">
<input type="button" value="新增" class="s_button" onclick="doAdd()"/>
<input type="button" value="删除" class="s_button" onclick="doDeleteAll()"/>
<input type="button" value="导出" class="s_button" onclick="doExportExcel()"/>
<input name="userExcel" type="file"/>
<input type="button" value="导入" class="s_button" onclick="doImportExcel()"/>
</li>
</div>
<div class="t_list" style="margin:0px; border:0px none;">
<table width="100%" border="0">
<tr class="t_tit">
<td width="30" align="center"><input type="checkbox" id="selAll" onclick="doSelectAll()" /></td>
<td width="140" align="center">用户名</td>
<td width="140" align="center">帐号</td>
<td width="160" align="center">所属部门</td>
<td width="80" align="center">性别</td>
<td align="center">电子邮箱</td>
<td width="100" align="center">操作</td>
</tr>
<s:iterator value="userList" status="st">
<tr <s:if test="#st.odd">bgcolor="f8f8f8"</s:if> >
<td align="center"><input type="checkbox" name="selectedRow" value="<s:property value='id'/>" /></td>
<td align="center"><s:property value="name"/></td>
<td align="center"><s:property value="account"/></td>
<td align="center"><s:property value="dept"/></td>
<%-- <td align="center"><s:property value="gender?'男':'女'"/></td> --%>
<td align="center"><s:property value="gender"/></td>
<td align="center"><s:property value="email"/></td>
<td align="center">
<a href="javascript:doEdit('<s:property value='id'/>')">编辑</a>
<a href="javascript:doDelete('<s:property value='id'/>')">删除</a>
</td>
</tr>
</s:iterator>
</table>
</div>
</div>
<div class="c_pate" style="margin-top: 5px;">
<table width="100%" class="pageDown" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td align="right">
总共1条记录,当前第 1 页,共 1 页
<a href="#">上一页</a> <a href="#">下一页</a>
到 <input type="text" style="width: 30px;" onkeypress="if(event.keyCode == 13){doGoPage(this.value);}" min="1"
max="" value="1" />
</td>
</tr>
</table>
</div>
</div>
</div>
</form>
</body>
</html>
接下来写一个Useraction类
import java.io.File;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import cn.itcast.nsfw.user.entity.User;
import cn.itcast.nsfw.user.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
@Resource
private UserService userService;
private List<User> userList;
private User user;
private String[] selectedRow;
private File headImg;
private String headImgContentType;
private String headImgFileName;
private File userExcel;
private String userExcelContentType;
private String userExcelFileName;
//列表页面
public String listUI(){
userList = userService.findObjects();
return "listUI";
}
//跳转到新增页面
public String addUI(){
return "addUI";
}
//保存新增
public String add(){
try {
if(user != null){
//处理头像
if(headImg != null){
//1、保存头像到upload/user
//获取保存路径的绝对地址
String filePath = ServletActionContext.getServletContext().getRealPath("upload/user");
String fileName = UUID.randomUUID().toString().replaceAll("-", "") + headImgFileName.substring(headImgFileName.lastIndexOf("."));
//复制文件
File destFile=new File(filePath, fileName);
FileUtils.copyFile(headImg, destFile);
//2、设置用户头像路径
user.setHeadImg("user/" + fileName);
}
userService.save(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
//跳转到编辑页面
public String editUI(){
if (user != null && user.getId() != null) {
user = userService.findObjectById(user.getId());
}
return "editUI";
}
//保存编辑
public String edit(){
try {
if(user != null){
//处理头像
if(headImg != null){
//1、保存头像到upload/user
//获取保存路径的绝对地址
String filePath = ServletActionContext.getServletContext().getRealPath("upload/user");
String fileName = UUID.randomUUID().toString().replaceAll("-", "") + headImgFileName.substring(headImgFileName.lastIndexOf("."));
//复制文件
File destFile=new File(filePath, fileName);
FileUtils.copyFile(headImg, destFile);
//1.2删除原来头像
//13获取原来头像绝对路径
String oldPath = ServletActionContext.getServletContext().getRealPath("upload");
/**
* 因为原来路径为getRealPath("upload/user");但是保存时候user.setHeadImg("user/" + fileName);已经加了/user 所以上面获取upload就可以
*
* 复制头像到文件 相当于到upload/userw文件夹找到原来的头像 然后删除
*/
File oldFile=new File(oldPath, user.getHeadImg());
//删除
oldFile.delete();
//2、设置用户头像路径
user.setHeadImg("user/" + fileName);
}
userService.update(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
//删除
public String delete(){
if(user != null && user.getId() != null){
userService.delete(user.getId());
}
return "list";
}
//批量删除
public String deleteSelected(){
if(selectedRow != null){
for(String id: selectedRow){
userService.delete(id);
}
}
return "list";
}
//导出用户列表
public void exportExcel(){
try {
//1、查找用户列表
userList = userService.findObjects();
//2、导出
HttpServletResponse response = ServletActionContext.getResponse();
//告诉浏览器导出为excel文件类型
response.setContentType("application/x-execl");
//设置以浏览器打开方式并且设置文件名以及编码
response.setHeader("Content-Disposition", "attachment;filename=" + new String("用户列表.xls".getBytes(), "ISO-8859-1"));
//获取输出流
ServletOutputStream outputStream = response.getOutputStream();
//调用导出方法
userService.exportExcel(userList, outputStream);
if(outputStream != null){
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//导入用户列表
public String importExcel(){
//1、获取excel文件
if(userExcel != null){
//是否是excel ^开始 .+任意字符串 $结束 ?i 忽略大小写 xls | xlsx 后缀名 \\.表示最后.
if(userExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){
//2、导入
userService.importExcel(userExcel, userExcelFileName);
}
}
return "list";
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String[] getSelectedRow() {
return selectedRow;
}
public void setSelectedRow(String[] selectedRow) {
this.selectedRow = selectedRow;
}
public File getHeadImg() {
return headImg;
}
public void setHeadImg(File headImg) {
this.headImg = headImg;
}
public String getHeadImgContentType() {
return headImgContentType;
}
public void setHeadImgContentType(String headImgContentType) {
this.headImgContentType = headImgContentType;
}
public String getHeadImgFileName() {
return headImgFileName;
}
public void setHeadImgFileName(String headImgFileName) {
this.headImgFileName = headImgFileName;
}
public File getUserExcel() {
return userExcel;
}
public void setUserExcel(File userExcel) {
this.userExcel = userExcel;
}
public String getUserExcelContentType() {
return userExcelContentType;
}
public void setUserExcelContentType(String userExcelContentType) {
this.userExcelContentType = userExcelContentType;
}
public String getUserExcelFileName() {
return userExcelFileName;
}
public void setUserExcelFileName(String userExcelFileName) {
this.userExcelFileName = userExcelFileName;
}
}
接下来service层
import java.io.File;
import java.io.FileInputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import cn.itcast.core.util.ExcelUtil;
import cn.itcast.nsfw.user.dao.UserDao;
import cn.itcast.nsfw.user.entity.User;
import cn.itcast.nsfw.user.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
@Override
public void save(User user) {
userDao.save(user);
}
@Override
public void update(User user) {
userDao.update(user);
}
@Override
public void delete(Serializable id) {
userDao.delete(id);
}
@Override
public User findObjectById(Serializable id) {
return userDao.findObjectById(id);
}
@Override
public List<User> findObjects() {
return userDao.findObjects();
}
//实现导出
public void exportExcel(List<User> userList, ServletOutputStream outputStream) {
ExcelUtil.exportUserExcel(userList, outputStream);
}
//实现导入
public void importExcel(File userExcel, String userExcelFileName) {
try {
FileInputStream fileInputStream = new FileInputStream(userExcel);
//正则表达式判断excel为03版本 ^开始 $结束 ?i 忽略大小写 xls 后缀名 \\.最后. 03版本后缀为xls 07版本 xlsx
boolean is03Excel = userExcelFileName.matches("^.+\\.(?i)(xls)$");
//1、读取工作簿
Workbook workbook = is03Excel ? new HSSFWorkbook(fileInputStream):new XSSFWorkbook(fileInputStream);
//2、读取工作表
Sheet sheet = workbook.getSheetAt(0);
//3、读取行
if(sheet.getPhysicalNumberOfRows() > 2){
User user = null;
for(int k = 2; k < sheet.getPhysicalNumberOfRows(); k++){
//4、读取单元格
Row row = sheet.getRow(k);
user = new User();
//用户名
Cell cell0 = row.getCell(0);
user.setName(cell0.getStringCellValue());
//帐号
Cell cell1 = row.getCell(1);
user.setAccount(cell1.getStringCellValue());
//所属部门
Cell cell2 = row.getCell(2);
user.setDept(cell2.getStringCellValue());
//性别
Cell cell3 = row.getCell(3);
//user.setGender(cell3.getStringCellValue().equals("男"));
char ch=cell3.getStringCellValue().charAt(0); //将获取的字符串转为字符
System.out.println(ch);
user.setGender(ch);
//手机号
String mobile = "";
Cell cell4 = row.getCell(4);
try {
mobile = cell4.getStringCellValue();
} catch (Exception e) {
double dMobile = cell4.getNumericCellValue();
mobile = BigDecimal.valueOf(dMobile).toString();
}
user.setMobile(mobile);
//电子邮箱
Cell cell5 = row.getCell(5);
user.setEmail(cell5.getStringCellValue());
//生日
Cell cell6 = row.getCell(6);
if(cell6.getDateCellValue() != null){
user.setBirthday(cell6.getDateCellValue());
}
//默认用户密码为 123456
user.setPassword("123456");
//默认用户状态为 有效
user.setState(User.USER_STATE_VALID);
//5、保存用户
save(user);
}
}
workbook.close();
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
导出的方法抽取
package cn.itcast.core.util;
import java.util.List;
import javax.servlet.ServletOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import cn.itcast.nsfw.user.entity.User;
public class ExcelUtil {
/**
* 导出用户的所有列表到excel
* @param userList 用户列表
* @param outputStream 输出流
*/
public static void exportUserExcel(List<User> userList, ServletOutputStream outputStream) {
try {
//1、创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//1.1、创建合并单元格对象
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 4);//起始行号,结束行号,起始列号,结束列号
//1.2、头标题样式
HSSFCellStyle style1 = createCellStyle(workbook, (short)16);
//1.3、列标题样式
HSSFCellStyle style2 = createCellStyle(workbook, (short)13);
//2、创建工作表
HSSFSheet sheet = workbook.createSheet("用户列表");
//2.1、加载合并单元格对象
sheet.addMergedRegion(cellRangeAddress);
//设置默认列宽
sheet.setDefaultColumnWidth(25);
//3、创建行
//3.1、创建头标题行;并且设置头标题
HSSFRow row1 = sheet.createRow(0);
HSSFCell cell1 = row1.createCell(0);
//加载单元格样式
cell1.setCellStyle(style1);
cell1.setCellValue("用户列表");
//3.2、创建列标题行;并且设置列标题
HSSFRow row2 = sheet.createRow(1);
String[] titles = {"用户名","帐号", "所属部门", "性别", "电子邮箱"};
for(int i = 0; i < titles.length; i++){
HSSFCell cell2 = row2.createCell(i);
//加载单元格样式
cell2.setCellStyle(style2);
cell2.setCellValue(titles[i]);
}
//4、操作单元格;将用户列表写入excel
if(userList != null){
for(int j = 0; j < userList.size(); j++){
HSSFRow row = sheet.createRow(j+2);//因为前面已经2行了所以要+2
HSSFCell cell11 = row.createCell(0);
cell11.setCellValue(userList.get(j).getName());
HSSFCell cell12 = row.createCell(1);
cell12.setCellValue(userList.get(j).getAccount());
HSSFCell cell13 = row.createCell(2);
cell13.setCellValue(userList.get(j).getDept());
HSSFCell cell14 = row.createCell(3);
//cell14.setCellValue(userList.get(j).isGender()?"男":"女");
System.out.println(userList.get(j).getGender());
cell14.setCellValue(String.valueOf(userList.get(j).getGender())); //注意:要将字符转为字符串
HSSFCell cell15 = row.createCell(4);
cell15.setCellValue(userList.get(j).getEmail());
}
}
//5、输出
workbook.write(outputStream);
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建单元格样式
* @param workbook 工作簿
* @param fontSize 字体大小
* @return 单元格样式
*/
private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontSize) {
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
//创建字体
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗字体
font.setFontHeightInPoints(fontSize);
//加载字体
style.setFont(font);
return style;
}
}
user-struts.xml主要代码
<package name="user-action" namespace="/nsfw" extends="struts-default">
<action name="user_*" class="cn.user.action.UserAction" method="{1}">
<result name="{1}">/WEB-INF/jsp/nsfw/user/{1}.jsp</result>
<result name="list" type="redirectAction">
<param name="actionName">user_listUI</param>
</result>
</action>
</package>
要导入的excel文件内容
导入后