67,512
社区成员
发帖
与我相关
我的任务
分享
package poiexample;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
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.CellStyle;
public class poiUtils {
public static final String SRC="E:/workbook.xls";
/**
* 生成xls表格
* @param path 生成后放入的路径
* @param T
* @param className 类名
* @throws IOException
* @throws ClassNotFoundException
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
@SuppressWarnings("deprecation")
public static <T> void writeXLS(String path,List<T> T,Class<?> className) throws IOException, ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
HSSFWorkbook wb = new HSSFWorkbook();// 创建Excel工作簿对象
HSSFSheet sheet = wb.createSheet(className.getName());// 创建Excel工作表对象
CellStyle cellStyle = wb.createCellStyle();// 创建单元格样式
HSSFRow row = sheet.createRow((short) 0); // 创建Excel工作表的行
HSSFCell cell = row.createCell(0);
cell.setCellStyle(cellStyle); // 创建Excel工作表指定行的单元格
Class<?> clazz=Class.forName(className.toString().substring(6));
Field[] field = clazz.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
row.createCell((short)i).setCellValue(field[i].getName());
}
for (int i = 1; i <T.size()+1; i++) {
row = sheet.createRow((short)i);
for (int j = 0; j < field.length; j++) {
// Field field1 = T.get(i).getClass().getDeclaredField(field[j].getName());
// field1.setAccessible(true);
// System.out.println("haha-------------"+field1.getName());
//此处怎么改
row.createCell((short)j).setCellValue("11");
}
}
FileOutputStream fileOut = new FileOutputStream(path);
wb.write(fileOut);
fileOut.close();
}
}
public class hahahah {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException {
Man m1 = new Man();
m1.setName("a11");
m1.setAge("a22");
m1.setSex("a33");
Man m2 = new Man();
m2.setName("b11");
m2.setAge("b22");
m2.setSex("b33");
Man m3 = new Man();
m3.setName("c11");
m3.setAge("c22");
m3.setSex("c33");
Man m4 = new Man();
m4.setName("d11");
m4.setAge("d22");
m4.setSex("d33");
Man m5 = new Man();
m5.setName("e11");
m5.setAge("e22");
m5.setSex("e33");
List<Man> lists=new ArrayList<Man>();
lists.add(m1);
lists.add(m2);
lists.add(m3);
lists.add(m4);
lists.add(m5);
try {
poiUtils.writeXLS(poiUtils.SRC,lists,Man.class);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Man {
private String name;
private String age;
private String sex;
public Man() {
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Field field1 = field[j];
String fieldName = field1.getName();
String getMethodName = "get"+ fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1);
Method getMethod = clazz.getMethod(getMethodName,new Class[] {});
String value = (String) getMethod.invoke(T.get(i-1), new Object[] {});
row.createCell((short)j).setCellValue(value);
已解决