Solved

Java App - how export data to MS Excel

Posted on 2001-09-03
4
302 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json example 39 134
Java JRE greater than 1.6 5 51
Windows 10 IE Certificate Issue 10 42
how to install java on RHEL image on EC2 4 7
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

867 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

22 Experts available now in Live!

Get 1:1 Help Now