一个关于poi导出的API

Posted xue123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个关于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的主要内容,如果未能解决你的问题,请参考以下文章

关于 java poi 导出excel时 导出多个sheet的问题

java poi 导出excel

关于java poi导出excel弹出下载框无法弹出的问题

SpringBoot加Poi仿照EasyPoi实现Excel导出

是在java中,如何用Poi导出excel,导出的是一个jsp页面的列表,并且,POI.jar包如何下载?

使用poi导出Excel