StoragePosXlsParser.java 2.5 KB
package com.myproject.poi;

import com.csvreader.CsvReader;
import com.myproject.bean.excel.StoragePosExcel;
import com.myproject.exception.ExcelParseException;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;

import java.io.IOException;
import java.util.List;

/**
 * Created by kangmor on 2015/11/17.
 */
public class StoragePosXlsParser extends BaseXlsParser {

    private final static String[] titles = new String[]{"位置","优先级", "高度",	/*"层数", "夹板厚度",
            "取料前点",	"取料点", "投料前点", "投料点", "起点", "终点", "偏差1", "偏差2", "偏差3", "偏差4",
            "左右边", "X轴行走", "Z(左右)", "皮带放料点",*/ "宽度"
    };

    private final static int EXT_DATA_SIZE = 50;

    @Override
    protected Object readData(HSSFRow hssfRow, HSSFRow startRow) throws ExcelParseException {
        StoragePosExcel storagePos = new StoragePosExcel();
        storagePos.setPosName(getValue(hssfRow.getCell(0)));
        String data = "";
        int errorColumn = 0;
        try {
            //storagePos.setH(hssfRow.getCell(1).getNumericCellValue());
            if (!StringUtils.isEmpty(getValue(hssfRow.getCell(1))))
                storagePos.setPriority(Double.valueOf(getValue(hssfRow.getCell(1))));
            if (!StringUtils.isEmpty(getValue(hssfRow.getCell(2))))
                storagePos.setH(Double.valueOf(getValue(hssfRow.getCell(2))).intValue());
            if (!StringUtils.isEmpty(getValue(hssfRow.getCell(3))))
                storagePos.setW(Double.valueOf(getValue(hssfRow.getCell(3))).intValue());

            for (int i = 4; i < 100; i++) {
                errorColumn = i+1;
                //位置Dxxx
                String posDxx = getValue(startRow.getCell(i));
                if (!StringUtils.isEmpty(posDxx)) {
                    String cellValue = getValue(hssfRow.getCell(i));
                    if (!StringUtils.isEmpty(cellValue)) {
                        data += posDxx + ":" + cellValue + "#";
                    }
                }
            }
        } catch (Exception e) {
            log.error(e);
            int errorRow = hssfRow.getRowNum() + 1;

            log.error("Number parse error at row: " + errorRow + " and column: "+errorColumn);
            log.error(e);
            throw new ExcelParseException("Excel解析错误在第" + errorRow + "行,"+errorColumn +"列");
        }
        return storagePos;
    }

    @Override
    protected String[] getTitles() {
        return titles;
    }
}