1. 程式人生 > >JAVA匯出EXCEL實現

JAVA匯出EXCEL實現

JAVA匯出EXCEL實現的多種方式

java匯出Excel的方法有多種,最為常用的方式就是使用第三方jar包,目前POI和JXL是最常用的二方包了,也推薦使用這兩種。

POI實現

POI這裡不詳細解釋,可參考徐老師發的部落格:http://blog.csdn.net/evangel_z/article/details/7332535,他利用開源元件POI3.0.2動態匯出EXCEL文件的通用處理類ExportExcel,詳細使用方法下載最新程式碼看看就可以裡,徐老師寫的很明瞭!總之思路就是用Servlet接受post、get請求,獲取檔案匯出路徑,然後將測試資料封裝好呼叫通用處理類匯出Excel,然後再下載剛匯出的Excel,會自動在瀏覽器彈出選擇儲存路徑的彈出框,這樣就達到裡大家常見的檔案匯出下載的功能!當然,真正的專案裡不可能把檔案匯出到本地,肯定是先吧檔案匯出到伺服器上,再去伺服器下載,對於使用者來說就感覺好像直接就匯出了!
這種實現邏輯也可以修改,就是把通用處理類ExportExcel從void改為返回read好資料的InputStream,而不要直接就去write,然後呼叫下載的方法downLoad使用HttpServletResponse.getOutputStream()所得到的輸出流來write資料,然後呼叫flush()時就會在頁面彈出選擇路徑的彈出框,選擇好後資料就真正從快取輸出到了Excel中,這樣就省去裡中間先要匯出一次的步驟了。

JXL實現

我這裡講一下JXL,其實和POI差不多,就是呼叫的元件不同,引入的jar包不同了,整個Excel匯出下載的邏輯還是一樣的。好了,直接上程式碼,都是通用程式碼,以後都能用的上。
先是幾個mode類封裝了在處理過程中會用到的模型。
ExcelColMode 主要封裝的是Map中的key或者dto中實現get方法的欄位名,其實就是表格的標題的屬性名。

public class ExcelColMode {

    /**
     * Map中的key或者dto中實現get方法的欄位名
     */
    private String name;

