导入实体的类的list和文件输出流生成XX.slx文件
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelBuilder<T> {
@SuppressWarnings("unchecked")
public void buildExcel(List<T> list, OutputStream out) {
if (list.isEmpty()) {return;}
Object entity = null;
Class<T> entityClass = null;
Field[] fields = null;
HSSFWorkbook workbook = null;
HSSFSheet sheet = null;
HSSFCellStyle style = null;
HSSFRow row = null;
HSSFCell cell = null;
int rowIndex = 0;
for (int count = 0; count <= list.size(); count++) {
if (rowIndex == 0) {
entity = list.get(rowIndex);
System.out.println("第一个实体类:" + entity);
entityClass = (Class<T>) entity.getClass();
System.out.println("clazz = " + entityClass.getName());
fields = entityClass.getDeclaredFields();
System.out.println("fieldsNumber = " + fields.length);
for (Field field : fields) {
System.out.println("fieldName = " + field.getName());
}
workbook = new HSSFWorkbook();
sheet = workbook.createSheet();
style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
row = sheet.createRow(rowIndex);
for (short i = 0; i < fields.length; i++) {
cell = row.createCell(i);
cell.setCellStyle(style);
cell.setCellValue(fields[i].getName());
}
rowIndex++;
}
else {
row = sheet.createRow(rowIndex);
for (short i = 0; i < fields.length; i++) {
String name = null;
String methodName = null;
Method method = null;
Object fieldValue = null;
try {
name = fields[i].getName().substring(0, 1).toUpperCase() + fields[i].getName().substring(1);
methodName = "get" + name;
method = entityClass.getMethod(methodName);
entity = list.get(count-1);
fieldValue = method.invoke(entity);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
cell = row.createCell(i);
cell.setCellStyle(style);
if (fieldValue instanceof Integer) {
Integer intValue = (Integer) fieldValue;
cell.setCellValue(intValue);
} else if (fieldValue instanceof Float) {
float fValue = (Float) fieldValue;
cell.setCellValue(fValue);
} else if (fieldValue instanceof Double) {
double dValue = (Double) fieldValue;
cell.setCellValue(dValue);
} else if (fieldValue instanceof Long) {
long lValue = (Long) fieldValue;
cell.setCellValue(lValue);
} else if (fieldValue instanceof Boolean) {
boolean bValue = (Boolean) fieldValue;
cell.setCellValue(bValue?"男":"女");
} else if (fieldValue instanceof Date)
{
Date date = (Date) fieldValue;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cell.setCellValue(sdf.format(date));
} else {
cell.setCellValue(fieldValue.toString());
}
}
rowIndex++;
}
}
try {
workbook.write(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}