使用java将数据插入excel

时间:2012-08-23 07:16:50

标签: java sql excel syntax odbc

我使用java swing建立一个独立的应用程序。和excel 2007 sheet作为存储我的数据的后端。 excel表中有15列。我只需要5列即可将数据插入其中。但它显示"语法错误"当我尝试插入15列。一次可插入的列数是否有限制?还是有冗长记录的其他语法?我是java的新手。好心地帮助我。在此先感谢:)我已经发布了编码部分

 try
                       {

                         String value1=t1.getText();

                         String value2=(String)c6.getSelectedItem();

                         String value3=t3.getText();

                         String value4=t4.getText();

                         String value5=t5.getText();

                         String value6 = (String) c4.getSelectedItem(); 

                         String value7=t7.getText();

                         String value8=t8.getText();

                         String value9 = (String) c1.getSelectedItem(); 

                         String value10 = (String) c2.getSelectedItem(); 

                         String value11 = (String) c5.getSelectedItem(); 

                         String value12 = (String) c3.getSelectedItem(); 

                         String value13=t13.getText();

                         String value14=t14.getText();

                         String value15=t15.getText();

           //connection to the database          

                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
               Connection con=DriverManager.getConnection("jdbc:odbc:testingApp");

          //Execute some sql and load the records into the resultset
                  Statement st=con.createStatement();
                String q = "insert into [sheet1$] (CRN,CR_NUM,SOW_N,PSM,LBG,TYPE,VALUE,REMARKS,STATUS,APPROVAL,PLATFORM,MODEL,START_DATE,END_DATE,CUM_VALUE) values ('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"','"+value6+"','"+value7+"','"+value8+"','"+value9+"','"+value10+"','"+value11+"','"+value12+"','"+value13+"','"+value14+"','"+value15+"')";
                 int ds = st.executeUpdate(q);
                 System.out.println("Inserted Sucessfully...!!!");

                  /* t1.setText(null);
                    c6.addItem(null);
                    t3.setText(null);
                    t4.setText(null);
                    t5.setText(null);
                    c4.addItem(null);
                    t7.setText(null);
                    t8.setText(null);
                    c1.addItem(null);
                    c2.addItem(null);
                    c5.addItem(null);
                    c3.addItem(null); 
                    t13.setText(null);
                    t14.setText(null);
                    t15.setText(null); */
                    }catch(Exception e)
                       {
                           System.out.println(e.getMessage());
                        }
                    }

} 

1 个答案:

答案 0 :(得分:0)

如果您想使用xls / xlsx格式作为sp00m sai,Apache POI是最佳解决方案,
如果你想拥有csv文件,那么只需创建文本文件,然后用逗号和带有“\ n”字符的行写入数据分隔列