    /** 列寬 */
private Integer width; /** * 字型格式,可以設定字型大小,字型顏色,字型加粗 */ private ExcelFontFormat fontFormat; /** * 內容格式化 */ private ExcelColModelFormatterInter contentFormatter; public ExcelColMode(String name) { this.name = name; } public ExcelColMode(String name, Integer width) { this
.name = name; this.width = width; } public String getName() { return name; } public void setName(String name) { this.name = name; } public ExcelFontFormat getFontFormat() { return fontFormat; } public void setFontFormat(ExcelFontFormat fontFormat) { this.fontFormat = fontFormat; } public ExcelColModelFormatterInter getContentFormatter() { return contentFormatter; } public void setContentFormatter(ExcelColModelFormatterInter contentFormatter) { this.contentFormatter = contentFormatter } public Integer getWidth() { return width; } public void setWidth(Integer width) { this.width = width; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

ExcelHeadCell 主要封裝的是標題名

public class ExcelHeadCell implements Comparable<ExcelHeadCell> {

    /**
     * 列合併
     */
    private int colSpan;

    /**
     * 展現字元內容
     */
    private String content;

    /**
     * 父列的序列號
     */
    private int fatherIndex;

    /**
     * 字型格式等
     */
    private ExcelFontFormat fontFormat;

    private Integer height;

    /**
     * 最基礎的單元格,沒有行合併和列合併
     * 
     * @param content
     */
    public ExcelHeadCell(String content) {
        this.colSpan = 1;
        this.content = content;
    }

    public ExcelHeadCell(String content, Integer height) {
        this.colSpan = 1;
        this.content = content;
        this.height = height;
    }

    public ExcelHeadCell(String content, int fatherIndex, ExcelFontFormat fontFormat) {
        this.colSpan = 1;
        this.content = content;
        this.fatherIndex = fatherIndex;
        this.fontFormat = fontFormat;
    }

    public int getColSpan() {
        return colSpan;
    }

    public void setColSpan(int colSpan) {
        this.colSpan = colSpan;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public ExcelFontFormat getFontFormat() {
        return fontFormat;
    }

    public void setFontFormat(ExcelFontFormat fontFormat) {
        this.fontFormat = fontFormat;
    }

    public int getFatherIndex() {
        return fatherIndex;
    }

    public void setFatherIndex(int fatherIndex) {
        this.fatherIndex = fatherIndex;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public int compareTo(ExcelHeadCell o) {
        int i = -1;
        if (o == null) {
            i = 1;
        } else {
            i = o.fatherIndex > this.fatherIndex ? -1 : 1;
            if (o.fatherIndex == this.fatherIndex) {
                i = 0;
            }
        }
        return i;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100

ExcelExportRule 主要封裝的是之前的ExcelColMode和ExcelHeadCell以及sheet頁名稱sheetName

public class ExcelExportRule {

    /**
     * 封裝如何從資料集取資料,資料顯示格式,日期格式和數字格式在這裡設定
     */
    private List<ExcelColMode> colModes;

    /**
     * 封裝EXCEL頭部內容及內容顯示格式
     */
    private List<List<ExcelHeadCell>> headCols;

    /**
     * 資料背景顏色區分,0:不區分,1:按行奇偶區分,奇數行白色,偶數行灰色,2:按列奇偶區分 奇數列白色,偶數列灰色, <br/>
     * <b>注意:此引數為0時,單元格設定的背景色才起作用</b>
     */
    private int distinguishable = 0;

    /**
     * EXCEL的sheet頁名稱
     */
    private String sheetName;

    /**
     * 是否樹形結構,1:是,0:否
     */
    private String hierarchical = "0";

    /**
     * id欄位名,當hierarchical="1"時候才起作用
     */
    private String idName;

    /**
     * 父id欄位名,當hierarchical="1"時候才起作用
     */
    private String pidName;

    public List<ExcelColMode> getColModes() {
        return colModes;
    }

    public void setColModes(List<ExcelColMode> colModes) {
        this.colModes = colModes;
    }

    public List<List<ExcelHeadCell>> getHeadCols() {
        return headCols;
    }

    public void setHeadCols(List<List<ExcelHeadCell>> headCols) {
        this.headCols = headCols;
    }

    public int getDistinguishable() {
        return distinguishable;
    }

    public void setDistinguishable(int distinguishable) {
        this.distinguishable = distinguishable;
    }

    public String getSheetName() {
        return sheetName;
    }

    public void setSheetName(String sheetName) {
        this.sheetName = sheetName;
    }

    public String getHierarchical() {
        return hierarchical;
    }

    public void setHierarchical(String hierarchical) {
        this.hierarchical = hierarchical;
    }

    public String getIdName() {
        return idName;
    }

    public void setIdName(String idName) {
        this.idName = idName;
    }

    public String getPidName() {
        return pidName;
    }

    public void setPidName(String pidName) {
        this.pidName = pidName;
    }

    public void addExcelColMode(ExcelColMode excelColMode) {
        if (colModes == null)
            colModes = new ArrayList<ExcelColMode>();
        colModes.add(excelColMode);
    }

    public void addExcelHeadCellList(List<ExcelHeadCell> list) {
        if (headCols == null)
            headCols = new ArrayList<List<ExcelHeadCell>>();
        headCols.add(list);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107

ExcelFontFormat 封裝的是表格的一些樣式,如果對此沒什麼要求可以忽略

public class ExcelFontFormat {

    private int font = 0; // 字型 0:宋體,1:楷體,2:黑體,3:仿宋體,4:隸書
    private Colour color = Colour.BLACK; // 字型顏色
    private boolean bold = false; // 是否加粗
    private int flow = 0; // 文字浮動方向,0:靠左(預設),1:居中,2:靠右,
    private int fontSize = 0; // 文字大小,0:正常,-2,-1,0,1,2,3,4依次加大,最大到4
    private Colour backgroundColor = Colour.WHITE; // 單元格填充色
    private boolean italic;// 是否斜體
    private int verticalAlign = 1; // 文字上下對齊 0:上 1:中 2:下

    public int getFont() {
        return font;
    }

    public void setFont(int font) {
        this.font = font;
    }

    public Colour getColor() {
        return color;
    }

    public void setColor(Colour color) {
        this.color = color;
    }

    public Colour getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(Colour backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public boolean isBold() {
        return bold;
    }

    public void setBold(boolean bold) {
        this.bold = bold;
    }

    public int getFontSize() {
        return fontSize;
    }

    public void setFontSize(int fontSize) {
        this.fontSize = fontSize;
    }

    public int getFlow() {
        return flow;
    }

    public void setFlow(int flow) {
        this.flow = flow;
    }

    public Alignment convertFlow() {
        return convertFlow(flow);
    }

    public static Alignment convertFlow(int flow) {
        Alignment al = null;
        switch (flow) {
        case 0:
            al = Alignment.LEFT;
            break;
        case 1:
            al = Alignment.CENTRE;
            break;
        case 2:
            al = Alignment.RIGHT;
            break;
        default:
            al = Alignment.LEFT;
        }
        return al;
    }

    public FontName convertFontName() {
        return convertFontName(font);
    }

    public static FontName convertFontName(int font) {
        FontName fn = null;
        switch (font) {
        case 0:
            fn = WritableFont.createFont("SimSun");
            break;
        case 1:
            fn = WritableFont.createFont("KaiTi");
            break;
        case 2:
            fn = WritableFont.createFont("SimHei");
            break;
        case 3:
            fn = WritableFont.createFont("FangSong");
            break;
        case 4:
            fn = WritableFont.createFont("LiSu");
            break;
        default:
            fn = WritableFont.createFont("STSong");
        }
        return fn;
    }

    public int convertFontSize() {
        return convertFontSize(fontSize);
    }

    public static int convertFontSize(int fontSize) {
        return 12 + fontSize * 2;
    }

    @Override
    public boolean equals(Object obj) {
        boolean eq = false;
        if (this == obj) {
            eq = true;
        } else if (obj != null && obj instanceof ExcelFontFormat) {
            ExcelFontFormat e = (ExcelFontFormat) obj;
            if (e.bold == this.bold && e.backgroundColor == this.backgroundColor && e.color == this.color
                    && e.flow == this.flow && e.font == this.font && e.fontSize == this.fontSize
                    && e.italic == this.italic) {
                eq = true;
            }
        }
        return eq;
    }

    public boolean isItalic() {
        return italic;
    }

    public void setItalic(boolean italic) {
        this.italic = italic;
    }

    public int getVerticalAlign() {
        return verticalAlign;
    }

    public void setVerticalAlign(int verticalAlign) {
        this.verticalAlign = verticalAlign;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151

4個mode類以及有了,我介紹的很簡單,每個封裝類其實還封裝了一些其他的,但因為我的例子就只用到了這些就不多講了。下面是Excel處理類ExcelHelper,程式碼比較多,其實大家不用管太多,貼上過來用就行了,只要知道怎麼用他(包括輸入給些什麼,輸出的ByteArrayInputStream怎麼用)就行。

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableFont.FontName;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.JxlWriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.newsee.dto.common.ExcelColMode;
import com.newsee.dto.common.ExcelExportRule;
import com.newsee.dto.common.ExcelFontFormat;
import com.newsee.dto.common.ExcelHeadCell;

public class ExcelHelper {
    private static Log log = LogFactory.getLog(ExcelHelper.class);

    /**
     * 實際需要展現的資料,支援DTO和Map
     */
    private List<Object> rowDatas;

    private Set<Object> writed;

    /**
     * 取資料及資料展現相關
     */
    private List<ExcelColMode> colModes;

    /**
     * 行頭(橫向排列),如果有父行頭則按父行頭的順序,沒有父行頭的按List順序排列
     */
    private List<List<ExcelHeadCell>> headCols;

    /**
     * 資料背景顏色區分,0:不區分,1:按行奇偶區分,2:按列奇偶區分
     */
    private int distinguishable;

    /**
     * 快取展現內容的sheet頁
     */
    private WritableSheet sheet;

    /**
     * 快取單元格格式
     */
    private Map<ExcelFontFormat, WritableCellFormat> mappedFormat;

    /**
     * id欄位名稱,用於樹形結構
     */
    private String idName;

    /**
     * 父id欄位名稱,用於樹形結構
     */
    private String pidName;

    private static String ONE_BLANK = " ";

    private int curDataRowIndex;
    private int curExcelRowIndex;

    public InputStream writeExcel(List<Object> rowDatas, ExcelExportRule rule) throws IOException, WriteException,
            SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException,
            InvocationTargetException {
        if (rule != null) {
            this.rowDatas = rowDatas;
            this.colModes = rule.getColModes();
            this.headCols = rule.getHeadCols();
            this.distinguishable = rule.getDistinguishable();
            if (validate()) {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
                String sheetName = rule.getSheetName();
                if (StringUtil.isBlank(sheetName)) {
                    sheetName = "sheet0";
                }
                sheet = workbook.createSheet(sheetName, 0);
                // 設定列寬
                for (int i = 0; i < colModes.size(); i++) {
                    ExcelColMode colMode = colModes.get(i);
                    if (colMode.getWidth() != null)
                        sheet.setColumnView(i, colMode.getWidth());
                }
                writeHeads();
                // 樹形結構
                if ("1".equals(rule.getHierarchical())) {
                    this.idName = rule.getIdName();
                    this.pidName = rule.getPidName();
                    writeTreeBody();
                }
                // 非樹形結構
                else {
                    writeBody();
                }
                workbook.write();
                workbook.close();
                return new ByteArrayInputStream(outputStream.toByteArray());
            }
        } else {
            log.error("ExcelExportRule為空,無法匯出excel");
        }
        return null;
    }

    private boolean validate() {
        if (colModes == null || colModes.size() == 0) {
            log.error("讀取資料的規則ExcelExportRule.colModes為空");
            return false;
        }
        return true;
    }

    private void writeHeads() throws JxlWriteException, WriteException {
        curExcelRowIndex = 0;
        if (headCols != null && !headCols.isEmpty()) {
            int s = headCols.size();
            if (s > 1) {
                caculaterHeadColSpans();
            }
            for (int i = 0; i < s; i++) {
                int tempColIndex = 0;
                List<ExcelHeadCell> headRowCols = headCols.get(i);
                for (int j = 0; j < headRowCols.size(); j++) {
                    ExcelHeadCell headCol = headRowCols.get(j);
                    writeHeadCell(headCol, tempColIndex);
                    tempColIndex += headCol.getColSpan();
                }
                curExcelRowIndex++;
            }
        }
    }

    // 計算標題需要列數
    private void caculaterHeadColSpans() {
        int s = headCols.size();
        for (int i = s - 1; i > 0; i--) {
            List<ExcelHeadCell> subCols = headCols.get(i);
            Collections.sort(subCols);
            List<ExcelHeadCell> supCols = headCols.get(i - 1);
            int[] fatherColSpans = new int[supCols.size()];
            for (ExcelHeadCell subCol : subCols) {
                int fi = subCol.getFatherIndex();
                fatherColSpans[fi] += subCol.getColSpan();
            }
            for (int j = 0; j < supCols.size(); j++) {
                ExcelHeadCell supCol = supCols.get(j);
                if (fatherColSpans[j] > 0) {
                    supCol.setColSpan(fatherColSpans[j]);
                }
            }
        }
    }

    private void writeHeadCell(ExcelHeadCell headCol, int colIndex) throws JxlWriteException, WriteException {
        ExcelFontFormat eff = headCol.getFontFormat();
        String content = headCol.getContent();
        int colspan = headCol.getColSpan();
        if (headCol.getHeight() != null)
            sheet.setRowView(curExcelRowIndex, headCol.getHeight(), false);
        writeCell(content, eff, colIndex, colspan);
    }

    private void writeCell(String content, ExcelFontFormat eff, int colIndex, int colspan) throws JxlWriteException,
            WriteException {
        if (eff != null) {
        WritableCellFormat wcf = getCellFormat(eff);
            sheet.addCell(new Label(colIndex, curExcelRowIndex, content, wcf));
        } else {
            sheet.addCell(new Label(colIndex, curExcelRowIndex, content));
        }
        if (colspan > 1) {
            sheet.mergeCells(colIndex, curExcelRowIndex, colIndex + colspan - 1, curExcelRowIndex);
        }
    }

    /**
     * 從快取中取格式化的字型,沒有則新建並快取,生成EXCELL完成後需要清除快取的字型
     * 
     * @param eff
     * @return
     * @throws WriteException
     */
    private WritableCellFormat getCellFormat(ExcelFontFormat eff) throws WriteException {
        WritableCellFormat wcf = null;
        if (mappedFormat == null) {
            mappedFormat = new HashMap<ExcelFontFormat, WritableCellFormat>();
        } else {
            wcf = mappedFormat.get(eff);
        }
        if (wcf == null) {
            FontName fn = eff.convertFontName();
            WritableFont wf = new WritableFont(fn, eff.convertFontSize(), eff.isBold() ? WritableFont.BOLD
                    : WritableFont.NO_BOLD, eff.isItalic(), UnderlineStyle.NO_UNDERLINE, eff.getColor());
            wcf = new WritableCellFormat(wf);
            wcf.setBackground(eff.getBackgroundColor());
            wcf.setAlignment(eff.convertFlow());
            wcf.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK);
            if (eff.getVerticalAlign() == 0)
                wcf.setVerticalAlignment(VerticalAlignment.TOP);
            else if (eff.getVerticalAlign() == 1)
                wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
            else if (eff.getVerticalAlign() == 2)
                wcf.setVerticalAlignment(VerticalAlignment.BOTTOM);
            mappedFormat.put(eff, wcf);
        }
        return wcf;
    }

    private void writeTreeBody() throws RowsExceededException, WriteException, SecurityException,
            IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        if (rowDatas != null && rowDatas.size() > 0) {
            curDataRowIndex = 1;
            Object fo = rowDatas.get(0);
            boolean isMap = fo instanceof Map;
            if (isMap) {
                writeTreeDatas4Map();
            } else {
                writeTreeDatas4Dto();
            }
        }
    }

    private void writeTreeDatas4Map() throws JxlWriteException, WriteException {
        if (writed == null) {
        writed = new HashSet<Object>();
        }
        for (Object data : rowDatas) {
            if (!writed.contains(data)) {
                Map m = (Map) data;
                Object pid = m.get(pidName);
                if (pid == null) {
                    writeRow4Map(data);
                    curDataRowIndex++;
                    curExcelRowIndex++;
                    writed.add(data);
                    writeSubDatas4Map(m, 1);
                } else {
                    if (pid instanceof String) {
                        String ps = (String) pid;
                        if (StringUtil.isBlank(ps) || (Integer.valueOf(ps) <= 0)) {
                            writeRow4Map(data);
                            curDataRowIndex++;
                            curExcelRowIndex++;
                            writed.add(data);
                            writeSubDatas4Map(m, 1);
                        }
                    } else if (pid instanceof Integer) {
                        Integer pi = (Integer) pid;
                        if (pi <= 0) {
                            writeRow4Map(data);
                            curDataRowIndex++;
                            curExcelRowIndex++;
                            writed.add(data);
                            writeSubDatas4Map(m, 1);
                        }
                    } else if (pid instanceof Long) {
                        Long pl = (Long) pid;
                        if (pl.compareTo(0L) <= 0) {
                            writeRow4Map(data);
                            curDataRowIndex++;
                            curExcelRowIndex++;
                            writed.add(data);
                            writeSubDatas4Map(m, 1);
                        }
                    } else if (pid instanceof BigDecimal) {
                        if (((BigDecimal) pid).compareTo(BigDecimal.ZERO) <= 0) {
                            writeRow4Map(data);
                            curDataRowIndex++;
                            curExcelRowIndex++;
                            writed.add(data);
                            writeSubDatas4Map(m, 1);
                        }
                    }
                }
            }
        }
    }

    private void writeSubDatas4Map(Map father, int deep) throws RowsExceededException, WriteException {
        for (Object data : rowDatas) {
            if (!writed.contains(data)) {
                Map m = (Map) data;
                Object pid = m.get(pidName);
                Object fid = father.get(idName);
                if (pid != null) {
                    if (pid instanceof Long) {
                        Long pl = (Long) pid;
                        Long fl = null;
                        try {
                            fl = (Long) fid;
                        } catch (Exception e) {
                            if (fid instanceof BigDecimal) {
                                fl = ((BigDecimal) fid).longValue();
                            }
                        }
                        if (pl.equals(fl)) {
                            writeSubRow4Map(m, deep);
                            curDataRowIndex++;
                            curExcelRowIndex++;
                            int subDeep = deep + 1;
                            writed.add(data);
                            writeSubDatas4Map(m, subDeep);
                        }
                    } else if (pid instanceof String) {
                        String ps = (String) pid;
                        String fs = null;
                        try {
                            fs = (String) fid;
                        } catch (Exception e) {
                            if (fid instanceof BigDecimal) {
                                fs = ((BigDecimal) fid).toString();
                            }
                        }
                        if (ps.equals(fs)) {
                            writeSubRow4Map(m, deep);
                            curDataRowIndex++;
                            curExcelRowIndex++;
                            int subDeep = deep + 1;
                            writed.add(data);
                            writeSubDatas4Map(m, sub