Solved

Java App - how export data to MS Excel

Posted on 2001-09-03
4
304 Views
Last Modified: 2012-06-27
I need the code to export data to an MS Excel file and open the file up from a Java app. The data would be contained in say for example a 2d array.
Does anyone have this?
0
Comment
Question by:jholden022500
  • 2
4 Comments
 
LVL 4

Expert Comment

by:Oliver_Dornauf
ID: 6450567
0
 
LVL 2

Accepted Solution

by:
vasan_sr earned 200 total points
ID: 6450873
u have to create a dsn for the excel.....and modify the following code as u r need.....

// package classes;
  import java.sql.*;

  public class excelWrite
  {
    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="";
        ResultSet rs=null;
          String srno="10";
 
        try {
            conn=DriverManager.getConnection("jdbc:odbc:excel","","");
            stmt=conn.createStatement();
               
            //sql="INSERT INTO [Sheet1$](srno,test,description,output,output1,pass) VALUES ('1','Hari','Venke','Vasan','Swami','Shalin')";
             int i =stmt.executeUpdate(sql);

               //excel reading
           //  sql="select  'Account Code','Account Period, yyyyppp','Transaction Date yyyymmdd','Record Type'  from [Sheet1$]";
            // sql="select * from [Sheet1$]";
             //System.out.println("1");
               //rs=stmt.executeQuery(sql);
               //System.out.println("2");

            //while(rs.next()){
               //        System.out.println(rs.getString("Dr/Cr"));
//                  " "+ rs.getString("Account Period, yyyyppp")
//                         +" "+ rs.getString("Transaction Date yyyymmdd")+
//                         " "+ rs.getString("Record Type"));
            //}
        }
        catch (Exception e){
            System.err.println(e);
        }
        finally {
            try{
                rs.close();
                stmt.close();
                conn.close();
                rs=null;
                stmt=null;
                conn=null;
            }
            catch(Exception e){}
        }
    }
  }
0
 
LVL 2

Expert Comment

by:vasan_sr
ID: 6466122
Thanks jholden
0
 

Expert Comment

by:anirbanr
ID: 8616069
Getting SQLExceptions like the following

java.sql.SQLException: [Microsoft][ODBC Excel Driver] The INSERT INTO statement contains the following unknown field name:

java.sql.SQLException: [Microsoft][ODBC Excel Driver] Number of query values and destination fields are not the same.


Is there any other configuration that needs to be done
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question