先准备需要的参数

参数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的更多相关文章

  1. poi 导入导出的api说明(大全)

    原文链接:http://www.cnblogs.com/qingruihappy/p/8443101.html poi 导入导出的api说明(大全) 一. POI简介 ApachePOI是Apache ...

  2. POI导出EXCEL经典实现

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  3. java利用poi导出数据到excel

    背景: 上一篇写到利用jtds连接数据库获取对应的数据,本篇写怎样用poi将数据到处到excel中,此程序为Application 正文: 第三方poi jar包:poi驱动包下载 代码片段: /** ...

  4. POI导出Word插入复选框

    POI功能比较强大,但是有些不常用功能比如插入特殊符号,不知道API怎么调用 Word里要插入复选框,首先想到的是POI有没有提供现成的API,搜了一番,貌似都说不直接支持 http://stacko ...

  5. POI导出EXCEL经典实现(转)

    http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html 1.Apache POI简介 Apache POI是Apache软件基 ...

  6. Poi导出Excle

    场景 准备金系统需要从数据库读取大量数据存放到List集合中(可能还会做逻辑上的处理),并生成一个Excle文件,下载到客户本地. 问题一:客户体验 如果导出的文件比较大,比如几十万条数据,同步导出页 ...

  7. java poi导出多sheet页

    /** * @Title: exportExcel * @Description: 导出Excel的方法 * @param workbook * @param sheetNum (sheet的位置,0 ...

  8. Latex公式导出word,Latex转换MathML使用POI导出公式可编辑的Word文件

    背景 之前在 使用spire.doc导出支持编辑Latex公式的标准格式word 博客中写过,使用spire.doc来生成word,不得不说spire.doc的api操作起来还是比较方便,但是使用的过 ...

  9. poi导出excel

    Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...

随机推荐

  1. 手机上使用asmack开发xmpp客户端

    openfire服务端,smack:     下载地址:http://www.igniterealtime.org/downloads/index.jsp     源代码:http://www.ign ...

  2. centos 添加用户

    测试环境:CentOS 6.0 1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #adduser tommy //添加一个名为tommy的用户 #passwd tommy   // ...

  3. 大型分布式C++框架《一:框架简介》

    首先名字要取得霸气才能吸引人气,哈哈~~ 下面简单介绍下情况.框架是腾讯电商平台的分布式框架.虽然腾讯拍拍已经玩完了.但是这套框架还是很不错的.而且据原腾讯同事说微信也是用的这套框架.源码肯定是不能说 ...

  4. ORA-00314,redolog 损坏,或丢失处理方法

    alertsid.log报错信息: Fri Sep 27 15:18:39 2013 Started redo scan Fri Sep 27 15:18:39 2013 Errors in file ...

  5. java--字节数组输入、输出流

    在java网络编程中,字节数组很重要,它可以传输任何资料(文本,音频,视频,图片等),因此掌握字节数组和其它数据类型的相互转化尤为重要. 示例代码: package com.lky.util; imp ...

  6. PE基金的运作模式有哪些?

    一.信托制(1)信托型基金是由基金管理机构与信托公司合作设立,通过发起设立信托受益份额募集资金,然后进行投资运作的集合投资工具(2)信托公司和基金管理机构组成决策委员会实施,共同进行决策(3)在内部分 ...

  7. socket编程2

    package tcp; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOExceptio ...

  8. iOS - 排序的队列中插入数值

    http://stackoverflow.com/questions/8180115/nsmutablearray-add-object-with-order 用Selector http://sta ...

  9. RequireJS进阶(二)

    这一篇来认识下打包工具的paths参数,在入门一中就介绍了require.config方法的paths参数.用来配置jquery模块的文件名(jQuery作为AMD模块时id为“jquery”,但文件 ...

  10. BLOG PLUGINS

    文章分享按钮 (1)加网(JiaThis) (2)百度分享 文章关联推荐 每篇博文下面可以显示你博客中与该篇博文有些关联的几篇文章,也就是智能推荐,一方面可以增加你博文的曝光率和点击率,一方面也可以给 ...