利用Apache POI和Java反射開發一個excel導出工具類
一個通用的excel工具類POIExcelUtil。現分享一下代碼。
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set;import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class POIExcelUtil {
public static final String FILE_EXTENSION_XLS = "xls"; public static final String FILE_EXTENSION_XLSX = "xlsx"; /** * * @param Map * <String,String> maps 屬性表,成員屬性age為KEY,中文名稱為VALUE * @param List * <T> list 需要導出的數據列表對象 * @param File * file 指定輸出文件位置,只能導出excel2003以上版本 * * @return true 導出成功 false 導出失敗 */ public static <T> boolean excelExport(Map<String, String> maps, List<T> list, File file) { try { Workbook wb = null; String filename = file.getName(); String type = filename.substring(filename.lastIndexOf(".")+1).toLowerCase(); if (type.equals(FILE_EXTENSION_XLS)) { wb = new HSSFWorkbook(); } if (type.equals(FILE_EXTENSION_XLSX)) { wb = new XSSFWorkbook(); } CreationHelper createHelper = wb.getCreationHelper(); Sheet sheet = wb.createSheet("sheet1"); Set<String> sets = maps.keySet(); Row row = sheet.createRow(0); int i = 0; // 定義表頭 for (Iterator<String> it = sets.iterator(); it.hasNext();) { String key = it.next(); Cell cell = row.createCell(i++); cell.setCellValue(createHelper.createRichTextString(maps.get(key))); } // 填充表單內容 System.out.println("--------------------100%"); float avg = list.size() / 20f; int count = 1; for (int j = 0; j < list.size(); j++) { T p = list.get(j); Class classType = p.getClass(); int index = 0; Row row1 = sheet.createRow(j+1); for (Iterator<String> it = sets.iterator(); it.hasNext();) { String key = it.next(); String firstLetter = key.substring(0, 1).toUpperCase(); // 獲得和屬性對應的getXXX()方法的名字 String getMethodName = "get" + firstLetter+ key.substring(1); // 獲得和屬性對應的getXXX()方法 Method getMethod = classType.getMethod(getMethodName,new Class[] {}); // 調用原對象的getXXX()方法 Object value = getMethod.invoke(p, new Object[] {}); Cell cell = row1.createCell(index++); cell.setCellValue(value.toString()); } if (j > avg * count) { count++; System.out.print("I"); } if (count == 20) { System.out.print("I100%"); count++; } } FileOutputStream fileOut = new FileOutputStream(file); wb.write(fileOut); fileOut.close(); } catch (IOException e) { e.printStackTrace(); return false; } catch (SecurityException e) { e.printStackTrace(); return false; } catch (NoSuchMethodException e) { e.printStackTrace(); return false; } catch (IllegalArgumentException e) { e.printStackTrace(); return false; } catch (IllegalAccessException e) { e.printStackTrace(); return false; } catch (InvocationTargetException e) { e.printStackTrace(); return false; } return true; }}
調用方法截取代碼: Map<String,String> maps = new LinkedHashMap<String,String>(); maps.put("uid", "帳號"); maps.put("cn", "姓名"); maps.put("dept", "部門"); maps.put("mail", "郵箱");
Properties props = System.getProperties(); String USER_HOME = props.getProperty("user.home"); File file = new File(USER_HOME + "/Desktop/excelExport.xlsx"); POIExcelUtil.excelExport(maps, demo,file);</pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!