通用EXCEL模板导出
废话不多说,直接上代码:
maven
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
实体工具类
import lombok.Data;
import java.util.List;
@Data
public class ParameterUtil {
/**
* 模板存放路径
*/
private String excelUrl;
/**
* 数据导入开始行数
*/
private Integer excelRwo;
/**
* 导出excel名称
*/
private String excelName;
/**
* 数据对象
*/
private List<?> objects;
/**
* 模板sheet下标
*/
private Integer sheetIndex;
}
导出工具类
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.Map;
public class ExcelUtil {
/**
* 公用excel导出
* @param request
* @param response
* @param parameterUtil 参数实体
*/
public void exportExcel(HttpServletRequest request, HttpServletResponse response,ParameterUtil parameterUtil) {
if (parameterUtil != null){
try {
InputStream is = null;
if (StringUtils.isNotBlank(parameterUtil.getExcelUrl())){
is = this.getClass().getResourceAsStream(parameterUtil.getExcelUrl());
}else {
//return "模板路径不能为空";
}
//这里只支持 xls 模板 需要用到xlsx 请使用 XSSFWorkbook
HSSFWorkbook wb = new HSSFWorkbook(is);
// XSSFWorkbook wb = new XSSFWorkbook(is);
// 根据页面index 获取sheet页
// XSSFSheet sheet = null;
HSSFSheet sheet = null;
if (parameterUtil.getSheetIndex()!=null){
sheet=wb.getSheetAt(parameterUtil.getSheetIndex());
}else {
sheet=wb.getSheetAt(0);
}
if (parameterUtil.getObjects() != null){
for (int i = 0; i < parameterUtil.getObjects().size(); i++){
Map<?, ?> map = new org.apache.commons.beanutils.BeanMap(parameterUtil.getObjects().get(i));
// 创建HSSFRow对象
HSSFRow row = null;
if (parameterUtil.getExcelRwo() != null && parameterUtil.getExcelRwo()>=0){
row = sheet.createRow(i + parameterUtil.getExcelRwo());
}else {
row = sheet.createRow(i + 1);
}
//getDeclaredFields:获得某个类的所有声明的字段
//循环获取实体对象的属性
for (Field f : parameterUtil.getObjects().get(i).getClass().getDeclaredFields()){
ExcelExport ef = f.getAnnotation(ExcelExport.class);
f.setAccessible(true);
//通过反射进行获取实体类中的字段值,当未设置Field的setAccessible方法为true时,会在调用的时候进行访问安全检查,会抛出IllegalAccessException异常
if(ef != null){
// 创建HSSFCell对象 设置单元格的值
row.createCell(ef.isRwo()).setCellValue(map.get(f.getName()) != null ? map.get(f.getName()).toString() : "");
}
}
}
}
// 输出Excel文件
OutputStream output = response.getOutputStream();
response.reset();
// 设置文件头
response.setHeader("Content-Disposition",
"attchement;filename=" + new String(parameterUtil.getExcelName().getBytes("gb2312"), "ISO8859-1"));
response.setContentType("application/msexcel");
wb.write(output);
output.flush();
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
自定义注解类
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelExport {
/**
* 第 isRwo 列
* @return
*/
int isRwo() default 0;
}
通用的三个工具类就这些了 接下来是用法:
1.首先准备一个xls的excel文件
2.创建好实体类 并且使用自定义注解
/**
* 学生实体类
* 该注解只有一个参数,就是这一列显示在excel第级列,下标从0开始
*/
@Data
public class StudentModel {
/**
* 班级
*/
@ExcelExport(isRwo = 0)
private String stuClass ;
/**
* 姓名
*/
@ExcelExport(isRwo = 1)
private String stuNme;
/**
* 年龄
*/
@ExcelExport(isRwo = 2)
private String stuAge;
}
3.做好这些准备工作 就可以直接调用了
/**
* 导出 excel
*
*/
@RequestMapping("/excel.json")
@ResponseBody
public void excel(HttpServletRequest request, HttpServletResponse response) throws UnitedException {
List<StudentModel> list = new ArrayList<>();
StudentModel sm1 = new StudentModel();
StudentModel sm2 = new StudentModel();
sm1.setStuClass("班级一");
sm2.setStuClass("班级二");
sm1.setStuNme("张三");
sm2.setStuNme("李四");
sm1.setStuAge("15岁");
sm2.setStuAge("16岁");
list.add(sm1 );
list.add(sm2 );
ParameterUtil parameterUtil = new ParameterUtil();
parameterUtil.setExcelUrl("模板存放路径");
parameterUtil.setExcelName("导出excel的名字.xls");
parameterUtil.setExcelRwo(1);//这里是从第几行开始,excel行级下标也是从0开始 所以我们这里写1
parameterUtil.setSheetIndex(0);//这里是把数据导入到哪个sheet里面 不写默认写入第一个sheet
parameterUtil.setObjects(list);//实体数据
ExcelUtil excelUtil = new ExcelUtil();
excelUtil.exportExcel(request,response,parameterUtil);
}
调用这个方法就可以将excel进行导出了
注:有些特殊格式,可以直接在模板中设置