ExcelBuilderImpl.java 5.5 KB
Newer Older
J
update  
jipengfei.jpf 已提交
1 2
package com.alibaba.excel.write;

3 4 5
import com.alibaba.excel.context.GenerateContext;
import com.alibaba.excel.context.GenerateContextImpl;
import com.alibaba.excel.metadata.BaseRowModel;
J
update  
jipengfei.jpf 已提交
6 7 8 9
import com.alibaba.excel.metadata.ExcelColumnProperty;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
10 11 12
import com.alibaba.excel.util.POITempFile;
import com.alibaba.excel.util.TypeUtil;
import org.apache.commons.beanutils.BeanUtilsBean;
J
update  
jipengfei.jpf 已提交
13 14 15
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

16 17 18 19 20 21
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

J
update  
jipengfei.jpf 已提交
22 23 24 25 26 27 28
/**
 * @author jipengfei
 */
public class ExcelBuilderImpl implements ExcelBuilder {

    private GenerateContext context;

29 30 31 32 33
    @Override
    public void init(InputStream templateInputStream, OutputStream out, ExcelTypeEnum excelType, boolean needHead) {
        try {
            //初始化时候创建临时缓存目录,用于规避POI在并发写bug
            POITempFile.createPOIFilesDirectory();
J
update  
jipengfei.jpf 已提交
34

35 36 37 38
            context = new GenerateContextImpl(templateInputStream, out, excelType, needHead);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
J
update  
jipengfei.jpf 已提交
39 40
    }

41 42
    @Override
    public void addContent(List data, int startRow) {
J
update  
jipengfei.jpf 已提交
43 44 45 46
        if (data != null && data.size() > 0) {
            int rowNum = context.getCurrentSheet().getLastRowNum();
            if (rowNum == 0) {
                Row row = context.getCurrentSheet().getRow(0);
47
                if (row == null) {
J
update  
jipengfei.jpf 已提交
48 49 50 51 52
                    if (context.getExcelHeadProperty() == null || !context.needHead()) {
                        rowNum = -1;
                    }
                }
            }
53 54 55
            if (rowNum < startRow) {
                rowNum = startRow;
            }
J
update  
jipengfei.jpf 已提交
56 57
            for (int i = 0; i < data.size(); i++) {
                int n = i + rowNum + 1;
58

J
update  
jipengfei.jpf 已提交
59 60 61 62 63
                addOneRowOfDataToExcel(data.get(i), n);
            }
        }
    }

64
    @Override
J
update  
jipengfei.jpf 已提交
65 66
    public void addContent(List data, Sheet sheetParam) {
        context.buildCurrentSheet(sheetParam);
67
        addContent(data, sheetParam.getStartRow());
J
update  
jipengfei.jpf 已提交
68 69
    }

70
    @Override
J
update  
jipengfei.jpf 已提交
71 72 73
    public void addContent(List data, Sheet sheetParam, Table table) {
        context.buildCurrentSheet(sheetParam);
        context.buildTable(table);
74
        addContent(data, sheetParam.getStartRow());
J
update  
jipengfei.jpf 已提交
75 76
    }

77
    @Override
J
update  
jipengfei.jpf 已提交
78 79 80 81 82 83 84 85
    public void finish() {
        try {
            context.getWorkbook().write(context.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

86
    private void addOneRowOfDataToExcel(List<Object> oneRowData, Row row) {
J
update  
jipengfei.jpf 已提交
87 88 89 90
        if (oneRowData != null && oneRowData.size() > 0) {
            for (int i = 0; i < oneRowData.size(); i++) {
                Cell cell = row.createCell(i);
                cell.setCellStyle(context.getCurrentContentStyle());
91 92 93 94 95 96 97 98 99 100 101 102 103 104
                Object cellValue = oneRowData.get(i);
                if (cellValue != null) {
                    if (cellValue instanceof String) {
                        cell.setCellValue((String)cellValue);
                    } else if (cellValue instanceof Integer) {
                        cell.setCellValue(Double.parseDouble(cellValue.toString()));
                    } else if (cellValue instanceof Double) {
                        cell.setCellValue((Double)cellValue);
                    } else if (cellValue instanceof Short) {
                        cell.setCellValue((Double.parseDouble(cellValue.toString())));
                    }
                } else {
                    cell.setCellValue((String)null);
                }
J
update  
jipengfei.jpf 已提交
105 106 107 108 109 110 111 112
            }
        }
    }

    private void addOneRowOfDataToExcel(Object oneRowData, Row row) {
        int i = 0;
        for (ExcelColumnProperty excelHeadProperty : context.getExcelHeadProperty().getColumnPropertyList()) {
            Cell cell = row.createCell(i);
113 114 115 116 117 118
            BaseRowModel baseRowModel = (BaseRowModel)oneRowData;
            if (baseRowModel.getStyle(i) != null) {
                cell.setCellStyle(baseRowModel.getStyle(i));
            } else {
                cell.setCellStyle(context.getCurrentContentStyle());
            }
J
update  
jipengfei.jpf 已提交
119 120
            String cellValue = null;
            try {
121 122 123 124 125 126 127 128
                Object value = BeanUtilsBean.getInstance().getPropertyUtils().getNestedProperty(oneRowData,
                    excelHeadProperty.getField().getName());

                if (value instanceof Date) {
                    cellValue = TypeUtil.formatDate((Date)value, excelHeadProperty.getFormat());
                } else {
                    cellValue = BeanUtilsBean.getInstance().getConvertUtils().convert(value);
                }
J
update  
jipengfei.jpf 已提交
129 130 131
            } catch (Exception e) {
                e.printStackTrace();
            }
132 133 134 135 136 137
            if (TypeUtil.isNotEmpty(cellValue)) {
                if (TypeUtil.isNum(excelHeadProperty.getField())) {
                    cell.setCellValue(Double.parseDouble(cellValue));
                } else {
                    cell.setCellValue(cellValue);
                }
J
update  
jipengfei.jpf 已提交
138 139 140 141 142
            } else {
                cell.setCellValue("");
            }
            i++;
        }
143

J
update  
jipengfei.jpf 已提交
144 145 146 147 148
    }

    private void addOneRowOfDataToExcel(Object oneRowData, int n) {
        Row row = context.getCurrentSheet().createRow(n);
        if (oneRowData instanceof List) {
149
            addOneRowOfDataToExcel((List)oneRowData, row);
J
update  
jipengfei.jpf 已提交
150 151 152 153 154
        } else {
            addOneRowOfDataToExcel(oneRowData, row);
        }
    }
}