• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

Does Excel Have to be running to insert data into it from JAVA?

I can load an Excel spreadsheet, but it only works if I have the Spreadsheet open in Excel.  I would like this to load data at night from an Database table.   Any ideas or links anyone can point me to?
0
RedBane
Asked:
RedBane
  • 2
1 Solution
 
wgilsterCommented:
Use the ODBCJDBC driver. Here is an example

package classes;
import java.sql.*;

public class TestServer
{
  static
  {
      try  {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      }
      catch (Exception e) {
          System.err.println(e);
      }
  }

  public static void main(String args[]) {
      Connection conn=null;
      Statement stmt=null;
      String sql="";
      try {
          conn=DriverManager.getConnection("jdbc:odbc:excel","","");
          stmt=conn.createStatement();
          sql="INSERT INTO [Sheet1$] (FIELD_NAME1 FIELD_NAME2 FIELD_NAME3) VALUES (100,'String Value',200)";
          stmt.executeUpdate(sql);

      }
      catch (Exception e){
          System.err.println(e);
      }
      finally {
          try{
              stmt.close();
              conn.close();
              stmt=null;
              conn=null;
          }
          catch(Exception e){}
      }
  }
}
0
 
wgilsterCommented:
With my example above all you need to do is create is a DSN named "excel", and your ready to insert all the data you want.  If your importing data from a database just open the database the same way and import each resultset into Excel.
0
 
RedBaneAuthor Commented:
Thanks for the help.

Its working now with the below statement.  I had to set up a data range in Excel.  I can not get it to work by putting in the column names in the first part of the Insert statement.

sqlProd = "INSERT INTO \"ProductWS$\" VALUES ("+ProdID+",\'Desc\',\'TransType\')";

Thanks
Rob
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now