一个关于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();
}

原文地址:https://www.cnblogs.com/zhaoblog/p/5543571.html