Solved

Java App - how export data to MS Excel

Posted on 2001-09-03
4
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

734 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