Solved

Java App - how export data to MS Excel

Posted on 2001-09-03
4
301 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
Comment Utility
0
 
LVL 2

Accepted Solution

by:
vasan_sr earned 200 total points
Comment Utility
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
Comment Utility
Thanks jholden
0
 

Expert Comment

by:anirbanr
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now