使用poi导入、导出Excel内容

来源:互联网 发布:cpa做题软件 编辑:程序博客网 时间:2024/06/05 07:24
  在报表中我们经常会用到导出数据、或者从外部导入数据。所以我通过poi来对excel进行导入导出操作。 
    支持功能: 
    1、可定制导入/导出数据字段 
    2、可以对导入/导出的数据进行编码转换。例:在用户表中的sex性别字段,数据存储为:0:男;1:女,则在导出数据的时候可以对编码进行转换,导出结果为男/女。同样在导入时,可以将文字转换为编码存储到数据库中。 
    3、完美支持1997——2010的office Excel版本。已经过测试。 

    欢迎大家提出意见


<h3 style="padding: 0px; margin: 0px 0px 2px; font-size: 10pt; color: rgb(0, 102, 0); font-family: 'Microsoft YaHei', Verdana, sans-serif, SimSun;">文件ExcelColumn.java</h3> package com.excel; /** * excel列信息 * * @createTime: 2012-4-19 下午12:03:49 * @author: <a href="mailto:hubo@feinno.com">hubo</a> * @version: 0.1 * @lastVersion: 0.1 * @updateTime:  * @updateAuthor: <a href="mailto:hubo@feinno.com">hubo</a> * @changesSum:  *  */public class ExcelColumn {    /**     * 索引     */    private int index;         /**     * 字段名称     */    private String fieldName;         /**     * 字段显示名称     */    private String fieldDispName;         /**     * 字段类型     */    private int type;         public ExcelColumn() {             }         public ExcelColumn(int index, String fieldName, String fieldDispName) {        super();        this.index = index;        this.fieldName = fieldName;        this.fieldDispName = fieldDispName;    }         public ExcelColumn(int index, String fieldName, String fieldDispName, int type) {        super();        this.index = index;        this.fieldName = fieldName;        this.fieldDispName = fieldDispName;        this.type = type;    }     public int getIndex() {        return index;    }     public String getFieldName() {        return fieldName;    }     public void setIndex(int index) {        this.index = index;    }     public void setFieldName(String fieldName) {        this.fieldName = fieldName;    }     public String getFieldDispName() {        return fieldDispName;    }     public void setFieldDispName(String fieldDispName) {        this.fieldDispName = fieldDispName;    }     public int getType() {        return type;    }     public void setType(int type) {        this.type = type;    }     @Override    public String toString() {        return "ExcelColumn [fieldDispName=" + fieldDispName + ", fieldName="                + fieldName + ", index=" + index + ", type=" + type + "]";    }}

ExcelHead.java


package com.excel; import java.util.List;import java.util.Map; /** * Excel头信息 * * @createTime: 2012-4-18 下午01:17:53 * @author: <a href="mailto:hubo@feinno.com">hubo</a> * @version: 0.1 * @lastVersion: 0.1 * @updateTime:  * @updateAuthor: <a href="mailto:hubo@feinno.com">hubo</a> * @changesSum:  *  */public class ExcelHead {    /**     * 列信息     */    private List<ExcelColumn> columns;         /**     * 需要转换的列     */    private Map<String, Map> columnsConvertMap;         /**     * 头部所占用的行数     */    private int rowCount;         /**     * 头部所占用的列数     */    private int columnCount;     public List<ExcelColumn> getColumns() {        return columns;    }     public int getRowCount() {        return rowCount;    }     public int getColumnCount() {        return columnCount;    }     public void setColumns(List<ExcelColumn> columns) {        this.columns = columns;    }     public void setRowCount(int rowCount) {        this.rowCount = rowCount;    }     public void setColumnCount(int columnCount) {        this.columnCount = columnCount;    }     public Map<String, Map> getColumnsConvertMap() {        return columnsConvertMap;    }     public void setColumnsConvertMap(Map<String, Map> columnsConvertMap) {        this.columnsConvertMap = columnsConvertMap;    }     @Override    public String toString() {        return "ExcelHead [columnCount=" + columnCount + ", columns=" + columns                + ", columnsConvertMap=" + columnsConvertMap + ", rowCount="                + rowCount + "]";    } }

