一、简介
本文介绍java实现简单的excel导出,需要开发工具先引入maven。
二、pom整合poi依赖
在pom引入poi依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
三、编写启动类
启动类如下代码所示
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class ExportDemoApp extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ExportDemoApp.class , args);
}
}
四、编写导出工具类
导出工具类如以下代码所示
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
/**
* 导出工具类
*/
public class ExportExcelUtils {
private static final Logger logger = LoggerFactory.getLogger(ExportExcelUtils.class);
//列名
public static final String FIELDNAME = "fieldName";
//列中文名
public static final String FIELDZNAME = "fieldZName";