give me a java code snippet to connect Microsoft excel
through.... I am trying alot... plz help me.
Answers were Sorted based on User's Feedback
Answer / sushant
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String myDB = "jdbc:odbc:Driver={Microsoft Excel Driver
(*.xls)};DBQ=c:/java/scratch/jdbc_test.xls;"
+ "DriverID=22;READONLY=false";
Connection conn = DriverManager.getConnection(myDB, "", "");
| Is This Answer Correct ? | 19 Yes | 6 No |
Answer / m.gangadhar
hi create DSN for Microsoft Excel(.xsl) in
controlpanel/administrativetools/datasources
after that write normal code for driver class and getting
connection Object.
| Is This Answer Correct ? | 10 Yes | 4 No |
Answer / r.salimulla baba
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Connect extends HttpServlet
{
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con=null;
Statement st=null;
PreparedStatement pst=null;
ResultSet rs;
try
{
Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection
("jdbc:odbc:Std","","");
st=con.createStatement();
rs=st.executeQuery("select * from
[ExcelSheetName$]");
while(rs.next())
{
out.println(rs.getString
(1));
out.println(rs.getString
(2));
out.println(rs.getString
(3));
}
}
catch(Exception e)
{
System.out.println("Errr :"+e);
}
}
}
| Is This Answer Correct ? | 15 Yes | 9 No |
Answer / shailendra
Use Apache POI. They have provided all the api, for
creating, updating, writing.
http://poi.apache.org/hssf/quick-guide.html
| Is This Answer Correct ? | 10 Yes | 8 No |
Answer / n.gowtham raj
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
public class EmployeeReader1 {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection("jdbc:odbc:gowtham");
Statement st = con.createStatement
();
ResultSet rs = st.executeQuery
("select * from [Sheet1$]");
while(rs.next())
{
System.out.println
("USER ID:"+rs.getString("ID"));
}
rs.close();
st.close();
} catch (Exception ex) {
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
}
}
Step 1: goto control panel and select the Administrative
Tools and click the odbc driver.
Step 2: Then click the add button and select the microsoft
excel driver (".xls")
Step 3: then give the dsn name. (gowtham).
Step 4: then click the select workbook button and choose
the excel file then it work well.
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / amar
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Connect extends HttpServlet
{
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con=null;
Statement st=null;
PreparedStatement pst=null;
ResultSet rs;
try
{
Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection
("jdbc:odbc:Std","","");
st=con.createStatement();
rs=st.executeQuery("select * from
[ExcelSheetName$]");
while(rs.next())
{
out.println(rs.getString
(1));
out.println(rs.getString
(2));
out.println(rs.getString
(3));
}
}
catch(Exception e)
{
System.out.println("Errr :"+e);
}
}
}
| Is This Answer Correct ? | 4 Yes | 4 No |
Answer / vijay
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
public class EmployeeReader1 {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager
.getConnection
("jdbc:odbc:friends_exl");
Statement st = con.createStatement
();
ResultSet rs = st.executeQuery
("Select * from [Sheet1$]");
ResultSetMetaData rsmd =
rs.getMetaData();
int numberOfColumns =
rsmd.getColumnCount();
System.out.println("No of cols " +
numberOfColumns);
while (rs.next()) {
for (int i = 1; i <=
numberOfColumns; i++) {
if (i > 1)
System.out.print(", ");
String columnValue
= rs.getString(i);
System.out.print
(columnValue);
}
System.out.println("");
}
rs.close();
st.close();
} catch (Exception ex) {
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
}
}
| Is This Answer Correct ? | 2 Yes | 5 No |
What is db client?
What is Type-3 Driver and when this driver is used?
What is jdbc odbc in java?
how do we get the connection from connection pool
Does jdbc require oracle client?
How to set NULL values in JDBC PreparedStatement?
What is type 4 jdbc driver?
Briefly tell about the jdbc architecture.
Once I have the Java 2 SDK, Standard Edition, from Sun, what else do I need to connect to a database?
What is data source in java?
What is PreparedStatement?
What is jdbc and jdbc drivers?