3public class ReadExcel extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// createExcel();
// readExcel();
writeExcel("mnt/sdcard/test.xls");
}
public void readExcel() {
try {
/**
* 后續(xù)考慮問題,比如Excel里面的圖片以及其他數(shù)據(jù)類型的讀取
**/
InputStream is = new FileInputStream("mnt/sdcard/test.xls");
Workbook book = Workbook
.getWorkbook(new File("mnt/sdcard/test.xls"));
book.getNumberOfSheets();
// 獲得第一個工作表對象
Sheet sheet = book.getSheet(0);
int Rows = sheet.getRows();
int Cols = sheet.getColumns();
System.out.println("當前工作表的名字:" + sheet.getName());
System.out.println("總行數(shù):" + Rows);
System.out.println("總列數(shù):" + Cols);
for (int i = 0; i < Cols; ++i) {
for (int j = 0; j < Rows; ++j) {
// getCell(Col,Row)獲得單元格的值
System.out
.print((sheet.getCell(i, j)).getContents() + " ");
}
System.out.print("
");
}
// 得到第一列第一行的單元格
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 {
// 創(chuàng)建或打開Excel文件
WritableWorkbook book = Workbook.createWorkbook(new File(
"mnt/sdcard/test.xls"));
// 生成名為“第一頁”的工作表,參數(shù)0表示這是第一頁
WritableSheet sheet1 = book.createSheet("第一頁", 0);
WritableSheet sheet2 = book.createSheet("第三頁", 2);
// 在Label對象的構造函數(shù)中,元格位置是第一列第一行(0,0)以及單元格內容為test
Label label = new Label(0, 0, "test");
// 將定義好的單元格添加到工作表中
sheet1.addCell(label);
/*
* 生成一個保存數(shù)字的單元格.必須使用Number的完整包路徑,否則有語法歧義
*/
jxl.write.Number number = new jxl.write.Number(1, 0, 555.12541);
sheet2.addCell(number);
// 寫入數(shù)據(jù)并關閉文件
book.write();
book.close();
} catch (Exception e) {
System.out.println(e);
}
}
/**
* jxl暫時不提供修改已經(jīng)存在的數(shù)據(jù)表,這里通過一個小辦法來達到這個目的,不適合大型數(shù)據(jù)更新! 這里是通過覆蓋原文件來更新的.
*
* @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.getSheet(0);
WritableCell wc = ws.getWritableCell(0, 0);
// 判斷單元格的類型,做出相應的轉換
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 {
// 創(chuàng)建工作薄
WritableWorkbook wwb = Workbook.createWorkbook(new File(filePath));
// 創(chuàng)建工作表
WritableSheet ws = wwb.createSheet("Sheet1", 0);
// 添加標簽文本
// 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(50), rnd
// .nextInt(50), rnd.nextInt(1000)));
// }
// 添加圖片(注意此處jxl暫時只支持png格式的圖片)
// 0,1分別代表x,y 2,5代表寬和高占的單元格數(shù)
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());
}
}
}
|