import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.Date; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class XLSExport { // 定製日期格式 private static String DATE_FORMAT = "m/d/yy h:mm" ; // "m/d/yy h:mm" // 定製浮點數格式 private static String NUMBER_FORMAT = " #,##0.00 " ; private String xlsFileName; private HSSFWorkbook workbook; private HSSFSheet sheet; private HSSFRow row; public XLSExport() { this .workbook = new HSSFWorkbook(); this .sheet = workbook.createSheet(); } /** * 初始化Excel * @param fileName * 導出文件名 */ public XLSExport(String fileName) { this .xlsFileName = fileName; this .workbook = new HSSFWorkbook(); this .sheet = workbook.createSheet(); } /** * 導出Excel文件 * @throws XLSException */ public void exportXLS() throws XLSException { try { FileOutputStream fOut = new FileOutputStream(xlsFileName); workbook.write(fOut); fOut.flush(); fOut.close(); } catch (FileNotFoundException e) { throw new XLSException("生成導出Excel文件出錯! " ); } catch (IOException e) { throw new XLSException("寫入Excel文件出錯! "); } } /** * 導出 excel 能夠穿 response.getOutputStream * @param os * @throws XLSException */ public void exportXLS(OutputStream os) throws XLSException { try { workbook.write(os); os.flush(); os.close(); } catch (FileNotFoundException e) { throw new XLSException("生成導出Excel文件出錯! " ); } catch (IOException e) { throw new XLSException("寫入Excel文件出錯! "); } } /** * 使用ByteArrayOutputStream * 將 Workbook內容寫到此輸出流 再經過toByteArray方法獲取數據源 * 再 new ByteArrayInputStream(); * @return */ public InputStream getExcelInputStream(){ ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { workbook.write(bos); } catch (IOException e) { e.printStackTrace(); } return new ByteArrayInputStream(bos.toByteArray()); } /** * 增長一行 * @param index * 行號 */ public void createRow( int index) { this .row = this .sheet.createRow(index); } /** * 建立表頭 * @param index * @param headName */ public void createTableHead(int index,String...headName){ createRow(index); //添加表頭信息 for(int i = 0;i<headName.length;i++){ setCell(i,headName[i]); } } /** * 根據傳進來的集合 生成excel * @param list * @param flag :true 表明有表頭 false 沒有 */ public void createExcelByList(List list,boolean flag){ for(int i = 0;i<list.size();i++){ //建立行 createRow(flag?i+1:i); //獲取對象及反射對象 Object obj = list.get(i); Class c = obj.getClass(); //獲取全部屬性 Field[] fieldArray = c.getDeclaredFields(); //遍歷全部屬性 try { for(int j = 0;j<fieldArray.length;j++){ //獲取屬性對應的值 String getter = GetNameUtil.getGetter(fieldArray[j]); Method m = c.getDeclaredMethod(getter); Object value = m.invoke(obj); if(value != null){ Class type = fieldArray[j].getType();//獲取屬性類型 if(type==Integer.class||type==int.class){ setCell(j, (Integer)value); }else if(type==Double.class||type==double.class){ setCell(j, (Double)value); }else if(type==Date.class){//java.sql.Date日期處理 Date d = (Date)value; GregorianCalendar gc = new GregorianCalendar(); gc.setTime(d); setCell(j, gc); }else if(type==String.class){ setCell(j, (String)value); } } } } catch (Exception e) { e.printStackTrace(); } } } /** * 設置單元格 * @param index * 列號 * @param value * 單元格填充值 */ public void setCell( int index, String value) { HSSFCell cell = this .row.createCell(index); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(value); } /** * 設置單元格 * @param index * 列號 * @param value * 單元格填充值 */ public void setCell( int index, Calendar value) { HSSFCell cell = this .row.createCell(index); cell.setCellValue(value.getTime()); HSSFCellStyle cellStyle = workbook.createCellStyle(); // 創建新的cell樣式 cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 設置cell樣式爲定製的日期格式 cell.setCellStyle(cellStyle); // 設置該cell日期的顯示格式 sheet.setColumnWidth(index, 10000); } /** * 設置單元格 * @param index * 列號 * @param value * 單元格填充值 */ public void setCell( int index, int value) { HSSFCell cell = this .row.createCell(index); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(value); } /** * 設置單元格 * @param index * 列號 * @param value * 單元格填充值 */ public void setCell( int index, double value) { HSSFCell cell = this .row.createCell(index); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(value); HSSFCellStyle cellStyle = workbook.createCellStyle(); // 創建新的cell樣式 HSSFDataFormat format = workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); // 設置cell樣式爲定製的浮點數格式 cell.setCellStyle(cellStyle); // 設置該cell浮點數的顯示格式 } public String getXlsFileName() { return xlsFileName; } public void setXlsFileName(String xlsFileName) { this.xlsFileName = xlsFileName; } public HSSFWorkbook getWorkbook() { return workbook; } public void setWorkbook(HSSFWorkbook workbook) { this.workbook = workbook; } public HSSFSheet getSheet() { return sheet; } public void setSheet(HSSFSheet sheet) { this.sheet = sheet; } public HSSFRow getRow() { return row; } public void setRow(HSSFRow row) { this.row = row; } }