Excel 분석 (1) - JXL

2441 단어

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) {

		}

	}

}

 
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>

좋은 웹페이지 즐겨찾기