java - 线程中的异常 "AWT-EventQueue-0"java.lang.NoSuchMethodError : org. apache.poi.util.POILogger.log(I[Ljava/lang/Object;)V

标签 java apache-poi

过去一周我一直在从事一个工作项目,但由于不断收到错误,我陷入了困境。我想知道是否有人可以帮助我。

我已附上我创建的两个文件以及我收到的错误。如果有人能告诉我如何修复该错误,我将不胜感激

我正在使用 eclipse 并附加了所有最新的 jar 文件(xmlbeans2.6、poi-scratchpad 3.13beta、poi-ooxml-schemas 3.13beta、poi-ooxml-3.13beta 和 poi-3.13 beta)

第一个类创建一个用户可以与之交互的界面。首先你打开一个Excel文件。然后通过点击分类按钮,它应该调用第二类。第二节课将读取 Excel 文件并显示其内容。截至目前,如果我将调用函数更改为 readxlsfile 函数,它就可以完美运行。当我尝试调用 readxlsxfile 时出现问题。我收到以下错误

如果可能的话,谁能帮我找到一种方法,根据所选的 Excel 文件自动选择正确的函数。

public class RedoApplication extends JFrame{

private String directory;
private JFileChooser choose;
public RedoAPP RApp;
public File savefile;

public void setDirectory(String d)
{
    this.directory = d;
}

public String getDirectory()
{
    return this.directory;
}
private RedoApplication()
{
    super("Redo Categorization");
    setSize(750, 600);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();

    c.setLayout(null);

    final JTextArea instructionBar = new JTextArea(""
            + "REQUIREMENTS:\n"
            + "* Selected Excel(.xlsx) file should be CLOSED when executing\n"
            + "* 1ST WORKSHEET HAS TO BE THE RAW DATA\n\n"
            + "RECOMMENDATIONS:\n"
            + "Best if only HA or DTV products are in the raw data.\n\n"
            + "INSTRUCTION:\n1. Choose raw Excl file(only .xlsx)\n2. Do not alter default column names, but order doesn't matter\n\n"
            + "WHEN CATEGORIZED:\n1. The file you have selected will have categorization in the last column\n2. Statistics will be in last worksheet\n\n"
            + "ABOUT LOGIC FILE:\n"
            + "Logic file has to be in the same folder\n"
            + "The workbook(tab) order and name should stay the same.\n"
            + "Keyword sheet's name of keywords list should is also set.");

    instructionBar.setBounds(20, 90, 220, 450);
    instructionBar.setLineWrap(true);
    Font font = new Font("ARIAL", Font.BOLD, 12);
    instructionBar.setFont(font);       
    instructionBar.setWrapStyleWord(true); 
    instructionBar.setEditable(false);

    JLabel label = new JLabel();

    ImageIcon icon = new ImageIcon("c:/users....");
    label.setIcon(icon);
    label.setBounds(10, 10, 300, 50);

    JButton openButton = new JButton("Choose file");
    openButton.setBounds(330, 90, 100, 30);

    final JLabel statusBar = new JLabel("Accepting file...", SwingConstants.CENTER);
    statusBar.setBounds(280, 140, 200, 30);

    final JProgressBar progressBar = new JProgressBar();
    progressBar.setBounds(280, 340, 150, 20);
    progressBar.setStringPainted(true);

    final BlinkLabel bl = new BlinkLabel("Standby");
    bl.setBounds(310, 270, 150, 100);
    bl.setBlinking(false);

    final JButton catButton = new JButton("Categorize");        
    catButton.setBounds(330, 190, 100, 100);

    Font font1 = new Font("ARIAL", Font.BOLD, 14);
    JLabel analysisBar = new JLabel("Quick analysis:");
    analysisBar.setBounds(480, 40, 200, 100);
    analysisBar.setFont(font1);
    analysisBar.setForeground(Color.BLUE);

    final JTextArea analysisArea = new JTextArea("No analysis yet.");
    analysisArea.setBounds(480, 100, 250, 200);
    analysisArea.setFont(font1);
    analysisArea.setEditable(false);

    final ButtonGroup g = new ButtonGroup();

    JRadioButton rb1 = new JRadioButton("DTV", true);
    rb1.setActionCommand("DTV");
    rb1.setBounds(330, 40, 60, 30);
    rb1.setBackground(Color.WHITE);

    JRadioButton rb2 = new JRadioButton("HA", false);
    rb2.setActionCommand("HA");
    rb2.setBounds(390, 40, 60, 30);
    rb2.setBackground(Color.WHITE);



    openButton.addActionListener
    (
            new ActionListener() 
            {
                public void actionPerformed(ActionEvent ae) 
                {

                    String[] excel = new String[] { "xlsx" };

                    choose = new JFileChooser("C:\\Users\\.....");

                    choose.addChoosableFileFilter(new SimpleFileFilter(excel,"Excel(2007-) (*.xlsx)"));

                    choose.setAcceptAllFileFilterUsed(true);

                    //InputFile = choose.getSelectedFile();

                    int option = choose.showOpenDialog(RedoApplication.this);

                    if (option == JFileChooser.APPROVE_OPTION) 
                    {
                        if (choose.getSelectedFile() != null)
                        {
                            statusBar.setText(choose.getSelectedFile().getName());

                            setDirectory(choose.getSelectedFile().getAbsolutePath());
                        }
                    } 
                    else 
                    {
                        statusBar.setText("No file selected");
                    }

                }
            }
        );

    catButton.addActionListener
    (
            new ActionListener() 
            {
                public void actionPerformed(ActionEvent ae) 
                {
                    System.out.println(g.getSelection().getActionCommand());
                    if(g.getSelection().getActionCommand().equals("DTV"))
                    {
                        File savefile = choose.getSelectedFile();
                        RedoAPP RApp = new RedoAPP();
                        RApp.readxlsxfile(savefile);


                    }
                }
            }
    );

    ActionListener alRadio = new ActionListener() 
     {
            public void actionPerformed(ActionEvent e) 
            {

            }
     };     
            c.add(instructionBar);
            c.add(openButton);
            c.add(statusBar);
            c.add(catButton);
            c.add(analysisBar);
            c.add(analysisArea);

            c.add(label);
            getContentPane().setBackground(Color.WHITE);

            rb1.addActionListener(alRadio);
            rb2.addActionListener(alRadio);
            g.add(rb1);
            g.add(rb2);
            c.add(rb1);
            c.add(rb2);
            c.add(progressBar);
            c.add(bl);

            setVisible(true);
}




public static void main(String args[]) 
{

    RedoApplication Redo = new RedoApplication();


}}

公共(public)类RedoAPP{

public void readxlsfile(File savefile)
{
    try
    {
        FileInputStream file = new FileInputStream(savefile);

        //Create Workbook
        HSSFWorkbook workbook = new HSSFWorkbook(file);
        //Get first sheet
        HSSFSheet sheet = workbook.getSheetAt(0);
        //Go through each row one by one
        Iterator<Row> rowIterator = sheet.rowIterator();

        while (rowIterator.hasNext())
        {
            Row row = rowIterator.next();
            //Iterate through each column of each row
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext())
            {
                Cell cell = cellIterator.next();
                //check cell type and format accordingly
                switch (cell.getCellType())
                {
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "  ");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getRichStringCellValue() + "  ");
                    break;
                }
            }
            System.out.println("");
        }
        file.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}


