在做通过java中excel模板导出后台数据时,
exportBook.write(byteArrayOut); 报错,原因是配置文件中只有POI的jar包,没有commons-collections4的jar包,在网上下载之后复制到该类所在的lib下,并在MANIFEST.MF文件中Bundle-ClassPath: 下添加 META-INF/lib/commons-collections4-4.1.jar 就可以了。主要是缺少commons-collections4 包导致报错。标记一下加深印象。
public Object exportProjectIntroList( final String typeId,
final ItemCondition itemCondition, final String modelId) {
String fileName = "DTKCJJZBMX.xls";
if ("1".equals(modelId)) { //
fileName = "DTKCJJZBMX.xls";
}
// 模板文件流
final InputStream templateFileIs = TemplateUtils.getTemplateFileInputStream(fileName);
// 根据条件查询页面需要导出的数据
List<Map> resultList = iTRpeProjBasicInforQueryContext.getModelResultList(dc, typeId, itemCondition);
//根据模型Id构建excel表头 不同的模型导出列表不一样
//final ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
byte[] bytes = null;
try {
//if (resultList != null && resultList.size()>0) {
Workbook exportBook = ModelExportExcelUtil.exportModelListInfo(templateFileIs,fileName, resultList);
bytes = getExcelBytes(exportBook);
//}
}
catch (Exception e) {
e.printStackTrace();
} finally {
if (templateFileIs != null) {
try {
templateFileIs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytes;
}
private byte[] getExcelBytes(final Workbook exportBook) {
ByteArrayOutputStream byteArrayOut = null;
byte[] bytes = null;
try {
byteArrayOut = new ByteArrayOutputStream();
exportBook.write(byteArrayOut);
bytes = byteArrayOut.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (byteArrayOut != null) {
byteArrayOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bytes;
}