使⽤EasyPoi根据模板导出Excel或word⽂档接着上篇⽂章 简单介绍下EasyPoi的使⽤,直接上代码吧
⾸先当然是先引⼊jar包了,看下图
其次,还是贴代码吧看实例,下⾯是根据模板导出的⼯具类,包含Excel和word
/**
* 导出Excel或Word⽂件
* @ClassName:ExportExcelOrWord
* @author leon
* @createDate 2018年11⽉26⽇下午14:25:27
* @version v1.0
* @classRemarks TODO
*/
@Configuration
public class ExportExcelOrWord {
private static Logger logger = Logger(ExportExcelOrWord.class);
//模板地址
@Value("${fileTemplateUrl}")
private String fileTemplateUrl="D:/";
//⽂件存储地址
@Value("${fileGoalUrl}")
private String fileGoalUrl="D:/export/";
/**
* 导出word数据(07版)
* @param params map数据集合
* @param fileName 模板名称
* @param goalName ⽂件名称
* @return ⽂件存储地址
* @throws Exception
*/
public String exportWord(Map<String, Object> params, String fileName, String goalName){
try {
fileName=fileTemplateUrl+fileName+".docx";
XWPFDocument doc = portWord07(fileName, params);
// 判断⽂件存放地址是否存在,没有则创建
File savefile = new File(fileGoalUrl);
if (!ists()) {
logger.info("导出word数据时存储⽂件⽬录不存在,为您创建⽂件夹!");
savefile.mkdirs();
}
goalName=fileGoalUrl+goalName+".docx";
FileOutputStream fos = new FileOutputStream(goalName);
doc.write(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
e.printStackTrace();
<("导出word数据异常:"+e);
return null;
}
return goalName;
}
/**
* 按模板导出Excel数据
* @param params 数据
* @param fileName 模板名称
* @param goalName ⽂件名称
* @return ⽂件存储地址
* @throws Exception
*/
public String exportExcel(Map<String, Object> params, String fileName, String goalName){
try {
fileName=fileTemplateUrl+fileName+".xlsx";
TemplateExportParams templateUrl = new TemplateExportParams(fileName);
Workbook workbook = portExcel(templateUrl, params);
// 判断⽂件存放地址是否存在,没有则创建
File savefile = new File(fileGoalUrl);
if (!ists()) {
logger.info("按模板导出Excel数据时存储⽂件⽬录不存在,为您创建⽂件夹!");
savefile.mkdirs();
}
goalName=fileGoalUrl+goalName+".xlsx";
FileOutputStream fos = new FileOutputStream(goalName);
workbook.write(fos);
fos.close();
}catch (IOException e) {
e.printStackTrace();
<("按模板导出Excel数据异常:"+e);
return null;
}
return goalName;
}
/**
* 按模板导出Excel数据(⽀持⽂件流读取⽂件)
* @param params 数据
* @param fileName 模板名称
* @param goalName ⽂件名称
* @return ⽂件存储地址
* @throws Exception
*/
public String exportExcel(Map<String, Object> params, String fileName, String goalName,HttpServletResponse response){
try {
fileName=fileTemplateUrl+fileName+".xlsx";
TemplateExportParams templateUrl = new TemplateExportParams(fileName);
Workbook workbook = portExcel(templateUrl, params);
// 判断⽂件存放地址是否存在,没有则创建
File savefile = new File(fileGoalUrl);
if (!ists()) {
logger.info("按模板导出Excel数据时存储⽂件⽬录不存在,为您创建⽂件夹!");
savefile.mkdirs();
}
//写⽂件流
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + de(goalName+".xlsx", "UTF-8")); workbook.OutputStream());
workbook.OutputStream());
//写⽂件
goalName=fileGoalUrl+goalName+".xlsx";
FileOutputStream fos = new FileOutputStream(goalName);
workbook.write(fos);
fos.close();
}catch (IOException e) {
e.printStackTrace();
<("按模板导出Excel数据异常:"+e);
return null;
}
return goalName;
}
/
**
* 多sheet导出Excel数据每个sheet对应⼀个map,
* key是sheet的NUM
* @param params 数据集合
* @param fileName 模板名称
* @param goalName ⽂件名称
* @return ⽂件存储地址
* @throws Exception
*/
public String exportExcelManeySheet(Map<Integer, Map<String, Object>> params, String fileName, String goalName){
try {
fileName=fileTemplateUrl+fileName+".xlsx";
TemplateExportParams templateUrl = new TemplateExportParams(fileName);
Workbook workbook = portExcel(params, templateUrl);
// 判断⽂件存放地址是否存在,没有则创建
File savefile = new File(fileGoalUrl);
if (!ists()) {
logger.info("多sheet导出Excel数据的存储⽂件⽬录不存在,为您创建⽂件夹!");
savefile.mkdirs();
}
goalName=fileGoalUrl+goalName+".xlsx";
FileOutputStream fos = new FileOutputStream(goalName);
workbook.write(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
<("多sheet导出Excel数据异常:"+e);
return null;
}
return goalName;
}
/**
* 单Excel⽂件多sheet导出Excel数据(注意sheetMap的key需与对象数组中的对象名称⼀致)
* @param sheetMap map数据如:sheetMap.put("PersonnelInfo",List<PersonnelInfo>);
* @param sheetName sheet名称数组如:new String[] {"⼈员信息","家庭信息",...};
* @param objectClass 对象名称数组如:new String[] {"PersonnelInfo","EducatInfo",...};
* @param goalName ⽂件名称
* @return ⽂件存储地址
* @throws Exception
*/
public String exportExcelManeySheet(Map<Object, Object> sheetMap,String[] sheetName,String[] objectClass,String goalName){
//判断参数是否为空
if (sheetName.length<1||objectClass.length<1||sheetMap==null||StringUtil.isNull(goalName)) {
return null;
}
try {
List<Map<String, Object>> sheetsList = new ArrayList<Map<String, Object>>() ;
for (int i = 0; i < sheetName.length; i++) {
//判断map和对象是否为空
//判断map和对象是否为空
if (StringUtil.isNull(objectClass[i]) &&(objectClass[i]) == null) {
continue;
}
ExportParams exportParams = new ExportParams() ;
exportParams.setSheetName(sheetName[i]);
exportParams.setStyle(ExcelExportStyler.class);
Map<String, Object> exportMap = new HashMap<String, Object>();
exportMap.put("title",exportParams);
exportMap.put("entity",Class.forName("com.ity."+objectClass[i]));
exportMap.put("data", (objectClass[i]));
sheetsList.add(exportMap);
}
Workbook workbook = portExcel(sheetsList, ExcelType.HSSF) ;
// 判断⽂件存放地址是否存在,没有则创建
File savefile = new File(fileGoalUrl);
if (!ists()) {
logger.info("单Excel⽂件多sheet导出Excel数据的存储⽂件⽬录不存在,为您创建⽂件夹!");
savefile.mkdirs();
}
goalName=fileGoalUrl+goalName+".xls";
FileOutputStream fos = new FileOutputStream(goalName);
workbook.write(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
<("单Excel⽂件多sheet导出Excel数据异常:"+e);
return null;
}
return goalName;
}
/**
* ⼀个excel 创建多个sheet
* list 多个Map key title 对应表格
* Title key entity 对应表格对应实体
* key data Collection 数据
* @param list 数据集合
* @param goalName ⽂件名称
* @return
*/
public String exportExcel(List<Map<String, Object>> list, String goalName,HttpServletResponse response){
try {
Workbook workbook = portExcel(list, ExcelType.HSSF);
// 判断⽂件存放地址是否存在,没有则创建
File savefile = new File(fileGoalUrl);
if (!ists()) {
logger.info("按模板导出Excel数据时存储⽂件⽬录不存在,为您创建⽂件夹!");
savefile.mkdirs();
}
//写⽂件流
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + de(goalName+".xls", "UTF-8")); workbook.OutputStream());
//写⽂件
goalName=fileGoalUrl+goalName+".xls";
FileOutputStream fos = new FileOutputStream(goalName);
workbook.write(fos);
fos.close();
}catch (IOException e) {
e.printStackTrace();
<("按模板导出Excel数据异常:"+e);
return null;
}
}
return goalName;
getsavefilename}
下⾯看下调⽤过程,超简单
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论