導出數據庫任意表數據爲EXCEL

public class ExportAsExcel {java

    final Logger logger = LoggerFactory.getLogger(getClass());數據庫

    static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");ide

    /**ui

     * 公共組件,導出Excel表格數據方法<p>spa

     * 使用方法: 數據爲List<Object>類型,注意當中object的屬性理論上必須爲java基本數據類型excel

     * 固然也支持像日期這種格式,即完整支持數據庫中字段類型orm

     * @author peryget

     * @param className     數據模型Object的類完整名稱it

     *                      ,eg:com.swust.kelab.Departmentio

     * @param propertyName  數據模型類的屬性對應的列名,

     *                       如「depaId」對應列表「機構id「,

     *                       未指定列名的屬性不會被導出

     * @param tableName     導出的excel中的表名稱

     * @param record        導出的文件絕對路徑(必須爲存在的文件,在本方法中沒有作文件路徑檢查)

     * @param objectList    要導出的數據集

     * @return              成功返回true,失敗返回false;

     */

    public static boolean exportExcel(String className,Map propertyName,String tableName,File record,List<? extends Object> objectList){

        HSSFWorkbook wbook = new HSSFWorkbook();

        HSSFSheet wsheet = wbook.createSheet(tableName); //表名

        HSSFCellStyle cellStyle=getStyle(wbook);

        // 設置Excel表頭

        HSSFRow excelTitle = wsheet.createRow(0);

        excelTitle.setHeightInPoints(22);

        if(objectList.size()<=0){

            wsheet.autoSizeColumn(0);

            HSSFCell titleCell = excelTitle.createCell(0);

            titleCell.setCellValue("對不起,沒有可用數據導出");

            titleCell.setCellStyle(cellStyle);

        }

        Class c = null;

        try {

            c = Class.forName(className);

        } 

        catch (ClassNotFoundException e1) {

            System.out.println("穿入反射用到的類名字有誤");

            e1.printStackTrace();

            return false;

        }http://www.huiyi8.com/moban/ 網頁模板

        java.lang.reflect.Field[] flds = c.getDeclaredFields();

        int j=0;

        for (int i = 0; i < flds.length; i++) {

            String columName=flds[i].getName();

            if(propertyName.containsKey(columName)){

                wsheet.setColumnWidth(j+1, (int)(100*35.7));

                HSSFCell titleCell = excelTitle.createCell(j++);

                titleCell.setCellValue((String)propertyName.get(columName));

                titleCell.setCellStyle(cellStyle);

            }

        }

        for (int i = 0; i < objectList.size(); i++) {

            Object obj = objectList.get(i);

            HSSFRow row = wsheet.createRow(i + 1);

            int k=0;

            for (j = 0; j < flds.length; j++) {

                String columName=flds[j].getName();

                if(!propertyName.containsKey(columName)){

                    continue;

                }    

                HSSFCell hssfCell = row.createCell(k++);

                try {

                    flds[j].setAccessible(true);

                    Object t = flds[j].get(obj);

                    if(t instanceof Date){

                        t = dateFormat.format(t);

                    }

                    if(t!=null){

                        hssfCell.setCellValue(String.valueOf(t));

                    }

                } catch (IllegalArgumentException e) {

                    System.out.println("該字段不是基本數據格式字段");

                    e.printStackTrace();

                } catch (IllegalAccessException e) {

                    System.out.println("非法訪問");

                    e.printStackTrace();

        try {

            FileOutputStream fOut = new FileOutputStream(record);

            wbook.write(fOut);

            fOut.flush();

            fOut.close();

        } catch (FileNotFoundException e) {

            System.out.println("文件路徑錯誤");

            e.printStackTrace();

        } catch (IOException e) {

            System.out.println("I/O錯誤");

            e.printStackTrace();

        }

        return true;

 * Excel表格格式屬性調整

 * @param wbook

 * @return

 */

    private static HSSFCellStyle getStyle(HSSFWorkbook wbook){

        HSSFCellStyle cellStyle = wbook.createCellStyle();

        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);

        cellStyle.setBorderRight(HSSFCellStyle.BORDER_DOUBLE);

        cellStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);

        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);

        HSSFFont font = wbook.createFont();

        font.setFontHeightInPoints((short) 12);

        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        cellStyle.setFont(font);

        return cellStyle;

    }

相關文章
相關標籤/搜索