fanky
fanky

Reputation: 515

Uploading Excel file in swing

I am upload the Excel file and read that file according to format, but problem is that that file is not read. I am getting exception. I am adding all required jar file (dom4j-1.6.1,poi-3.9-20121203,poi-ooxml-3.9-20121203,poi-ooxml-schemas-3.9-20121203,xmlbeans-2.3.0). Please suggest me what I getting wrong. Exception is below.

public class Csv_Upload extends JFrame implements ActionListener
{
    public static JFrame f;
    JPanel panel;
    JButton b1,b2,b3,b4;
    JTextField txt1;
    JLabel l1;
    Font g,g1;
      JFileChooser fc;
      JTextArea log;
      File file1 ;
      String str;
    DBConnection connect=new  DBConnection();
     static private final String newline = "\n";
    public Csv_Upload() 
    {
        panel=(JPanel)getContentPane();
        panel.setLayout(null);
        g=new Font("Georgia",Font.BOLD,22);
        g1=new Font("Georgia",Font.BOLD,15);
        panel.setBackground(new java.awt.Color(204, 230 , 255));    
        l1=new JLabel("Upload Excel File");
        l1.setBounds(200, 50, 400, 30);
        l1.setFont(g);
        l1.setForeground(Color.RED);
        panel.add(l1);
            txt1=new JTextField();
        txt1.setBounds(480, 150,200,40);
        panel.add(txt1);
        fc = new JFileChooser();
        b1=new JButton("Browse File");
        b1.setBounds(50, 150,200,40);
        b1.setFont(g1);
        b1.setForeground(Color.RED);
        panel.add(b1);
        b2=new JButton("Upload File");
        b2.setBounds(260, 150,200,40);
        b2.setForeground(Color.RED);
        b2.setFont(g1);
        panel.add(b2);
        ImageIcon img=new ImageIcon("calender.png");
        b3=new JButton(img);
        b3.setBounds(50, 230,50,30);
        b3.setForeground(Color.RED);
        b3.setFont(g1);
        panel.add(b3);      
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
//      b4.addActionListener(this);
        }

    @Override
    public void actionPerformed(ActionEvent e)
    {
         if (e.getSource() == b1) {
              int returnVal = fc.showOpenDialog(Csv_Upload.this);

              if (returnVal == JFileChooser.APPROVE_OPTION) {
             file1 = fc.getSelectedFile();
             str=String.valueOf(file1);
                System.out.println("file fath"+file1);
              } 
   //Handle save button action.
} 
         if(e.getSource()==b2)
         {
             try
                {
                    FileInputStream file = new FileInputStream(new File(str));
                    System.out.println("action performed in file"+file);
                    XSSFWorkbook workbook = new XSSFWorkbook(file);
                    XSSFSheet sheet = workbook.getSheetAt(0);
                    Iterator<Row> rowIterator = sheet.iterator();
                    while (rowIterator.hasNext()) 
                    {
                        Row row = rowIterator.next();
                        //For each row, iterate through all the columns
                        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() + "\t");
                                    break;
                                case Cell.CELL_TYPE_STRING:
                                    System.out.print(cell.getStringCellValue() + "\t");
                                    break;
                            }
                        }
                        System.out.println("");
                    }
                    file.close();
                } 
                catch (Exception e1) 
                {
                    e1.printStackTrace();
                }
         }

         if(e.getSource().equals(b3))
            {
            txt1.setText(new DatePicker(f).setPickedDate());
            }
          }

    public static void main(String []s)
        {
             f=new Csv_Upload();
            f.setVisible(true);
            f.setSize(750,500);

            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
}

Stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError:
    org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(Ljava/io/InputStream;)V
at ADD.Csv_Upload.actionPerformed(Csv_Upload.java:129)
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$200(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)

Upvotes: 2

Views: 970

Answers (1)

Catalina Island
Catalina Island

Reputation: 7126

The class org.apache.poi.xssf.usermodel.XSSFWorkbook has a constructor init method that accepts a java.io.InputStream, and XSSFWorkbook is located in poi-ooxml-schemas-3.9-20121203.jar. Check the README file in your project's dist directory. Also check the project JAR's manifest, which should look something like this:

Class-Path: lib/poi-3.9-20121203.jar lib/poi-ooxml-3.9-20121203.jar …

Upvotes: 3

Related Questions