Android解析Excel文档完整示例
MainActivity如下:
package cc.testexcel;import java.io.File;import jxl.Cell;import jxl.CellType;import jxl.DateCell;import jxl.NumberCell;import jxl.Sheet;import jxl.Workbook;import android.os.Bundle;import android.os.Environment;import android.app.Activity;/** * Demo描述: * 利用jxl.jar解析Excel文档 * * 注意事项: * 1 在读取每个单元格cell的时候,要留意其类型(CellType) * 这样就可以对不同类型区别对待 * 2 测试用Excel文档备份于assets文件夹下 * * 参考资料: * http://download.csdn.net/download/ljmin0204/4141034 * Thank you very much */public class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);parseExcel();}private void parseExcel() {try {Workbook workbook = null;try {File file=new File(Environment.getExternalStorageDirectory()+File.separator+"test.xls");workbook = Workbook.getWorkbook(file);} catch (Exception e) {throw new Exception("File not found");}//得到第一张表Sheet sheet = workbook.getSheet(0);//列数int columnCount = sheet.getColumns();//行数int rowCount = sheet.getRows();//单元格Cell cell = null;for (int everyRow = 0; everyRow < rowCount; everyRow++) {for (int everyColumn = 0; everyColumn < columnCount; everyColumn++) {cell = sheet.getCell(everyColumn, everyRow);if (cell.getType() == CellType.NUMBER) {System.out.println("数字="+ ((NumberCell) cell).getValue());} else if (cell.getType() == CellType.DATE) {System.out.println("时间="+ ((DateCell) cell).getDate());} else {System.out.println("everyColumn="+everyColumn+",everyRow="+everyRow+ ",cell.getContents()="+ cell.getContents());}}}//关闭workbook,防止内存泄露workbook.close();} catch (Exception e) {}}}
main.mxl如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试Excel的解析" android:layout_centerInParent="true" /></RelativeLayout>