JavaBean数据导出excel与csv文件

/**

导出excel文件,文件操作使用Apache POI框架

**/

public static <E> void exportExcel(HttpServletResponse response, String[] header, int[] column, String[] fileNames, List<E> list, String excelName) {
// 创建工作簿
HSSFWorkbook wb = new HSSFWorkbook();
// 创建一个sheet
HSSFSheet sheet = wb.createSheet(excelName);

HSSFRow headerRow = sheet.createRow(0);
HSSFRow contentRow = null;

// 设置标题
for (int i = 0; i < header.length; i++) {
headerRow.createCell(i).setCellValue(header[i]);
if (column != null) {
sheet.setColumnWidth(i, column[i]);
}
}
try {
int size = list.size();
for (int i = 0; i < size; i++) {
contentRow = sheet.createRow(i + 1);
// 获取每一个对象
E o = list.get(i);
Class cls = o.getClass();
for (int j = 0; j < fileNames.length; j++) {
String fieldName = fileNames[j].substring(0, 1).toUpperCase() + fileNames[j].substring(1);
Method getMethod;
try {
getMethod = cls.getMethod("get" + fieldName);
Object value = getMethod.invoke(o);
if (value != null) {
contentRow.createCell(j).setCellValue(value.toString());
}
} catch (NoSuchMethodException e) {
contentRow.createCell(j).setCellValue(i+1);
}

}
}
} catch (IllegalArgumentException e) {
logger.error("", e);
} catch (IllegalAccessException e) {
logger.error("", e);
} catch (InvocationTargetException e) {
logger.error("", e);
} catch (SecurityException e) {
logger.error("", e);
}

OutputStream os = null;
try {
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String((excelName + ".xlsx").getBytes(), "iso-8859-1"));
response.setContentType("application/vnd.ms-excel;charset=utf-8");
os = response.getOutputStream();
wb.write(os);
} catch (Exception e) {
logger.error("", e);
} finally {
IOUtil.close(os);
}
}

 

/**

导出csv文件,文件操作使用univocity框架

**/

public static <E> void exportCsv(HttpServletResponse response, String[] header, int[] column, String[] fileNames, List<E> list, String csvName){

OutputStream os = null;
CsvWriter writer = null;
try {
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String((csvName + ".csv").getBytes(), "iso-8859-1"));
response.setContentType("application/vnd.ms-excel;charset=utf-8");
os = response.getOutputStream();
writer = new CsvWriter(os, new CsvWriterSettings());
writer.writeHeaders(header);
int size = list.size();
for (int i = 0; i < size; i++) {
String[] content = new String[size];
// 获取每一个对象
E o = list.get(i);
Class cls = o.getClass();
for (int j = 0; j < fileNames.length; j++) {
String fieldName = fileNames[j].substring(0, 1).toUpperCase() + fileNames[j].substring(1);
Method getMethod;
try {
getMethod = cls.getMethod("get" + fieldName);
Object value = getMethod.invoke(o);
if (value != null) {
content[i] = value.toString();
}
} catch (NoSuchMethodException e) {
content[i] = "";
}
}
writer.writeRow(content);
}
} catch (Exception e) {
logger.error("", e);
} finally {
writer.close();
IOUtil.close(os);
}
}

 

转载于:https://www.cnblogs.com/Jhon-Mr/p/7722811.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值