如何将使用SAX从Excel读取的数据存储到HashMap中并与其他类

时间:2015-10-09 07:11:40

标签: spring java-ee hashmap singleton sax

我一直在使用Spring和java ee,我必须将信息存储到ArrayList<HashMap<String,Object>>中,以便与其他类共享此结构。 一类放置信息,其他类放置数据。 最好的方法是什么? 我需要在我的网站上为每个用户使用这些数据,我想使用单例,这样好吗?

@Singleton
public class ExcelFields {
    //data structure used to store cell coordinates and value where cell coordinates are the keys
    private HashMap<String,Object> firstRowValues;
    private HashMap<String,Object> secondRowValues;
    private HashMap<String,Object> thirdRowValues;
    private ArrayList<HashMap<String, Object>> rowValues;
    private static ExcelFields instance = null;

    @PostConstruct
    public void init(){
        this.firstRowValues=new HashMap<String,Object>();
        this.secondRowValues=new HashMap<String,Object>();
        this.thirdRowValues=new HashMap<String,Object>();
        this.rowValues=new ArrayList<HashMap<String,Object>>();
    }
    /**
     * @return the firstRowValues
     */
    public HashMap<String, Object> getFirstRowValues() {
        return firstRowValues;
    }
    /**
     * @param firstRowValues the firstRowValues to set
     */
    public void setFirstRowValues(HashMap<String, Object> firstRowValues) {
        this.firstRowValues = firstRowValues;
    }
    /**
     * @return the secondRowValues
     */
    public HashMap<String, Object> getSecondRowValues() {
        return secondRowValues;
    }
    /**
     * @param secondRowValues the secondRowValues to set
     */
    public void setSecondRowValues(HashMap<String, Object> secondRowValues) {
        this.secondRowValues = secondRowValues;
    }
    /**
     * @return the thirdRowValues
     */
    public HashMap<String, Object> getThirdRowValues() {
        return thirdRowValues;
    }
    /**
     * @param thirdRowValues the thirdRowValues to set
     */
    public void setThirdRowValues(HashMap<String, Object> thirdRowValues) {
        this.thirdRowValues = thirdRowValues;
    }
    /**
     * @return the rowValues
     */
    public ArrayList<HashMap<String, Object>> getRowValues() {
        return rowValues;
    }
    /**
     * @param rowValues the rowValues to set
     */
    public void setRowValues(ArrayList<HashMap<String, Object>> rowValues) {
        this.rowValues = rowValues;
    }

    public void addRowValues(HashMap<String, Object> rowValues2){
        this.rowValues.add(rowValues2);
    }

    public void clearAll(){
        firstRowValues.clear();
        secondRowValues.clear();
        thirdRowValues.clear();
        rowValues.clear();
    }



}

这是用例:用户上传的excel文件 - 我读了这个文件并将其映射到HashMap-我用这个信息来填充数据库,然后我可以清除HashMap以备将来上传(可能是很多用户同时使用) 这是Service类中调用Excel read:

的方法
private void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, InputStream sheetInputStream) throws Exception {
        InputSource sheetSource = new InputSource(sheetInputStream);
        SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxFactory.newSAXParser();
        XMLReader sheetParser = saxParser.getXMLReader();
        ContentHandler handler = new MyXSSFSheetHandler(styles, strings);
        sheetParser.setContentHandler(handler);
        sheetParser.parse(sheetSource);
    }

如何看待MyXSSFSheetHandler返回ContentHandler,以便我无法返回HashMap。 此外,HashMap必须用于使用HashMap值存储到数据库中的DatabaseServicesImpl服务

更新:我试图在MyXSSFSheetHandler中使用HashMAp添加get方法,但是当我调用它时,地图为空,它的代码为this:http://www.mysamplecode.com/2011/08/java-sax-parser-example-code-part-2.html

1 个答案:

答案 0 :(得分:0)

如果您希望下次每个部署的数据都将数据保存在.properties文件中并访问它。