CuzDataTable.java 3.79 KB
package com.xiniunet.util;

import com.xiniunet.framework.util.excel.datatable.Cell;
import com.xiniunet.framework.util.excel.datatable.Column;
import com.xiniunet.framework.util.excel.datatable.DataRow;
import com.xiniunet.framework.util.excel.datatable.DataTable;
import com.xiniunet.framework.util.excel.exception.TemplateNotMatchException;
import com.xiniunet.framework.util.excel.util.ExcelUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2017/4/28.
 */
public class CuzDataTable extends DataTable {

    /**
     * 构造方法。传入Excel流,不需要指定类型。同时,也不会对数据进行检查。
     * @param bytes         Excel的字节数组。
     * @param headRowIndex         表头开始行下标
     * @throws IOException    可能会出现的错误。通常为以下错误:<br/>
     * 1.流不能转换为Workbook
     */
    public CuzDataTable(byte[] bytes, int headRowIndex) throws IOException,TemplateNotMatchException {
        init();
        InputStream inputStream = null;         //文件输入流
        Workbook workbook = null;               //导入的文档
        boolean flag;           //用于判断文件的类型
        try {
            flag = true;
            inputStream = new ByteArrayInputStream(bytes);
            workbook = new HSSFWorkbook(inputStream);
        } catch (Exception e) {
            flag = false;
        }
        if( !flag) {
            try {
                flag = true;
                inputStream = new ByteArrayInputStream(bytes);
                workbook = new XSSFWorkbook(inputStream);
            } catch (Exception e) {
                flag = false;
            }
        }
        if(inputStream != null) {
            inputStream.close();
        }
        if( !flag ) {
            throw new TemplateNotMatchException("不支持的文件类型");
        }

		/* 创建导入表的对象 */
        Sheet sheet = workbook.getSheetAt(0);     //默认只处理第一个Sheet表
        this.setName(sheet.getSheetName());

		/* 读取表头 */
        Row headRow = sheet.getRow(headRowIndex);
        int columnNum = headRow.getPhysicalNumberOfCells();
        for(int i=0; i<columnNum; i++) {
            org.apache.poi.ss.usermodel.Cell cell = headRow.getCell(i);
            Column column = new Column();
            column.setName(ExcelUtil.getValue(cell));
            this.addColumn(column);
        }

        //表头开始之前的数据存放到 headInfo中
        String headInfo = "";
        for(int i = 0; i < headRowIndex; i++){
            Row inputRow = sheet.getRow(i);
            if(null != inputRow) {
                headInfo +=  ExcelUtil.getValue(inputRow.getCell(0));
            }
        }
        if (StringUtils.isNotBlank(headInfo)){
            this.setHeadInfo(headInfo);
        }

		/* 逐行读取导入文件的数据,从1开始是因为第0行为表头 */
        int rowNumber = sheet.getLastRowNum();
        for(int i = headRowIndex + 1; i <= rowNumber; i ++) {
            Row inputRow = sheet.getRow(i);
            DataRow row = new DataRow();
            if(null != inputRow) {
                for (int j = 0; j < columnNum; j++) {
                    org.apache.poi.ss.usermodel.Cell excelCell = inputRow.getCell(j);
                    if (null != excelCell ) {
                        row.put(this.getColumns()[j].getName(), new Cell(ExcelUtil.getValue(excelCell)));
                    }
                }
            }
            this.addRow(row);
        }
    }
}