excle导出使用poi
package com.ittax.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.HSSFHeader;
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 com.ittax.nsfw.user.entity.User; /**
* excel工具类,支持批量导出
* @author lizewu
*
*/
public class ExcelUtil { /**
* 将用户的信息导入到excel文件中去
* @param userList 用户列表
* @param out 输出表
*/
public static void exportUserExcel(List<User> userList,ServletOutputStream out)
{
try{
//1.创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//1.1创建合并单元格对象
CellRangeAddress callRangeAddress = new CellRangeAddress(,,,);//起始行,结束行,起始列,结束列
//1.2头标题样式
HSSFCellStyle headStyle = createCellStyle(workbook,(short));
//1.3列标题样式
HSSFCellStyle colStyle = createCellStyle(workbook,(short));
//2.创建工作表
HSSFSheet sheet = workbook.createSheet("用户列表");
//2.1加载合并单元格对象
sheet.addMergedRegion(callRangeAddress);
//设置默认列宽
sheet.setDefaultColumnWidth();
//3.创建行
//3.1创建头标题行;并且设置头标题
HSSFRow row = sheet.createRow();
HSSFCell cell = row.createCell(); //加载单元格样式
cell.setCellStyle(headStyle);
cell.setCellValue("用户列表"); //3.2创建列标题;并且设置列标题
HSSFRow row2 = sheet.createRow();
String[] titles = {"用户名","账号","所属部门","性别","电子邮箱"};
for(int i=;i<titles.length;i++)
{
HSSFCell cell2 = row2.createCell(i);
//加载单元格样式
cell2.setCellStyle(colStyle);
cell2.setCellValue(titles[i]);
} //4.操作单元格;将用户列表写入excel
if(userList != null)
{
for(int j=;j<userList.size();j++)
{
//创建数据行,前面有两行,头标题行和列标题行
HSSFRow row3 = sheet.createRow(j+);
HSSFCell cell1 = row3.createCell();
cell1.setCellValue(userList.get(j).getName());
HSSFCell cell2 = row3.createCell();
cell2.setCellValue(userList.get(j).getAccount());
HSSFCell cell3 = row3.createCell();
cell3.setCellValue(userList.get(j).getDept());
HSSFCell cell4 = row3.createCell();
cell4.setCellValue(userList.get(j).isGender()?"男":"女");
HSSFCell cell5 = row3.createCell();
cell5.setCellValue(userList.get(j).getEmail());
}
}
//5.输出
workbook.write(out);
workbook.close();
//out.close();
}catch(Exception e)
{
e.printStackTrace();
}
} /**
*
* @param workbook
* @param fontsize
* @return 单元格样式
*/
private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontsize) {
// TODO Auto-generated method stub
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;
}
}
//导出用户列表
public void exportExcel()
{
try
{
//1.查找用户列表
userList = userService.findObjects();
//2.导出
HttpServletResponse response = ServletActionContext.getResponse();
//这里设置的文件格式是application/x-excel
response.setContentType("application/x-excel");
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()
{
if(userExcel!= null)
{
//判断是否是Excel文件
if(userExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$"))
{
userService.importExcel(userExcel, userExcelFileName);
}
}
return"list";
}
//导出用户列表
public void exportExcel()
{
try
{
//1.查找用户列表
userList = userService.findObjects();
//2.导出
HttpServletResponse response = ServletActionContext.getResponse();
//这里设置的文件格式是application/x-excel
response.setContentType("application/x-excel");
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()
{
if(userExcel!= null)
{
//判断是否是Excel文件
if(userExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$"))
{
userService.importExcel(userExcel, userExcelFileName);
}
}
return"list";
}

下载完后,打开“poi-bin-3.15-20160924.tar.gz”获取操作excel需要的jar包,并将这些jar包复制到项目中。对于只操作2003 及以前版本的excel,只需要poi-3.15.jar ,如果需要同时对2007及以后版本进行操作则需要复制
poi-ooxml-3.15.jar
poi-ooxml-schemas-3.15.jar
以及复制在ooxml-lib目录下的xmlbeans-2.6.0.jar(但不知为何,我下的jar文件中没有dom4j.jar)这个文件,还是加上dom4j.jar,防止报错.
excle导出使用poi的更多相关文章
- Excle导出优化(poi)
搜索词条 1.idea报java.lang.OutOfMemoryError: Java heap space怎么解决? 2.java.lang.OutOfMemoryError: GC overhe ...
- java实现excel的导入导出(poi详解)[转]
java实现excel的导入导出(poi详解) 博客分类: java技术 excel导出poijava 经过两天的研究,现在对excel导出有点心得了.我们使用的excel导出的jar包是poi这个 ...
- 021医疗项目-模块二:药品目录的导入导出-介绍poi类
我们使用的是.10版本 Apache POI - the Java API for Microsoft Documents,Apache POI 是用Java编写的免费开源的跨平台的 Java API ...
- java实现excel的导入导出(poi详解)
经过两天的研究,现在对excel导出有点心得了.我们使用的excel导出的jar包是poi这个阿帕奇公司的一个项目,后来被扩充了.是比较好用的excel导出工具. 下面来认识一下这个它吧. 我们知道要 ...
- JAVA实用案例之文件导入导出(POI方式)
1.介绍 java实现文件的导入导出数据库,目前在大部分系统中是比较常见的功能了,今天写个小demo来理解其原理,没接触过的同学也可以看看参考下. 目前我所接触过的导入导出技术主要有POI和iRepo ...
- Java中导入导出Excel -- POI技术
一.介绍: 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实 ...
- 关于excel导入、导出(POI)
当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际的开发中, ...
- Java操作Excle(基于Poi)
有一次有个同事问我会不会有java操作Excle,回答当然是不会了!感觉被嘲讽了,于是开始寻找度娘,找到个小例子,结果越写越有意思,最后就成就了这个工具类. import java.io.Buffer ...
- Excel导入导出工具——POI XSSF的使用
工具简介 POI是Apache提供的一款用于处理Microsoft Office的插件,它可以读写Excel.Word.PowerPoint.Visio等格式的文件. 其中XSSF是poi对Excel ...
随机推荐
- 文件锁简单操作(lockfileEx\unlockfileEx)
#include "stdafx.h"#include <Windows.h>#include <iostream> using namespace std ...
- tomcat urlwrite报错
十二月 26, 2017 2:15:30 下午 org.apache.catalina.core.ApplicationContext logINFO: org.tuckey.web.filters. ...
- 人工智能二之Sublime Text3环境配置
1.在Ubuntu中按CTRL+ALT+T打开命令窗口,按下面步骤和命令进行安装即可: 添加sublime text 3的仓库: sudo add-apt-repository ppa:webupd8 ...
- jQuery-图片的放大镜显示效果(不需要大小图)
问题:当页面高度很大时,放大图片的图层不会跟随着 1.demo.html ;display:none;} #tip s {position:absolute;top:40px;l ...
- 【oracle】首次启动SQL Developer配置java.exe出错(Could not find jvm.cfg! )
1.环境 win7/8/8.1 x64,Oracle 11g r2,jdk7 x64 2.问题 第一次启动Oracle SQL Developer的时候会让我们填写Java.exe的路径,我在jdk ...
- powerdesigner自动将name填充到注释的脚本
我在建模的时候,希望在生成脚本的时候有注释,所以才会看到Comment列,实际上,只要你的表中的Name列不为空,运行下面的VBScript,PD会帮你自动填充注释的Comment列值. '把pd中那 ...
- C++——virtual
一.放在父类的函数名前面:多态 1.作用:实现多态:子类可以自定义父类中的virtual函数 #include <iostream> using namespace std; class ...
- C++标准库vector以及迭代器
今天看C++的书,出现了一个新的概念,容器vector以及容器迭代器. vector是同一种对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库将负责管理与存储元素相关的类存. ...
- IDEA工作中常用快捷键
ctrl+shift+t: Ubuntu中在一个工具栏中打开两个终端 shift+shift: 搜索任何类 ctrl+N: 搜索任何类 ctrl+right: forward----自定义 ctrl+ ...
- About English Web Site Font
Which font will you choose when develop one website! Actually , I have no idea about this! If you ha ...