简体   繁体   English

Excel中的Apache POI

[英]excel java apache poi

I want to insert excel data to the database using this code. 我想使用此代码将excel数据插入数据库。

public class Insert {
      public static void main( String [] args ) {
        String fileName="C:\\File.xls";
        Vector dataHolder=read(fileName);
        saveToDatabase(dataHolder);
    }
        public static Vector read(String fileName)    {
        Vector cellVectorHolder = new Vector();
        try{
                FileInputStream myInput = new FileInputStream(fileName);
                POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
            HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
            HSSFSheet mySheet = myWorkBook.getSheetAt(0);
           Iterator rowIter = mySheet.rowIterator(); 
           while(rowIter.hasNext()){
                  HSSFRow myRow = (HSSFRow) rowIter.next();
                  Iterator cellIter = myRow.cellIterator();
                  Vector cellStoreVector=new Vector();
                  while(cellIter.hasNext()){
                          HSSFCell myCell = (HSSFCell) cellIter.next();
                          cellStoreVector.addElement(myCell);
                  }
                  cellVectorHolder.addElement(cellStoreVector);
          }
        }catch (Exception e){e.printStackTrace(); }
        return cellVectorHolder;
    }
        private static void saveToDatabase(Vector dataHolder) {
        String username="";
                String password="";
                for (int i=0;i<dataHolder.size(); i++){
                   Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
                        for (int j=0; j < cellStoreVector.size();j++){
                                HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j);
                                String st = myCell.toString();
                                 username=st.substring(0,1);
                                 password=st.substring(0);
                                                        }
                        try{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
        Statement stat=con.createStatement();
        int k=stat.executeUpdate("insert into login(username,password) value('"+username+"','"+password+"')");
        System.out.println("Data is inserted");
        stat.close();
        con.close();
        }
        catch(Exception e){}
        }
        }
      }

How do I insert this code in a button? 如何将此代码插入按钮?

generally you have to add Action listener to button: 通常,您必须将Action侦听器添加到按钮:

    Button button = new Button("click me");
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // call your logic here
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM