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 are the different types of lockings in jdbc?
How to check jdbc driver version in websphere?
Hi friends, In JDBC 4 drivers are there among this which driver is best and why it is best.Which driver is most commonly used in web applications.
What is meant by dao?
What do you mean by batch processing in jdbc?
What are the measures to connect to the db using jdbc?
How does jdbc connect to database?
how to use CallableStatement? specially how to use their index given ..
What is database connection pooling? Advantages of using a connection pool?
How does a file store compare with a jdbc store?
How many rowset are available in jdbc?
What is jdbc dialect?