java—如何根据条件返回代码中的main方法?

t3psigkw  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(337)

在这个节目里我正在读 .xlsx 文件。并且把单元格数据加到向量上,如果向量大小小于12就不需要读取剩余的数据了,我需要走main方法。在我的程序中我能做什么?
这是我的密码:

package com.read;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;
import org.apache.poi.openxml4j.opc.OPCPackage;
import java.io.InputStream;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

public class SendDataToDb {
public static void main(String[] args) {
    SendDataToDb sd = new SendDataToDb();
    try {
        sd.processOneSheet("C:/Users/User/Desktop/New folder/Untitled 2.xlsx");
        System.out.println("in Main method");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public void processOneSheet(String filename) throws Exception {
    System.out.println("executing Process Method");
    OPCPackage pkg = OPCPackage.open(filename);
    XSSFReader r = new XSSFReader( pkg );
    SharedStringsTable sst = r.getSharedStringsTable();
    System.out.println("count "+sst.getCount());
    XMLReader parser = fetchSheetParser(sst);

    // To look up the Sheet Name / Sheet Order / rID,
    //  you need to process the core Workbook stream.
    // Normally it's of the form rId# or rSheet#
    InputStream sheet2 = r.getSheet("rId2");
    System.out.println("Sheet2");
    InputSource sheetSource = new InputSource(sheet2);
    parser.parse(sheetSource);
    sheet2.close();
}

public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {
    //System.out.println("EXECUTING fetchSheetParser METHOD");
    XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
    ContentHandler handler = new SheetHandler(sst);
    parser.setContentHandler(handler);
    System.out.println("Method :fetchSheetParser");
    return parser;
}

/**
 * See org.xml.sax.helpers.DefaultHandler javadocs 
 */
private  class SheetHandler extends DefaultHandler {

    private SharedStringsTable sst;
    private String lastContents;
    private boolean nextIsString;
    Vector values = new Vector(20);

    private SheetHandler(SharedStringsTable sst) {
        this.sst = sst;

    }

    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        // c => cell
        //long l = Long.valueOf(attributes.getValue("r"));
        if(name.equals("c")){
            columnNum++;
        }
        if(name.equals("c")) {
            // Print the cell reference
            // Figure out if the value is an index in the SST
            String cellType = attributes.getValue("t");
            if(cellType != null && cellType.equals("s")) {
                nextIsString = true;
            } else {
                nextIsString = false;
            }
        }

        // Clear contents cache
        lastContents = "";
    }

    public void endElement(String uri, String localName, String name)
            throws SAXException {
        //System.out.println("Method :222222222");
        // Process the last contents as required.
        // Do now, as characters() may be called more than once
        if(nextIsString) {
            int idx = Integer.parseInt(lastContents);
            lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
            nextIsString = false;
        }
        // v => contents of a cell
        // Output after we've seen the string contents
        if(name.equals("v")) {

             values.add(lastContents);

        }
        if(name.equals("row")) {
            System.out.println(values);
            //values.setSize(50);
            System.out.println(values.size()+"  "+values.capacity());
         //********************************************************
      //I AM CHECKING CONDITION HERE, IF CONDITION IS TRUE I NEED STOP THE REMAINING PROCESS AND GO TO MAIN METHOD.  
            if(values.size() < 12)
            values.removeAllElements();
  //WHAT CODE I NEED TO WRITE HERE TO STOP THE EXECUTION OF REMAINING PROCESS AND GO TO MAIN

         //***************************************************************

        }

    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {
        //System.out.println("method : 333333333333");
        lastContents += new String(ch, start, length);
    }
}

}
请检查//行和之间的代码//***************

uxh89sit

uxh89sit1#

您可以在希望停止解析的任何位置引发saxexception: throw new SAXException("<Your message>") 用主方法处理。

n7taea2i

n7taea2i2#

在检查之后,应该抛出异常以从那里跳出并将其返回到main方法。

throw new Exception("vector size has to be less than 12");

相关问题