当前位置: 技术问答>java相关
java读excel出错
来源: 互联网 发布时间:2017-04-22
本文导语: 程序 ---------- import java.io.*; import java.sql.*; public class ReadExcel{ public static void main(String[] args){ Connection conn=null; Statement stmt=null; ResultSet rs=null; try{ Class.f...
程序
----------
import java.io.*;
import java.sql.*;
public class ReadExcel{
public static void main(String[] args){
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:myexcel");
stmt=conn.createStatement();
rs=stmt.executeQuery("Select * From [Sheet1$]");
ResultSetMetaData rsmd=rs.getMetaData();
int numberOfColumns=rsmd.getColumnCount();
while(rs.next()){
for(int i=0;i1)System.out.println(",");
//System.out.println(rs.getString(i));
System.out.println(rsmd.getColumnName(i));
}
}
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println(e);
}
}
}
---出错-----
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid descriptor index
----------
import java.io.*;
import java.sql.*;
public class ReadExcel{
public static void main(String[] args){
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:myexcel");
stmt=conn.createStatement();
rs=stmt.executeQuery("Select * From [Sheet1$]");
ResultSetMetaData rsmd=rs.getMetaData();
int numberOfColumns=rsmd.getColumnCount();
while(rs.next()){
for(int i=0;i1)System.out.println(",");
//System.out.println(rs.getString(i));
System.out.println(rsmd.getColumnName(i));
}
}
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println(e);
}
}
}
---出错-----
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid descriptor index
|
這個用例是完成.我在我的機器上用了通過,
jb7+jdk1.3.1
========================================================begin==
import java.sql.*;
import java.io.*;
import java.util.*;
import java.text.*;
public class Excel {
public Excel()
{
setDefaults();
}
private static void m(String pMessage)
{
System.out.println(pMessage);
}
private void setDefaults()
{
setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
setUrl("jdbc:odbc");
// ODBC data source named "TestExcel" defined from Control Panel
setDataSource("ExcelODBC");
setTableName("ExcelSheetName");
}
public void openDatabase()
{
String lConnectStr = getUrl()+":"+getDataSource();
try {
Class.forName(getDriver());
gConnection = DriverManager.getConnection(lConnectStr);
}
catch (Exception e) {
m("openDatabase(): "+e.getMessage());
}
}
private void closeDatabase()
{
try {
getConnection().close();
}
catch (Exception e) {
m("closeDatabase(): "+e.getMessage());
}
}
private void createTable()
{
m("createTable() begin");
Statement lStat = null;
try {
lStat = getConnection().createStatement();
lStat.execute("CREATE TABLE "+getTableName()+" ("
+" ID INTEGER"
+" ,NAME VARCHAR"
+")");
}
catch (Exception e) {
m("createTable(): "+e.getMessage());
}
m("createTable() end");
}
private void doInsert()
{
m("doInsert() begin");
Statement lStat = null;
try {
lStat = getConnection().createStatement();
lStat.executeUpdate("INSERT INTO "+getTableName()+ "(ID,NAME) VALUES(10,'KANGAROO')");
lStat.executeUpdate("INSERT INTO "+getTableName()+"(ID,NAME) VALUES (20,'KOALA')");
lStat.executeUpdate("INSERT INTO "+getTableName()+"(ID,NAME) VALUES (30,'PAULHOGAN')");
lStat.close();
}
catch (Exception e) {
m("doInsert(): "+e.getMessage());
}
m("doInsert() end");
}
private void doQuery()
{
m("doQuery() begin");
try {
Statement lStat = getConnection().createStatement();
ResultSet lRes = lStat.executeQuery( "SELECT * FROM "+getTableName());
ResultSetMetaData lMeta = lRes.getMetaData();
// print out the column headers separated by commas
for (int i = 1; i 1)
System.out.print(", ");
String lValue = lMeta.getColumnName(i);
System.out.print(lValue);
}
System.out.println("");
// print out the data separated by commas
while (lRes.next())
{
for (int i=1; i 1)
System.out.print(", ");
String lValue = lRes.getString(i);
System.out.print(lValue);
}
System.out.println("");
}
lRes.close();
lStat.close();
}
catch (Exception e) {
m("doQuery(): "+e.getMessage());
}
m("doQuery() end");
}
private void run()
{
openDatabase();
createTable();
doInsert();
doQuery();
closeDatabase();
}
public static void main(String args[])
{
m("main() begin");
Excel lExcel = new Excel();
lExcel.run();
m("main() end");
System.exit(0);
}
public void setTableName (String pValue)
{
gTableName = pValue;
}
public String getTableName()
{
return(gTableName);
}
public void setSql(String pValue)
{
gSql = pValue;
}
public String getSql()
{
return(gSql);
}
public Connection getConnection()
{
return(gConnection);
}
public String getDataSource()
{
return(gDataSource);
}
public void setDataSource(String pValue)
{
gDataSource = pValue;
}
public void setDriver(String pValue)
{
gDriver = pValue;
}
public void setUrl(/tech-qa-java/String pValue/index.html)
{
gUrl = pValue;
}
public String getDriver ()
{
return (gDriver);
}
public String getUrl ()
{
return (gUrl);
}
private Connection
gConnection = null;
private String gDataSource = null ,
gTableName = null ,
gSql = null ,
gDriver = null ,
gUrl = null ;
}
=====================================================end===
請大家試用.
jb7+jdk1.3.1
========================================================begin==
import java.sql.*;
import java.io.*;
import java.util.*;
import java.text.*;
public class Excel {
public Excel()
{
setDefaults();
}
private static void m(String pMessage)
{
System.out.println(pMessage);
}
private void setDefaults()
{
setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
setUrl("jdbc:odbc");
// ODBC data source named "TestExcel" defined from Control Panel
setDataSource("ExcelODBC");
setTableName("ExcelSheetName");
}
public void openDatabase()
{
String lConnectStr = getUrl()+":"+getDataSource();
try {
Class.forName(getDriver());
gConnection = DriverManager.getConnection(lConnectStr);
}
catch (Exception e) {
m("openDatabase(): "+e.getMessage());
}
}
private void closeDatabase()
{
try {
getConnection().close();
}
catch (Exception e) {
m("closeDatabase(): "+e.getMessage());
}
}
private void createTable()
{
m("createTable() begin");
Statement lStat = null;
try {
lStat = getConnection().createStatement();
lStat.execute("CREATE TABLE "+getTableName()+" ("
+" ID INTEGER"
+" ,NAME VARCHAR"
+")");
}
catch (Exception e) {
m("createTable(): "+e.getMessage());
}
m("createTable() end");
}
private void doInsert()
{
m("doInsert() begin");
Statement lStat = null;
try {
lStat = getConnection().createStatement();
lStat.executeUpdate("INSERT INTO "+getTableName()+ "(ID,NAME) VALUES(10,'KANGAROO')");
lStat.executeUpdate("INSERT INTO "+getTableName()+"(ID,NAME) VALUES (20,'KOALA')");
lStat.executeUpdate("INSERT INTO "+getTableName()+"(ID,NAME) VALUES (30,'PAULHOGAN')");
lStat.close();
}
catch (Exception e) {
m("doInsert(): "+e.getMessage());
}
m("doInsert() end");
}
private void doQuery()
{
m("doQuery() begin");
try {
Statement lStat = getConnection().createStatement();
ResultSet lRes = lStat.executeQuery( "SELECT * FROM "+getTableName());
ResultSetMetaData lMeta = lRes.getMetaData();
// print out the column headers separated by commas
for (int i = 1; i 1)
System.out.print(", ");
String lValue = lMeta.getColumnName(i);
System.out.print(lValue);
}
System.out.println("");
// print out the data separated by commas
while (lRes.next())
{
for (int i=1; i 1)
System.out.print(", ");
String lValue = lRes.getString(i);
System.out.print(lValue);
}
System.out.println("");
}
lRes.close();
lStat.close();
}
catch (Exception e) {
m("doQuery(): "+e.getMessage());
}
m("doQuery() end");
}
private void run()
{
openDatabase();
createTable();
doInsert();
doQuery();
closeDatabase();
}
public static void main(String args[])
{
m("main() begin");
Excel lExcel = new Excel();
lExcel.run();
m("main() end");
System.exit(0);
}
public void setTableName (String pValue)
{
gTableName = pValue;
}
public String getTableName()
{
return(gTableName);
}
public void setSql(String pValue)
{
gSql = pValue;
}
public String getSql()
{
return(gSql);
}
public Connection getConnection()
{
return(gConnection);
}
public String getDataSource()
{
return(gDataSource);
}
public void setDataSource(String pValue)
{
gDataSource = pValue;
}
public void setDriver(String pValue)
{
gDriver = pValue;
}
public void setUrl(/tech-qa-java/String pValue/index.html)
{
gUrl = pValue;
}
public String getDriver ()
{
return (gDriver);
}
public String getUrl ()
{
return (gUrl);
}
private Connection
gConnection = null;
private String gDataSource = null ,
gTableName = null ,
gSql = null ,
gDriver = null ,
gUrl = null ;
}
=====================================================end===
請大家試用.
|
把for(int i=0;i