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 { @Override public 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) { } } } </pre>main.mxl如下:<pre class="brush:xml; toolbar: true; auto-links: false;"> <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> </pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!