public void readxlsxfile(File savefile)
{
    try
    {
        FileInputStream file1 = new FileInputStream(savefile);

        XSSFWorkbook workbook = new XSSFWorkbook(file1);
        XSSFSheet sheet = workbook.getSheetAt(0);

        Iterator<Row> rowIterator = sheet.rowIterator();

        while (rowIterator.hasNext())
        {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext())
            {
                Cell cell = cellIterator.next();

                switch (cell.getCellType())
                {
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "  ");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getRichStringCellValue() + "  ");
                    break;
                }
            }
            System.out.println("");
        }
        file1.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

}

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.poi.util.POILogger.log(I[Ljava/lang/Object;)V at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.parseRelationshipsPart(PackageRelationshipCollection.java:313) at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.(PackageRelationshipCollection.java:163) at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.(PackageRelationshipCollection.java:131) at org.apache.poi.openxml4j.opc.PackagePart.loadRelationships(PackagePart.java:561) at org.apache.poi.openxml4j.opc.PackagePart.(PackagePart.java:109) at org.apache.poi.openxml4j.opc.PackagePart.(PackagePart.java:80) at org.apache.poi.openxml4j.opc.PackagePart.(PackagePart.java:125) at org.apache.poi.openxml4j.opc.ZipPackagePart.(ZipPackagePart.java:78) at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:245) at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:684) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:275) at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:274) at samsung.RedoAPP.readxlsxfile(RedoAPP.java:73) at samsung.RedoApplication$2.actionPerformed(RedoApplication.java:176) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

我相信您已将其配置为使用相同版本的 POI,但我仍然认为旧版本正在从某处泄漏到您的类路径中。以下代码取自the POI FAQs ;请绝对确定以下所有三个输出均符合您的预期。

ClassLoader classloader =
   org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader();
URL res = classloader.getResource(
             "org/apache/poi/poifs/filesystem/POIFSFileSystem.class");
String path = res.getPath();
System.out.println("POI Core came from " + path);

classloader = org.apache.poi.POIXMLDocument.class.getClassLoader();
res = classloader.getResource("org/apache/poi/POIXMLDocument.class");
path = res.getPath();
System.out.println("POI OOXML came from " + path);

classloader = org.apache.poi.hslf.HSLFSlideShow.class.getClassLoader();
res = classloader.getResource("org/apache/poi/hslf/HSLFSlideShow.class");
path = res.getPath();
System.out.println("POI Scratchpad came from " + path);

关于java - 线程中的异常 "AWT-EventQueue-0"java.lang.NoSuchMethodError : org. apache.poi.util.POILogger.log(I[Ljava/lang/Object;)V,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31793939/

相关文章:

java - 使用变量调用 onclick 函数

java - Equals 枚举类型中的方法

java - 由 Apache POI 创建时 excel 文件中的兼容模式

java - Excel::String 单元格值未与 Java 中的整数连接

java - 对于使用 apache POI 转换为 CSV 时的 xlsx 单元格数据

java - 如何在 Selenium 中有效管理浏览器选项卡\窗口?

java - 使用 Apache POI (Java) 将 .docx 中的文本替换为表格

apache-poi - 是否可以在露天使用 apache POI 转换?

excel - 如何使用 POI 4.0.1 Java 添加垂直线到条形图

java - 文本重复