一个关于poi导出的API
先准备需要的参数
参数1:String title=“用户信息”
参数2:String[] headers
String[] headers = { "用户名", "昵称", "部门","用户描述", "性别", "职责","地址","传真","邮件","家庭电话","移动电话","办公电话","用户优先级"};
参数3: List<T> dataset:
List<User> list = new ArrayList<User>();
if(!StringUtils.isEmpty(batchExp) && "yes".equals(batchExp)){//如果数量大于500,以500为单位分批导出
int startIndex = Integer.parseInt(request.getParameter("startIndex"));
list = userManager.getAllOrgUsersNoAdmin(buildSearch(),startIndex,500);
}else{
list = userManager.getAllOrgUsersNoAdmin(buildSearch(),-1,-1);
}
参数4:List<String> exportFile
List<String> exportFile = new ArrayList<String>();
exportFile.add("loginName");exportFile.add("realName");
exportFile.add("depName");
exportFile.add("userDescription");
exportFile.add("fileGender");exportFile.add("duty");exportFile.add("userAddress");
exportFile.add("userFax");exportFile.add("userEmail");exportFile.add("userHometel");
exportFile.add("userMobiletel");exportFile.add("userOfficetel");exportFile.add("userPriority");
参数5 OutputStream out
参数6 String pattern
public void exportExcel(String title, String[] headers,
List<T> dataset,List<String> exportFile, OutputStream out, String pattern) {
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 生成并设置另一个样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
// 声明一个画图的顶级管理器
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//产生表格标题行
HSSFRow row = sheet.createRow(0);
for (short i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
//遍历集合数据,产生数据行
Iterator<T> it = dataset.iterator();
int index = 0;
while (it.hasNext()) {index++;
row = sheet.createRow(index);
T t = (T) it.next();
int j = 0;
for (short i = 0; i < exportFile.size(); i++) {
try {
String name = exportFile.get(i);
Object value = BeanUtils.getProperty(t, name);
HSSFCell cell = row.createCell(j);
cell.setCellStyle(style2);
j++;
// 判断值的类型后进行强制类型转换
String textValue = null;
/*if (value instanceof Date) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
textValue = sdf.format(date);
} else {*/
// 其它数据类型都当作字符串简单处理
if (null != value)
textValue = value.toString();
//}
// 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
if (textValue != null) {
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Matcher matcher = p.matcher(textValue);
if (matcher.matches()) {
// 是数字当作double处理
cell.setCellValue(Double.parseDouble(textValue));
}else if(name.equals("qrCodePath")){
//属性名为qrCodePath即为图片路径信息
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedImage bufferImg = ImageIO.read(new File(textValue));
ImageIO.write(bufferImg, "PNG", byteArrayOut);
// 有图片时,设置行高为327px;
row.setHeightInPoints(327);
// 设置图片所在列宽度为195px,注意这里单位的一个换算
sheet.setColumnWidth(i, (short) (40 * 195));
// sheet.autoSizeColumn(i);
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 680, 95, (short) i, index, (short) i, index);
HSSFPicture pic = patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
pic.resize();
}else {
HSSFRichTextString richString = new HSSFRichTextString(textValue);
HSSFFont font3 = workbook.createFont();
font3.setColor(HSSFColor.BLACK.index);
richString.applyFont(font3);
cell.setCellValue(richString);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
try {
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}
public void exportExcel(String sheetName, String[] headers,
Collection<T> dataset, List<String> exportFile,
HttpServletResponse response) throws IOException {
OutputStream out = null;
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("gbk");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(sheetName.getBytes("GBK"), "ISO-8859-1") + ".xls");
out = response.getOutputStream();
HSSFWorkbook workbook = new HSSFWorkbook();
createSheet(sheetName, headers, dataset, exportFile, out, "yyyy-MM-dd",
workbook);
try {
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
out.flush();
out.close();
}
一个关于poi导出的API的更多相关文章
- poi 导入导出的api说明(大全)
原文链接:http://www.cnblogs.com/qingruihappy/p/8443101.html poi 导入导出的api说明(大全) 一. POI简介 ApachePOI是Apache ...
- POI导出EXCEL经典实现
1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...
- java利用poi导出数据到excel
背景: 上一篇写到利用jtds连接数据库获取对应的数据,本篇写怎样用poi将数据到处到excel中,此程序为Application 正文: 第三方poi jar包:poi驱动包下载 代码片段: /** ...
- POI导出Word插入复选框
POI功能比较强大,但是有些不常用功能比如插入特殊符号,不知道API怎么调用 Word里要插入复选框,首先想到的是POI有没有提供现成的API,搜了一番,貌似都说不直接支持 http://stacko ...
- POI导出EXCEL经典实现(转)
http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html 1.Apache POI简介 Apache POI是Apache软件基 ...
- Poi导出Excle
场景 准备金系统需要从数据库读取大量数据存放到List集合中(可能还会做逻辑上的处理),并生成一个Excle文件,下载到客户本地. 问题一:客户体验 如果导出的文件比较大,比如几十万条数据,同步导出页 ...
- java poi导出多sheet页
/** * @Title: exportExcel * @Description: 导出Excel的方法 * @param workbook * @param sheetNum (sheet的位置,0 ...
- Latex公式导出word,Latex转换MathML使用POI导出公式可编辑的Word文件
背景 之前在 使用spire.doc导出支持编辑Latex公式的标准格式word 博客中写过,使用spire.doc来生成word,不得不说spire.doc的api操作起来还是比较方便,但是使用的过 ...
- poi导出excel
Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...
随机推荐
- HDU_2028——求多个数的最小公倍数
Problem Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Output 为每组测试数据输出它们的最 ...
- 素数与素性测试(Miller-Rabin测试)
转载自Matrix大牛的博客 把代码翻译成C++ http://www.matrix67.com/blog/archives/234 题目链接: http://hihocoder.com/proble ...
- openstack 手动 部署安装调试
Virtual Interface creation failed
- MySQL事务处理2
MySQL5.X 都已经发布好久了,但是还有很多人认为MySQL是不支持事务处理的,这不得不怪他们是孤陋寡闻的,其实,只要你的MySQL版本支持BDB或 InnoDB表类型,那么你的MySQL就具有事 ...
- Eclipse颜色主题插件-Eclipse Color Theme
Eclipse颜色主题插件-Eclipse Color Theme 由于看烦了eclipse自带的的配色方案,自己动手配置又太麻烦,无意间在 http://eclipsecolorthemes.org ...
- thinkphp 中 使用七牛云上传
利用七牛云私有空间存储文件 第一步,注册七牛云,创建空间,将空间设为私有 需要记下的东西: AK,SK,bucket 第二步配置ThinkPHP 在config.php添加 'UPLOAD_SITEI ...
- OD: Exploit Me - Overwrite Return Address
修改邻接变量的方法对代码环境限制比较多,更通用.更强大的方法是修改 EBP.返回地址等状态值. 为了方便调试,修改之前的代码如下: #include<stdio.h> #include&l ...
- Web文件管理:elFinder.Net(支持FTP)
elFinder 是一个基于 Web 的文件管理器,灵感来自 Mac OS X 的 Finder 程序. elFinder.Net是.Net版本的一个Demo,使用ASP.NET MVC 4集成,可以 ...
- 基于java callable及future接口解决生产者消费者问题
这两天复习java线程时,把java里面的线程基本知识点与jdk1.5以后新添加的一些类的使用都了解了一下,借用生产者消费者的问题来将他们实践一下. 题目:(题目在csdn一大牛的空间找的) 生产者- ...
- 关于textView的字数限制
在一个项目中遇到texteView的字数限制,在iOS7.0上,会出现崩溃.我在这里栽了一个大跟头,废话不多说,下面直接贴代码吧. - (void)textViewDidChange:(UITextV ...