All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edi.rule.work.processor.JSSAXSheetContent Maven / Gradle / Ivy

The newest version!
package edi.rule.work.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edi.rule.util.ZSVessel;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;
import edi.rule.config.JSRuleContext;
import edi.rule.core.JSRuleDBCompatible;
import edi.rule.model.JSRuleAction;
import edi.rule.model.JSRuleImportSheet;
import edi.rule.work.constant.JSRuleJsonConfig;
import edi.rule.work.custom.JSRuleException;
import lombok.Data;

/**
 * @author 摩拉克斯
 * @date 2022年7月8日 上午10:30:40
 */
@Data
public final class JSSAXSheetContent> implements SheetContentsHandler{

	public final List> result;
	private JSRuleImportSheet sheet;
	private Map rowData;
	private String value;
	private int rowNumber;
	private final Map headInfo;
	private final Set columnInfo;
	private boolean isDataLine = false;
	private boolean isHeadLine = false;
	
	public JSSAXSheetContent(JSRuleImportSheet param) {
		if (param.getHeadLine()<1 || param.getDataLine()<2) {
			throw new JSRuleException("the property of headLine can't be less than 1 and dataLine can't be less than 2","headLine行号不能小于1且dataLine行号不能小于2");
		}
		if (param.getHeadLine()>=param.getDataLine()) {
    		throw new JSRuleException("the property of headLine must be less than dataLine","headLine行号必须小于dataLine");
    	}
    	this.sheet = param;
		this.result = new ArrayList<>();
		this.headInfo = new HashMap<>();
		this.columnInfo = new HashSet<>();
	}
	
    private void initRow(int rowNum) {
    	rowNumber = rowNum+1;
    	if (!isDataLine) {
    		if (rowNumber>= sheet.getDataLine()) {
    			isDataLine = true;
    		}else if (rowNumber== sheet.getHeadLine()) {
    			isHeadLine = true;
    		}
    	}
    }

	@Override
	public void startRow(int rowNum) {
		initRow(rowNum);
		rowData = new HashMap<>();
	}

	@Override
	public void cell(String cellReference, String formattedValue, XSSFComment comment) {
		value = formattedValue.trim();
		if (isDataLine && !value.equals(JSRuleJsonConfig.POI_DEFAULT_NULL_SIGN)) {
			rowData.put(headInfo.get(getColumnName(cellReference)),value);
		}else if (isHeadLine) {
			headInfo.put(getColumnName(cellReference), sheet.getFieldMappings().get(value));
			columnInfo.add(sheet.getFieldMappings().get(value));
		}
	}
	
	@Override
	public void endRow(int rowNum) {
		if (isDataLine) {
			if (JSRuleContext.getProperties().poi.excel.importFilterEmptyRow) {
				if (ZSVessel.isEmpty(rowData)) {
					return;
				}
			}
			rowData.remove(null);
			JSRuleDBCompatible.importSetNullValue(rowData, columnInfo);
			result.add(rowData);
		}else if (isHeadLine) {
			isHeadLine = false;
			columnInfo.remove(null);
		}
	}
	
	private String getColumnName(String cellName) {
    	return cellName.substring(0,cellName.indexOf(""+rowNumber));
    }

    @SuppressWarnings("unused")
    private int transformCellNameToNum(String cellName) {
        int result = 0;
        for (char c : cellName.toCharArray()) {
        	if (Character.isAlphabetic(c)) {
        		result = result * 26 + (c - 'A') + 1;
        	}
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy