一、pom依赖
<org.apache.poi.version>3.10-FINAL</org.apache.poi.version> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${org.apache.poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${org.apache.poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>${org.apache.poi.version}</version> </dependency>
二、封装的服务
import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; 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.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import tf56.hermescontractweb.util.DateUtil; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.UUID; @Slf4j @Service public class ExcelExportService { @Autowired private HermesFileService hermesFileService; /** * 导出excel * @param excelDataList:需要导出的数据 List<javaBean> * @param clazz 对应excelDataList具体类型的clazz(导出excel表头使用javaBean属性名) * @param response * @throws Exception */ public void exportExcel2003(List excelDataList, Class clazz, HttpServletResponse response) throws Exception { List<String> excelTitles = getExcelTitles(clazz); List<Map<String, Object>> rowDataList = new ArrayList<>(); for (Object obj : excelDataList) { rowDat