先贴代码。 :p ExportExcel.java /** * @author cosmo * @created date 2010-7-4 下午07:29:52 */ package com.myown.test; import java.io.FileOutputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; /** * @author cosmo * @created date 2010-7-4 下午07:30:52 */ public class ExportExcel { /** * 以方法名为key,Method为value */ private Map<String, Method> methodMap; /** * @param list * 需要导出到excel的对象list * @param title * excel标题 * @param sheetName * sheet名 * @param sheetTitle * sheet内标题 */ public void export(List<?> list, String title, String sheetName, String sheetTitle) throws Exception { if (null == list || list.size() == 0) { return; } if (null == title || title.length() == 0) { title = "未命名"; } Object o = list.get(0); if (null == sheetName || sheetName.length() == 0) { sheetName = o.getClass().getSimpleName(); } String[] tag; if (null == sheetTitle || sheetTitle.length() == 0 || sheetTitle.indexOf("|") == -1) { // 如果没有指定需要导出的字段,则将对象的所有字段都进行导出 Field[] fields = o.getClass().getDeclaredFields(); tag = new String[fields.length]; for (int i = 0; i < fields.length; i++) { tag[i] = fields[i].getName(); } } else { // 反之则导出指定的字段 tag = sheetTitle.split("[|]"); } Workbook wb = new HSSFWorkbook(); CellStyle cellStyle = wb.createCellStyle(); Font font = wb.createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); cellStyle.setFont(font); Sheet sheet = wb.createSheet(sheetName); this.init(o); // 生成标题 Row rowTitle = sheet.createRow((short) 0); int k = 0; for (int i = 0; i < tag.length; i++) { if (tag[i].length() == 0) { continue; } Cell cellTitle = rowTitle.createCell(k); cellTitle.setCellValue(tag[i]); // 设置标题为粗体 cellTitle.setCellStyle(cellStyle); // 设置宽度自动适应 sheet.autoSizeColumn((short) k, true); k++; } // 生成内容 for (int i = 0; i < list.size(); i++) { Row row = sheet.createRow((short) (i + 1)); Object obj = list.get(i); int idx = 0; for (int j = 0; j < tag.length; j++) { if (tag[j].length() == 0) { continue; } String getter = this.getMethod(tag[j]); Cell cell = row.createCell(idx); // 调用get方法 Method m = this.methodMap.get(getter.toString()); if (null == m) { System.out.println("未找到对应的getter方法"); return; } cell.setCellValue(m.invoke(obj).toString()); // 设置宽度自动适应 sheet.autoSizeColumn((short) idx, true); idx++; } } FileOutputStream fileOut = new FileOutputStream("d:/" + title + ".xls"); wb.write(fileOut); fileOut.close(); } /** * 填充methodMap * * @param o */ private void init(Object o) { methodMap = new HashMap<String, Method>(); Class<?> c = o.getClass(); Method[] methods = c.getDeclaredMethods(); for (Method m : methods) { // 得到所有get方法和is方法 String name = m.getName(); if (name.startsWith("get") || name.startsWith("is")) { this.methodMap.put(name, m); } } } /** * 得到getter方法 * * @param method * @return */ private String getMethod(String method) { // TODO 忽略了is开头的方法, 偷懒了 : p // 得到字段的get方法 StringBuffer getMethod = new StringBuffer(); String field = method; // 首字母大写 String letter = field.substring(0, 1); field = field.replaceFirst(letter, letter.toUpperCase()); getMethod.append("get"); getMethod.append(field); return getMethod.toString(); } /** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub ExportExcel ee = new ExportExcel(); List<Test> list = new ArrayList<Test>(); Test t1 = new Test(1, "aaaaaaaaaaaaa", false); Test t2 = new Test(2, "b", true); Test t3 = new Test(3, "c", false); Test t4 = new Test(4, "d", true); list.add(t1); list.add(t2); list.add(t3); list.add(t4); // 不指定输出字段 ee.export(list, "test", "test", "sasd"); // 指定输出字段为 id 和 flag ee.export(list, "test1", "sadf", "|id|flag|"); System.out.println("finish"); } } Test.java package com.myown.test; /** * @author Cosmo * @created date 2010-7-4 下午07:40:52 */ public class Test { private int id; private String name; private boolean flag; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean getFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public Test() { } public Test(int id, String name, boolean flag) { this.id = id; this.name = name; this.flag = flag; } } ps.只为了实现功能,很多地方还很毛糙。见笑了. POI地址:http://poi.apache.org/