Thursday, March 27, 2014

jxl.jar read excel exist to sqlite, logcat tips couldnot find jxl.workbook.getworkbook


posts by lhx200610930202 on 2013-09-25 11:36:22 Edit
package com.example.database;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.read.biff.BiffException;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
jxl.Workbook wb = null;
String s = "";
ArrayList innerAList = null;
ArrayList > outerAlist = new ArrayList > ();
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
ArrayList > outerAlist1 = new ArrayList > ();
String path = "D: \ \ MOI \ \ User.xls";
File file = new File (path);
outerAlist1 = ReadDataFromExcel (file);
WriteDataToSQLite (outerAlist1);
}
public static ArrayList > ReadDataFromExcel (File file) {
ArrayList innerAList = null;
ArrayList > outerAlist = new ArrayList > ();
jxl.Workbook wb = null;
try {
wb = jxl.Workbook.getWorkbook (file); / / create a workbook object wb, a reference to the Excel file
point of the object to be readjxl.Sheet sheet = wb.getSheet (0); / / create a workbook wb worksheet object (relationship workbook, worksheet, like a billing relationship with the accounting table and a page on the books)
int stRows = sheet.getRows (); / / get the number of the current worksheet of all non-empty row number;
int stColumns = sheet.getColumns ();
for (int i = 0; i innerAList = new ArrayList ();
for (int j = 0; j Cell cell = sheet.getCell (j, i); / / create a cell object to read from the storage cell sheet of (column j, i-line);
innerAList.add (cell.getContents ()); / / get a string that has been converted to a string from the current cell, see API
}
outerAlist.add (innerAList);
}
} Catch (FileNotFoundException fnf) {
fnf.printStackTrace ();
} Catch (IOException ioe) {
ioe.printStackTrace ();
} Catch (BiffException be) {
be.printStackTrace ();
} Finally {
wb.close ();
}
return outerAlist;
}

public static void WriteDataToSQLite (ArrayList > al) {
Connection conn = null;
Statement stm = null;
try {
Class cl = Class.forName ("org.sqlite.JDBC");
conn = DriverManager.getConnection ("jdbc: sqlite: d :/ MOI / MOI.db");
stm = conn.createStatement ();

stm.executeUpdate ("create table if not exists Usermsg (" +
"_id Integer primary key autoincrement," +
"Userno text," + "username text,");
PreparedStatement prs = conn.prepareStatement (
"Insert into Usermsg values ​​(,,???);");

/ / The data in the ArrayList, turn into the database
for (int i = 0; i for (int j = 0;. j {
prs.setString (. j +2, al.get (i) get (j));

}
}
prs.addBatch () ;/ / this will add to the batch command prs go


conn.setAutoCommit (false);
prs.executeBatch ();
conn.setAutoCommit (true) ;/ / these three methods can be cached in prs sql execute commands in batch, real execution, and before a set of data to be inserted into the database.
/ / For (outside);
} Catch (SQLException sqe) {
sqe.printStackTrace ();
} Catch (ClassNotFoundException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} Finally {
try {
conn.close ();
} Catch (SQLException e) {
e.printStackTrace ();
}
}

}
}

Mark: jxl.jar, sqlitejdbc-v056.jar I have added through build path-configure build path to the project
You master online to look, thank you very much.
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
android There String path = "D: \ \ MOI \ \ User.xls"???
Reply:
cited a floor guoyoulei520 reply:
android inside String path =??? "D: \ \ MOI \ \ User.xls"

Thank you for your answer, now I have to use other methods to achieve the excel database content into android, very grateful to you for your answer.

No comments:

Post a Comment