<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
package com.example.demo.common;
import org.apache.poi.hssf.usermodel.HSSFCell;
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.ss.usermodel.Cell;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ExcelUtil<E> {
public static List<List<String>> cells = new ArrayList<>();
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet hssfSheet = hssfWorkbook.createSheet();
public Integer setWork(List list){
try {
cells.add(list);
if(cells.get(0).size()>=list.size()){
return 1;
}else {
boolean remove = cells.remove(list);
return null;
}
}catch (Exception e){
return null;
}
}
public String createWork(String path){
try {
String xlsPath =path +"test.xls";
File file = new File(path);
file.mkdirs();
File file1 = new File(file, "test.xls");
boolean newFile = file1.createNewFile();
hssfWorkbook.setSheetName(0,"sheet01");
for (int i = 0; i < cells.size(); i++) {
HSSFRow row = hssfSheet.createRow((short)i);
for (int j = 0; j < cells.get(0).size(); j++) {
HSSFCell cell = row.createCell((short)j);
cell.setCellValue(cells.get(i).get(j));
}
}
FileOutputStream fout = new FileOutputStream(file1);
hssfWorkbook.write(fout);
fout.flush();
fout.close();
System.out.println("文件生成");
return xlsPath;
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
for (int i = 0; i < cells.size(); i++) {
cells.remove(i);
}
}
}
public List<List<String>> readWork(File file){
try {
FileInputStream fileInputStream = new FileInputStream(file);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileInputStream);
HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
int lastRowNum = sheetAt.getLastRowNum();
List<List<String>> list = new ArrayList<>();
for (int i = 0; i <= lastRowNum; i++) {
HSSFRow row = sheetAt.getRow(i);
List<String> li = new ArrayList<>();
short lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
li.add(row.getCell(j).getStringCellValue());
}
list.add(li);
}
System.out.println("读取成功");
return list;
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
for (int i = 0; i < cells.size(); i++) {
cells.remove(i);
}
}
}
@Test
public void test01(){
File file = new File("E:\\1.xls");
List<List<String>> lists = readWork(file);
cells=lists;
createWork("");
}
public void WritExcel(List<E> list,String path){
if (list!=null && list.size()>0){
E e = list.get(0);
Field[] declaredFields = e.getClass().getDeclaredFields();
List<String> strings = new ArrayList<>();
for (Field declaredField : declaredFields) {
strings.add(declaredField.getName());
}
cells.add(strings);
for (E e1 : list) {
List<String> tempString = new ArrayList<>();
for (Field declaredField : declaredFields) {
String function = functionSet(declaredField,e1);
tempString.add(function);
}
cells.add(tempString);
}
createWork(path);
}else {
System.out.println("集合内请存入数值");
}
}
public List<E> ReadExcel(File file,Class<E> c){
List<List<String>> lists = readWork(file);
List<E> es = new ArrayList<>();
Field[] declaredFields = c.getDeclaredFields();
for (int j =1 ;j <lists.size() ; j++) {
E e2 = null;
try {
e2 = c.newInstance();
for (int i = 0; i < lists.get(j).size(); i++) {
Field declaredField = declaredFields[i];
functionGet(declaredField, e2, lists.get(j).get(i));
}
es.add(e2);
} catch (Exception Exception) {
Exception.printStackTrace();
}
}
return es;
}
public String functionSet(Field field, E object){
try {
if (field.getGenericType().toString().equals("class java.lang.String")) {
Method m = (Method) object.getClass().getMethod("get" + getMethodName(field.getName()));
String val = null;
val = (String) m.invoke(object);
if (val != null) {
return val;
}
}
if (field.getGenericType().toString().equals("class java.lang.Integer")) {
Method m = (Method) object.getClass().getMethod(
"get" + getMethodName(field.getName()));
Integer val = (Integer) m.invoke(object);
if (val != null) {
return val+"";
}
}
if (field.getGenericType().toString().equals("class java.lang.Double")) {
Method m = (Method) object.getClass().getMethod(
"get" + getMethodName(field.getName()));
Double val = (Double) m.invoke(object);
if (val != null) {
return val+"";
}
}
if (field.getGenericType().toString().equals("class java.lang.Boolean")) {
Method m = (Method) object.getClass().getMethod(
field.getName());
Boolean val = (Boolean) m.invoke(object);
if (val != null) {
return val +"";
}
}
if (field.getGenericType().toString().equals("boolean")) {
Method m = (Method) object.getClass().getMethod(
field.getName());
Boolean val = (Boolean) m.invoke(object);
if (val != null) {
return val+"";
}
}
if (field.getGenericType().toString().equals("class java.util.Date")) {
Method m = (Method) object.getClass().getMethod(
"get" + getMethodName(field.getName()));
Date val = (Date) m.invoke(object);
if (val != null) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(val);
}
}
if (field.getGenericType().toString().equals("class java.lang.Short")) {
Method m = (Method) object.getClass().getMethod(
"get" + getMethodName(field.getName()));
Short val = (Short) m.invoke(object);
if (val != null) {
return val+"";
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public E functionGet(Field field,E object,String val){
String countTemp="";
try {
if (field.getGenericType().toString().equals("class java.lang.String")) {
countTemp ="String type:" + val;
Method m = (Method) object.getClass().getMethod("set" + getMethodName(field.getName())
,String.class
);
m.invoke(object,val);
}
if (field.getGenericType().toString().equals("class java.lang.Integer")) {
countTemp ="Integer type:" + val;
Method m = (Method) object.getClass().getMethod(
"set" + getMethodName(field.getName()),Integer.class);
int i = Integer.parseInt(val);
m.invoke(object,i);
}
if (field.getGenericType().toString().equals("class java.lang.Double")) {
countTemp ="Double type:" + val;
Method m = (Method) object.getClass().getMethod(
"set" + getMethodName(field.getName()),Double.class);
double v = Double.parseDouble(val);
m.invoke(object,v);
}
if (field.getGenericType().toString().equals(countTemp ="class java.lang.Boolean")) {
countTemp ="Boolean type:" + val;
Method m = (Method) object.getClass().getMethod(
field.getName(),Boolean.class);
boolean b = Boolean.parseBoolean(val);
m.invoke(object,b);
}
if (field.getGenericType().toString().equals("boolean")) {
countTemp ="boolean type:" + val;
Method m = (Method) object.getClass().getMethod(
field.getName(),Boolean.class);
boolean b = Boolean.parseBoolean(val);
m.invoke(object,b);
}
if (field.getGenericType().toString().equals("class java.util.Date")) {
countTemp ="Date type:" + val;
Method m = (Method) object.getClass().getMethod(
"set" + getMethodName(field.getName()),Date.class);
Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(val);
m.invoke(object,parse);
}
if (field.getGenericType().toString().equals(countTemp ="class java.lang.Short")) {
countTemp ="Short type:" + val;
Method m = (Method) object.getClass().getMethod(
"set" + getMethodName(field.getName()));
short i = Short.parseShort(val);
m.invoke(object,i);
}
}catch (Exception e){
System.out.println(countTemp+" ;属性名为 :"+field.getName()+"该属性未赋值;");
e.printStackTrace();
}
return object;
}
private String getMethodName(String name) {
String s = name.substring(0, 1).toUpperCase() + name.substring(1);
return s;
}
}