反射--小念头
最编程
2024-05-01 12:52:46
...
package com.fang.jin.util;
import com.fang.jin.bo.FangItemExport;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@Component
public class ExcelManageUtil {
public static <T> ByteArrayInputStream writeDataToExcels( List<FangItemExport> insertDatas) throws Exception {
//创建workbook
Workbook workBook = new XSSFWorkbook();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = null;
Sheet sheet = workBook.createSheet("sheet");
CellStyle style = workBook.createCellStyle();
style.setAlignment(HorizontalAlignment.LEFT); // 想左靠齐
// 创建标题
int rowIndex = 0;
int colIndex = 0;
Row row = sheet.createRow(rowIndex);
List<String> titles = new ArrayList<>();
titles.add("用户id");
titles.add("客户名字");
titles.add("销售名称");
titles.add("电话");
for (String title : titles) {
row.createCell(colIndex, Cell.CELL_TYPE_STRING).setCellValue(title);
colIndex++;
}
try {
for (FangItemExport t : insertDatas) {
rowIndex++;
row = sheet.createRow(rowIndex); // 第二行开始插入数据
row.createCell(0).setCellValue(t.getId());
row.createCell(1).setCellValue(t.getName());
row.createCell(2).setCellValue(t.getUsername());
row.createCell(3).setCellValue(t.getTel());
}
workBook.write(out);
byte[] b = out.toByteArray();
in = new ByteArrayInputStream(b);
} catch (Exception e) {
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return in;
}
public static <T> ByteArrayInputStream writeToExcels( List<T> insertDatas) throws Exception {
//创建workbook
Workbook workBook = new XSSFWorkbook();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = null;
Sheet sheet = workBook.createSheet("sheet");
CellStyle style = workBook.createCellStyle();
style.setAlignment(HorizontalAlignment.LEFT); // 想左靠齐
// 创建标题
int rowIndex = 0;
int colIndex = 0;
Row row = sheet.createRow(rowIndex);
List<String> titles = getTitleList(insertDatas.get(0));
for (String title : titles) {
row.createCell(colIndex, Cell.CELL_TYPE_STRING).setCellValue(title);
colIndex++;
}
try {
for (T t : insertDatas) {
rowIndex++;
row = sheet.createRow(rowIndex); // 第二行开始插入数据
List<Field> fields =getFieldList(t.getClass());
for (Field field : fields) {
FieldName indexFieldName=field.getAnnotation(FieldName.class);
colIndex=getColIndex(titles,indexFieldName.name());
if(colIndex==-1){
continue;
}
String fieldName = field.getName();
String getMethodName =getMethodName(fieldName);
Method method = t.getClass().getMethod(getMethodName);
Object val = method.invoke(t);
String textVal = val != null?String.valueOf(val):"";
row.createCell(colIndex, Cell.CELL_TYPE_STRING).setCellValue(textVal);
row.setHeightInPoints(30);
}
}
workBook.write(out);
byte[] b = out.toByteArray();
in = new ByteArrayInputStream(b);
} catch (Exception e) {
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return in;
}
public static <T> List<String> getTitleList(T t){
List<String> fieldlist=new ArrayList<>();
List<Field> fields =getFieldList(t.getClass());
for (Field field : fields) {
FieldName indexFieldName=field.getAnnotation(FieldName.class);
if(indexFieldName!=null){
fieldlist.add(indexFieldName.name());
}
}
return fieldlist;
}
public static Integer getColIndex(List<String> titleRow ,String fieldName){
for(int i=0;i<titleRow.size();i++){
if(titleRow.get(i).equals(fieldName)){
return i;
}
}
return -1;
}
private static String getMethodName(String fildeName) throws Exception{
byte[] items = fildeName.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return "get"+new String(items);
}
public static List<Field> getFieldList(Class<?> clazz){
if(null == clazz){
return null;
}
List<Field> fieldList = new LinkedList<>();
Field[] fields = clazz.getDeclaredFields();
for(Field field : fields){
fieldList.add(field);
}
return fieldList;
}
}