ExcelHelper.java

package com.excel; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map; import jodd.bean.BeanUtil;import jodd.datetime.JDateTime;import jodd.util.StringUtil; import org.apache.poi.hssf.util.CellReference;import org.apache.poi.openxml4j.exceptions.InvalidFormatException;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.DateUtil;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.ss.usermodel.WorkbookFactory; /** * Excel助手类 * * @createTime: 2012-4-19 上午10:14:46 * @author: <a href="mailto:hubo@feinno.com">hubo</a> * @version: 0.1 * @lastVersion: 0.1 * @updateTime:  * @updateAuthor: <a href="mailto:hubo@feinno.com">hubo</a> * @changesSum:  *  */public class ExcelHelper {    private static ExcelHelper helper = null;         private ExcelHelper() {             }         public static synchronized ExcelHelper getInstanse() {        if(helper == null) {            helper = new ExcelHelper();        }        return helper;    }         /**     * 将Excel文件导入到list对象     * @param head  文件头信息     * @param file  导入的数据源     * @param cls   保存当前数据的对象     * @return     * @auther <a href="mailto:hubo@feinno.com">hubo</a>     * @return List     * 2012-4-19 下午01:17:48     */    public List importToObjectList(ExcelHead head, File file, Class cls) {        List contents = null;        FileInputStream fis;        // 根据excel生成list类型的数据        List<List> rows;        try {            fis = new FileInputStream(file);            rows = excelFileConvertToList(fis);                         // 删除头信息            for (int i = 0; i < head.getRowCount(); i++) {                rows.remove(0);            }                         // 将表结构转换成Map            Map<Integer, String> excelHeadMap = convertExcelHeadToMap(head.getColumns());            // 构建为对象            contents = buildDataObject(excelHeadMap, head.getColumnsConvertMap(), rows, cls);        } catch (FileNotFoundException ex) {            ex.printStackTrace();        } catch (Exception ex) {            ex.printStackTrace();        }        return contents;    }         /**     * 导出数据至Excel文件     * @param excelColumns  报表头信息     * @param excelHeadConvertMap   需要对数据进行特殊转换的列     * @param modelFile  模板Excel文件     * @param outputFile 导出文件     * @param dataList  导入excel报表的数据来源     * @auther <a href="mailto:hubo@feinno.com">hubo</a>     * @return void     * 2012-4-19 上午10:04:30     */    public void exportExcelFile(ExcelHead head, File modelFile, File outputFile, List<?> dataList) {        // 读取导出excel模板        InputStream inp = null;        Workbook wb = null;        try {            inp = new FileInputStream(modelFile);            wb = WorkbookFactory.create(inp);            Sheet sheet = wb.getSheetAt(0);            // 生成导出数据            buildExcelData(sheet, head, dataList);                         // 导出到文件中            FileOutputStream fileOut = new FileOutputStream(outputFile);            wb.write(fileOut);            fileOut.close();        } catch (FileNotFoundException ex) {            ex.printStackTrace();        } catch (InvalidFormatException ex) {            ex.printStackTrace();        } catch (IOException ex) {            ex.printStackTrace();        }    }         /**     * 将报表结构转换成Map     * @param excelColumns     * @auther <a href="mailto:hubo@feinno.com">hubo</a>     * @return void     * 2012-4-18 下午01:31:12     */    private Map<Integer, String> convertExcelHeadToMap(List<ExcelColumn> excelColumns) {        Map<Integer, String> excelHeadMap = new HashMap<Integer, String>();        for (ExcelColumn excelColumn : excelColumns) {            if(StringUtil.isEmpty(excelColumn.getFieldName())) {                continue;            } else {                excelHeadMap.put(excelColumn.getIndex(), excelColumn.getFieldName());            }        }        return excelHeadMap;    }         /**     * 生成导出至Excel文件的数据     * @param sheet 工作区间     * @param excelColumns  excel表头     * @param excelHeadMap  excel表头对应实体属性     * @param excelHeadConvertMap   需要对数据进行特殊转换的列     * @param dataList      导入excel报表的数据来源     * @auther <a href="mailto:hubo@feinno.com">hubo</a>     * @return void     * 2012-4-19 上午09:36:37     */    private void buildExcelData(Sheet sheet, ExcelHead head, List<?> dataList) {        List<ExcelColumn> excelColumns = head.getColumns();         Map<String, Map> excelHeadConvertMap = head.getColumnsConvertMap();                 // 将表结构转换成Map        Map<Integer, String> excelHeadMap = convertExcelHeadToMap(excelColumns);                 // 从第几行开始插入数据        int startRow = head.getRowCount();        int order = 1;        for (Object obj : dataList) {            Row row = sheet.createRow(startRow++);            for (int j = 0; j < excelColumns.size(); j++) {                Cell cell = row.createCell(j);                cell.setCellType(excelColumns.get(j).getType());                String fieldName = excelHeadMap.get(j);                if(fieldName != null) {                    Object valueObject = BeanUtil.getProperty(obj, fieldName);                                         // 如果存在需要转换的字段信息,则进行转换                    if(excelHeadConvertMap != null && excelHeadConvertMap.get(fieldName) != null) {                        valueObject = excelHeadConvertMap.get(fieldName).get(valueObject);                    }                                         if(valueObject == null) {                        cell.setCellValue("");                    } else if (valueObject instanceof Integer) {                        cell.setCellValue((Integer)valueObject);                    } else if (valueObject instanceof String) {                        cell.setCellValue((String)valueObject);                    } else if (valueObject instanceof Date) {                        cell.setCellValue(new JDateTime((Date)valueObject).toString("YYYY-MM-DD"));                    } else {                        cell.setCellValue(valueObject.toString());                    }                } else {                    cell.setCellValue(order++);                }            }        }    }         /**     * 将Excel文件内容转换为List对象     * @param fis   excel文件     * @return  List<List> list存放形式的内容     * @throws IOException     * @auther <a href="mailto:hubo@feinno.com">hubo</a>     * @return List<List>     * 2012-4-18 上午11:37:17     */    public List<List> excelFileConvertToList(FileInputStream fis) throws Exception {        Workbook wb = WorkbookFactory.create(fis);                 Sheet sheet = wb.getSheetAt(0);                 List<List> rows = new ArrayList<List>();        for (Row row : sheet) {            List<Object> cells = new ArrayList<Object>();            for (Cell cell : row) {                Object obj = null;                                 CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());                 switch (cell.getCellType()) {                    case Cell.CELL_TYPE_STRING:                        obj = cell.getRichStringCellValue().getString();                        break;                    case Cell.CELL_TYPE_NUMERIC:                        if (DateUtil.isCellDateFormatted(cell)) {                            obj = new JDateTime(cell.getDateCellValue());                        } else {                            obj = cell.getNumericCellValue();                        }                        break;                    case Cell.CELL_TYPE_BOOLEAN:                        obj = cell.getBooleanCellValue();                        break;                    case Cell.CELL_TYPE_FORMULA:                        obj = cell.getNumericCellValue();                        break;                    default:                        obj = null;                }                cells.add(obj);            }            rows.add(cells);        }        return rows;    }         /**     * 根据Excel生成数据对象     * @param excelHeadMap 表头信息     * @param excelHeadConvertMap 需要特殊转换的单元     * @param rows     * @param cls      * @auther <a href="mailto:hubo@feinno.com">hubo</a>     * @return void     * 2012-4-18 上午11:39:43     */    private List buildDataObject(Map<Integer, String> excelHeadMap, Map<String, Map> excelHeadConvertMap, List<List> rows, Class cls) {        List contents = new ArrayList();        for (List list : rows) {            // 如果当前第一列中无数据,则忽略当前行的数据            if(list == null || list.get(0) == null) {                break;            }            // 当前行的数据放入map中,生成<fieldName, value>的形式            Map<String, Object> rowMap = rowListToMap(excelHeadMap, excelHeadConvertMap, list);                         // 将当前行转换成对应的对象            Object obj = null;            try {                obj = cls.newInstance();            } catch (InstantiationException ex) {                ex.printStackTrace();            } catch (IllegalAccessException ex) {                ex.printStackTrace();            }            BeanUtil.populateBean(obj, rowMap);                         contents.add(obj);        }        return contents;    }         /**     * 将行转行成map,生成<fieldName, value>的形式     * @param excelHeadMap 表头信息     * @param excelHeadConvertMap excelHeadConvertMap     * @param list     * @return     * @auther <a href="mailto:hubo@feinno.com">hubo</a>     * @return Map<String,Object>     * 2012-4-18 下午01:48:41     */    private Map<String, Object> rowListToMap(Map<Integer, String> excelHeadMap, Map<String, Map> excelHeadConvertMap, List list) {        Map<String, Object> rowMap = new HashMap<String, Object>();        for(int i = 0; i < list.size(); i++) {            String fieldName =  excelHeadMap.get(i);            // 存在所定义的列            if(fieldName != null) {                Object value = list.get(i);                if(excelHeadConvertMap != null && excelHeadConvertMap.get(fieldName) != null) {                    value = excelHeadConvertMap.get(fieldName).get(value);                }                rowMap.put(fieldName, value);            }        }        return rowMap;    }}

AgentSalesDetail.java 

package com.poi; import java.util.Date; /** * AgAgentSalesDetail entity. @author MyEclipse Persistence Tools */public class AgentSalesDetail implements java.io.Serializable {     // Fields     private Integer id;    private String orderNo;    private Date orderDate;    private Integer orderType;    private String belongOrgId;    private String belongOrgName;    private String agentId;    private String agentName;    private String entId;    private String entName;    private Integer businessSum;    private Integer contractSum;    private Integer dividePercent;    private Integer divideSum;    private Integer payforSum;    private Date payforDate;    private Integer isReceive;    private Integer receiveSum;    private Date receiveDate;    private String remark;    private Date createDate;     // Constructors     /** default constructor */    public AgentSalesDetail() {    }     /** minimal constructor */    public AgentSalesDetail(Date orderDate, Integer orderType,            String belongOrgName, String agentName, String entName,            Integer businessSum, Integer contractSum, Integer dividePercent,            Integer divideSum, Integer payforSum, Integer isReceive) {        this.orderDate = orderDate;        this.orderType = orderType;        this.belongOrgName = belongOrgName;        this.agentName = agentName;        this.entName = entName;        this.businessSum = businessSum;        this.contractSum = contractSum;        this.dividePercent = dividePercent;        this.divideSum = divideSum;        this.payforSum = payforSum;        this.isReceive = isReceive;    }     /** full constructor */    public AgentSalesDetail(String orderNo, Date orderDate,            Integer orderType, String belongOrgId, String belongOrgName,            String agentId, String agentName, String entId, String entName,            Integer businessSum, Integer contractSum, Integer dividePercent,            Integer divideSum, Integer payforSum, Date payforDate,            Integer isReceive, Integer receiveSum, Date receiveDate,            String remark, Date createDate) {        this.orderNo = orderNo;        this.orderDate = orderDate;        this.orderType = orderType;        this.belongOrgId = belongOrgId;        this.belongOrgName = belongOrgName;        this.agentId = agentId;        this.agentName = agentName;        this.entId = entId;        this.entName = entName;        this.businessSum = businessSum;        this.contractSum = contractSum;        this.dividePercent = dividePercent;        this.divideSum = divideSum;        this.payforSum = payforSum;        this.payforDate = payforDate;        this.isReceive = isReceive;        this.receiveSum = receiveSum;        this.receiveDate = receiveDate;        this.remark = remark;        this.createDate = createDate;    }     // Property accessors    public Integer getId() {        return this.id;    }     public void setId(Integer id) {        this.id = id;    }          public String getOrderNo() {        return this.orderNo;    }     public void setOrderNo(String orderNo) {        this.orderNo = orderNo;    }     public Date getOrderDate() {        return this.orderDate;    }     public void setOrderDate(Date orderDate) {        this.orderDate = orderDate;    }          public Integer getOrderType() {        return this.orderType;    }     public void setOrderType(Integer orderType) {        this.orderType = orderType;    }          public String getBelongOrgId() {        return this.belongOrgId;    }     public void setBelongOrgId(String belongOrgId) {        this.belongOrgId = belongOrgId;    }          public String getBelongOrgName() {        return this.belongOrgName;    }     public void setBelongOrgName(String belongOrgName) {        this.belongOrgName = belongOrgName;    }          public String getAgentId() {        return this.agentId;    }     public void setAgentId(String agentId) {        this.agentId = agentId;    }          public String getAgentName() {        return this.agentName;    }     public void setAgentName(String agentName) {        this.agentName = agentName;    }          public String getEntId() {        return this.entId;    }     public void setEntId(String entId) {        this.entId = entId;    }          public String getEntName() {        return this.entName;    }     public void setEntName(String entName) {        this.entName = entName;    }          public Integer getBusinessSum() {        return this.businessSum;    }     public void setBusinessSum(Integer businessSum) {        this.businessSum = businessSum;    }          public Integer getContractSum() {        return this.contractSum;    }     public void setContractSum(Integer contractSum) {        this.contractSum = contractSum;    }          public Integer getDividePercent() {        return this.dividePercent;    }     public void setDividePercent(Integer dividePercent) {        this.dividePercent = dividePercent;    }          public Integer getDivideSum() {        return this.divideSum;    }     public void setDivideSum(Integer divideSum) {        this.divideSum = divideSum;    }          public Integer getPayforSum() {        return this.payforSum;    }     public void setPayforSum(Integer payforSum) {        this.payforSum = payforSum;    }     public Date getPayforDate() {        return this.payforDate;    }     public void setPayforDate(Date payforDate) {        this.payforDate = payforDate;    }          public Integer getIsReceive() {        return this.isReceive;    }     public void setIsReceive(Integer isReceive) {        this.isReceive = isReceive;    }          public Integer getReceiveSum() {        return this.receiveSum;    }     public void setReceiveSum(Integer receiveSum) {        this.receiveSum = receiveSum;    }     public Date getReceiveDate() {        return this.receiveDate;    }     public void setReceiveDate(Date receiveDate) {        this.receiveDate = receiveDate;    }          public String getRemark() {        return this.remark;    }     public void setRemark(String remark) {        this.remark = remark;    }     public Date getCreateDate() {        return this.createDate;    }     public void setCreateDate(Date createDate) {        this.createDate = createDate;    }     @Override    public String toString() {        return "AgentSalesDetail [agentId=" + agentId + ", agentName="                + agentName + ", belongOrgId=" + belongOrgId                + ", belongOrgName=" + belongOrgName + ", businessSum="                + businessSum + ", contractSum=" + contractSum                + ", createDate=" + createDate + ", dividePercent="                + dividePercent + ", divideSum=" + divideSum + ", entId="                + entId + ", entName=" + entName + ", id=" + id                + ", isReceive=" + isReceive + ", orderDate=" + orderDate                + ", orderNo=" + orderNo + ", orderType=" + orderType                + ", payforDate=" + payforDate + ", payforSum=" + payforSum                + ", receiveDate=" + receiveDate + ", receiveSum=" + receiveSum                + ", remark=" + remark + "]";    } }

ExcelHelperTest.java 

package com.poi; import java.io.File;import java.io.FileInputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; import jodd.datetime.JDateTime; import org.apache.poi.ss.usermodel.Cell;import org.junit.Test; import com.excel.ExcelColumn;import com.excel.ExcelHead;import com.excel.ExcelHelper; /** * poi包操作excel测试 * * @createTime: 2012-4-13 下午06:21:10 * @author: <a href="mailto:hubo@feinno.com">hubo</a> * @version: 0.1 * @lastVersion: 0.1 * @updateTime:  * @updateAuthor: <a href="mailto:hubo@feinno.com">hubo</a> * @changesSum:  *  */public class ExcelHelperTest {//  @Test    public void excelHelperImportTest() {        // excel列结构        List<ExcelColumn> excelColumns = new ArrayList<ExcelColumn>();        excelColumns.add(new ExcelColumn(0, "", "序号"));        excelColumns.add(new ExcelColumn(1, "orderDate", "日期"));        excelColumns.add(new ExcelColumn(2, "belongOrgName", "分支机构"));        excelColumns.add(new ExcelColumn(3, "agentName", "代理商名称"));        excelColumns.add(new ExcelColumn(4, "entName", "企业名称"));        excelColumns.add(new ExcelColumn(5, "businessSum", "业务金额"));        excelColumns.add(new ExcelColumn(6, "contractSum", "合同金额"));        excelColumns.add(new ExcelColumn(7, "dividePercent", "分成比例"));        excelColumns.add(new ExcelColumn(8, "divideSum", "分成金额"));        excelColumns.add(new ExcelColumn(9, "orderType", "订单类型"));        excelColumns.add(new ExcelColumn(10, "payforSum", "应结算金额(元)"));        excelColumns.add(new ExcelColumn(11, "payforDate", "应结算日期"));        excelColumns.add(new ExcelColumn(12, "isReceive", "是否到帐"));        excelColumns.add(new ExcelColumn(13, "receiveSum", "到帐金额(元)"));        excelColumns.add(new ExcelColumn(14, "receiveDate", "到帐日期"));        excelColumns.add(new ExcelColumn(15, "remark", "备注"));                 // 需要特殊转换的单元        Map<String, Map> excelColumnsConvertMap = new HashMap<String, Map>();        Map<String, Integer> isReceive = new HashMap<String, Integer>();        isReceive.put("是", 1);        isReceive.put("否", 0);        excelColumnsConvertMap.put("isReceive", isReceive);        Map<String, Integer> orderType = new HashMap<String, Integer>();        orderType.put("新订单", 1);        orderType.put("续订订单", 2);        excelColumnsConvertMap.put("orderType", orderType);                 File sourceFile = new File("./xls/务工易后向产品销售明细表、销售汇总表.xls");                 ExcelHead head = new ExcelHead();        head.setRowCount(2); // 头所占行数        head.setColumns(excelColumns);  // 列的定义        head.setColumnsConvertMap(excelColumnsConvertMap); // 列的转换                 List<AgentSalesDetail> agentSalesList =  ExcelHelper.getInstanse().importToObjectList(head, sourceFile, AgentSalesDetail.class);                 for (AgentSalesDetail agentSalesDetail : agentSalesList) {            System.out.println(agentSalesDetail);        }    }         @Test    public void excelHelperExportTest() {        List<AgentSalesDetail> agentSalesDetails = new ArrayList<AgentSalesDetail>();        for (int i = 0; i < 20; i++) {            AgentSalesDetail asd = new AgentSalesDetail();            asd.setOrderDate(new JDateTime(2012, 04, 04).convertToDate());            asd.setBelongOrgName("杭州办");            asd.setAgentName("杭州萧聘网络\n科技有限公司");            asd.setEntName("杭州悦奥体育用品销售有限公司");            asd.setBusinessSum(900);            asd.setContractSum(900);            asd.setDividePercent(55);            asd.setDivideSum(495);            asd.setOrderType(1);            asd.setPayforSum(495);            asd.setPayforDate(new JDateTime(2012, 04, 04).convertToDate());            asd.setIsReceive(2);            asd.setReceiveSum(495);            asd.setReceiveDate(new JDateTime(2012, 04, 04).convertToDate());                         agentSalesDetails.add(asd);        }                 // excel结构        List<ExcelColumn> excelColumns = new ArrayList<ExcelColumn>();        excelColumns.add(new ExcelColumn(0, "", "序号"));        excelColumns.add(new ExcelColumn(1, "orderDate", "日期", Cell.CELL_TYPE_NUMERIC));        excelColumns.add(new ExcelColumn(2, "belongOrgName", "分支机构", Cell.CELL_TYPE_STRING));        excelColumns.add(new ExcelColumn(3, "agentName", "代理商名称", Cell.CELL_TYPE_STRING));        excelColumns.add(new ExcelColumn(4, "entName", "企业名称", Cell.CELL_TYPE_STRING));        excelColumns.add(new ExcelColumn(5, "businessSum", "业务金额", Cell.CELL_TYPE_NUMERIC));        excelColumns.add(new ExcelColumn(6, "contractSum", "合同金额", Cell.CELL_TYPE_NUMERIC));        excelColumns.add(new ExcelColumn(7, "dividePercent", "分成比例", Cell.CELL_TYPE_NUMERIC));        excelColumns.add(new ExcelColumn(8, "divideSum", "分成金额", Cell.CELL_TYPE_NUMERIC));        excelColumns.add(new ExcelColumn(9, "orderType", "订单类型", Cell.CELL_TYPE_NUMERIC));        excelColumns.add(new ExcelColumn(10, "payforSum", "应结算金额(元)", Cell.CELL_TYPE_NUMERIC));        excelColumns.add(new ExcelColumn(11, "payforDate", "应结算日期", Cell.CELL_TYPE_NUMERIC));        excelColumns.add(new ExcelColumn(12, "isReceive", "是否到帐", Cell.CELL_TYPE_STRING));        excelColumns.add(new ExcelColumn(13, "receiveSum", "到帐金额(元)", Cell.CELL_TYPE_NUMERIC));        excelColumns.add(new ExcelColumn(14, "receiveDate", "到帐日期", Cell.CELL_TYPE_NUMERIC));        excelColumns.add(new ExcelColumn(15, "remark", "备注", Cell.CELL_TYPE_STRING));                 // 需要特殊转换的单元        Map<String, Map> excelHeadConvertMap = new HashMap<String, Map>();        Map isReceive = new HashMap();        isReceive.put(1, "是");        isReceive.put(0, "否");        excelHeadConvertMap.put("isReceive", isReceive);        Map orderType = new HashMap();        orderType.put(1, "新订单");        orderType.put(2, "续订订单");        excelHeadConvertMap.put("orderType", orderType);                 File modelFile = new File("./xls/2010销售明细_model.xlsx");        File outputFile = new File("./xls/2010销售明细_export.xlsx");                 ExcelHead head = new ExcelHead();        head.setRowCount(2); // 模板中头部所占行数        head.setColumns(excelColumns);  // 列的定义        head.setColumnsConvertMap(excelHeadConvertMap); // 列的转换                 ExcelHelper.getInstanse().exportExcelFile(head, modelFile, outputFile, agentSalesDetails);    }     //  @Test    public void excelHelperExcelFileConvertToList() throws Exception {        FileInputStream fis = new FileInputStream("./xls/upload_4df3a05f_136cdc915cf__7ffd_00000000.tmp");        List<List> dataList = ExcelHelper.getInstanse().excelFileConvertToList(fis);        for (List list : dataList) {            for (Object object : list) {                System.out.print(object);                System.out.print("\t\t\t");            }            System.out.println();        }    }}



0 0