Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java App - how export data to MS Excel

Posted on 2001-09-03
4
Medium Priority
?
310 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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

715 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