This example describes how Android applies to reading Excel files. Share to everyone for your reference, specific as follows:
Readexcel.java file:
The public class Readexcel extends activity {/** called the ' when ' is the ' The activity ' is a./@Override public void O
Ncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Createexcel ();
Readexcel ();
Writeexcel ("Mnt/sdcard/test.xls"); public void Readexcel () {try {/** * Follow up questions, such as pictures in Excel and reading of other data types **/InputStream is = new Fil
Einputstream ("Mnt/sdcard/test.xls");
Workbook book = workbook. Getworkbook (New File ("Mnt/sdcard/test.xls"));
Book.getnumberofsheets ();
Get first Worksheet object Sheet Sheet = book.getsheet (0);
int Rows = Sheet.getrows ();
int Cols = Sheet.getcolumns ();
System.out.println ("The name of the current worksheet:" + sheet.getname ());
SYSTEM.OUT.PRINTLN ("Total number of rows:" + rows);
SYSTEM.OUT.PRINTLN ("Total number of columns:" + Cols); for (int i = 0; i < Cols ++i) {for (int j = 0; j < Rows; ++j) {//Getcell (Col,row) Get cell value System . Out. Print (Sheet.getcell (i, j)). getcontents () + "T");
} System.out.print ("\ n");
//Get the first row of cell cell cell1 = Sheet.getcell (0, 0);
String result = Cell1.getcontents ();
SYSTEM.OUT.PRINTLN (result);
Book.close ();
catch (Exception e) {System.out.println (e); } public void Createexcel () {try {//Create or open an Excel file Writableworkbook book = Workbook.createworkbook (New F
Ile ("Mnt/sdcard/test.xls"));
Generates a worksheet named "first page", and parameter 0 indicates that this is the first page writablesheet Sheet1 = book.createsheet ("First page", 0);
Writablesheet Sheet2 = Book.createsheet ("Third page", 2);
In the constructor of a Label object, the cell position is the first row of the first column (0,0) and the contents of the cell are test label label = new label (0, 0, "test");
Add a defined cell to the worksheet Sheet1.addcell (label); * * Generates a cell that holds numbers. You must use the full package path of number, otherwise there is a syntactic ambiguity/jxl.write.Number number = new Jxl.write.Number (1, 0, 555.12541
);
Sheet2.addcell (number);
Writes the data and closes the file Book.write ();
Book.close ();
catch (Exception e) {System.out.println (e);
}
}
/** * JXL temporarily does not provide to modify the existing data table, here through a small way to achieve this goal, not suitable for large data updates!
This is updated by overwriting the original file. * * @param filePath */public void Updateexcel (String filePath) {try {workbook RWB = Workbook.getworkbook
(New File (FilePath)); Writableworkbook WWB = Workbook.createworkbook (New File ("D:/new.xls"), RWB);/copy writablesheet ws = Wwb.gets
Heet (0);
Writablecell WC = Ws.getwritablecell (0, 0);
To determine the type of cell, make the corresponding conversion label label = (Label) WC;
Label.setstring ("The value has been modified");
Wwb.write ();
Wwb.close ();
Rwb.close ();
catch (Exception e) {e.printstacktrace (); } public static void Writeexcel (String filePath) {try {//create work thin writableworkbook WWB = Workbook.createw
Orkbook (New File (FilePath));
create worksheet Writablesheet ws = Wwb.createsheet ("Sheet1", 0);
Add label text//Random rnd = new Random (new Date ()). GetTime ());
int fornumber = rnd.nextint (100);
Label label = new label (0, 0, "test"); for (int i = 0; I < 3;
i++) {//Ws.addcell (label);
Ws.addcell (New Jxl.write.Number (Rnd.nextint), Rnd//. Nextint (1000)); ///Add picture (note here JXL only supports pictures in PNG format)//0,1 respectively represents X,y 2, 5 for width and Gao Jian cell number ws.addimage (new Writableimage (5, 5, 2, 5, new
File ("Mnt/sdcard/nb.png")));
Wwb.write ();
Wwb.close ();
catch (Exception e) {System.out.println (e.tostring ());
}
}
}
jxl.7z Click here to download the site.
I hope this article will help you with the Android program.