1.导出接口
@RequestMapping(value = "/test", method = RequestMethod.GET)
public void add(String customExportFieldStr, //前段传来的自定义字段,以.隔开
Integer showIncreaseFlag, //是否需要导出自定义字段,1导出 其他不导出
HttpServletRequest request,
HttpServletResponse response) throws ValidateException, Exception {
TestVo t = new TestVo();
t.setName("111");
t.setNumber("1");
t.setTest("测试导出");
t.setTest2("222222");
List list = new ArrayList<>();
list.add(t);
//这个是导出所有
ExcelUtil.exportExcel(list,null,null,TestVo.class,"活动报表.xls",response);
//这个是按照传递的参数导出
ExcelUtil.customFieldExport(list, TestVo.class, customExportFieldStr, showIncreaseFlag, "测试自定义导出.xls",
response);
}
2.具体方法(util)
package com.bxm.advertiser.controllers;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.*;
/**
* 表格工具类
*
* @author 阿导
* @version v1.1.4_Report
* @fileName com.bxm.adsmanager.utils.ExcelUtil.java
* @CopyRright (c) 2018-杭州微财科技有限公司
* @created 2018-02-27 16:00:00
* @modifier 阿导
* @updated 2018-02-27 16:00:00
*/
public class ExcelUtil {
public static void exportExcel(List> list, String title, String sheetName, Class> pojoClass,String fileName,boolean isCreateHeader, HttpServletResponse response){
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List> list, String title, String sheetName, Class> pojoClass,String fileName, HttpServletResponse response){
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
public static void exportExcel(List
package com.bxm.advertiser.controllers;
public abstract class BaseExportVo {
/**
* Description: 返回默认的导出属性集合
* JDK version used:
* Author: hxpeng
* Create Date: 2018/7/26 14:53
*/
public abstract String[] getDefaultExportFields();
}
package com.bxm.advertiser.controllers;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
public @interface ExcelReportIncrease {
/**
* 自定义字段: field 值
*
* @return
*/
String belongField();
}
package com.bxm.advertiser.controllers;
import cn.afterturn.easypoi.excel.annotation.Excel;
public class TestVo extends BaseExportVo {
@Excel(name = "名称")
private String name;
@Excel(name = "编号")
private String number;
@ExcelReportIncrease(belongField = "test")
@Excel(name = "自定义导出字段")
private String test;
@ExcelReportIncrease(belongField = "test2")
@Excel(name = "自定义导出字段2")
private String test2;
@Override
public String[] getDefaultExportFields() {
return new String[]{"number","name" };
}
get 和set方法自己写
}
需要导入的包
cn.afterturn
easypoi-base
3.0.3
cn.afterturn
easypoi-web
3.0.3
cn.afterturn
easypoi-annotation
3.0.3
注意:他会和下面这个jar包冲突
org.apache.poi
poi-ooxml
3.5-FINAL