我过去一周一直致力于一个工作项目,因为我一直在犯错误而陷入困境。我想知道是否有人可以帮助我。
我附上了我创建的两个文件以及我收到的错误。如果有人能告诉我如何修复错误,将非常感激
我正在使用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();
}}
公共类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();
}
}
}
线程中的异常&#34; AWT-EventQueue-0&#34; java.lang.NoSuchMethodError:org.apache.poi.util.POILogger.log(I [Ljava / lang / Object;)V 在org.apache.poi.openxml4j.opc.PackageRelationshipCollection.parseRelationshipsPart(PackageRelationshipCollection.java:313) 在org.apache.poi.openxml4j.opc.PackageRelationshipCollection。(PackageRelationshipCollection.java:163) 在org.apache.poi.openxml4j.opc.PackageRelationshipCollection。(PackageRelationshipCollection.java:131) 在org.apache.poi.openxml4j.opc.PackagePart.loadRelationships(PackagePart.java:561) 在org.apache.poi.openxml4j.opc.PackagePart。(PackagePart.java:109) 在org.apache.poi.openxml4j.opc.PackagePart。(PackagePart.java:80) 在org.apache.poi.openxml4j.opc.PackagePart。(PackagePart.java:125) 在org.apache.poi.openxml4j.opc.ZipPackagePart。(ZipPackagePart.java:78) 在org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:245) 在org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:684) 在org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:275) 在org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) 在org.apache.poi.xssf.usermodel.XSSFWorkbook。(XSSFWorkbook.java:274) 在samsung.RedoAPP.readxlsxfile(RedoAPP.java:73) 在samsung.RedoApplication $ 2.actionPerformed(RedoApplication.java:176) 在javax.swing.AbstractButton.fireActionPerformed(未知来源) at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source) 在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源) 在javax.swing.DefaultButtonModel.setPressed(未知来源) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源) at java.awt.Component.processMouseEvent(Unknown Source) 在javax.swing.JComponent.processMouseEvent(未知来源) 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) 在java.awt.EventQueue.access $ 500(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) 在java.awt.EventQueue $ 4.run(未知来源) 在java.awt.EventQueue $ 4.run(未知来源) 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) 在java.awt.EventDispatchThread.run(未知来源)
答案 0 :(得分:2)
我相信您已将其配置为使用相同版本的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);