Android操作Excel表格

需要用到两个jar包,jxl.jar和poi-ooxml-3.9.jar

只读取Excel表格信息

Workbook workbook = Workbook.getWorkbook(file);//只读取表格不需要获取WritableWorkbook
WritableSheet sheet = workbook.getSheet(0);//获取第一张表
int rowsCount = sheet.getRows();//获取表中的行数
int cloumsCount = sheet.getColumns();//获取表中的列数
Cell cell = sheet.getCell(2, 3);//获取第二行第三列的cell对象
String contents = cell.getContents();//获取cell对象中的内容

创建Excel表格并插入数据信息

File file = new File(sdcardPath + File.separator + "Excel"+ File.separator + date+".xls");//获取excel文件对象
WorkbookSettings settings = new WorkbookSettings (); 
settings.setWriteAccess(null);
settings.setEncoding("UTF-8");
WritableWorkbook workbook = Workbook.createWorkbook(file,settings);//创建excel文件
WritableSheet sheet=workbook.createSheet("提取确认表", 0);//创建第一张表,表名是"提取确认表"
Label label1 = new Label(0, 0, "序号");//0行0列
Label label2 = new Label(1, 0,"行李编号1" );//0行1列
Label label3 = new Label(2, 0,"行李编号2" );//0行2列
Label label4 = new Label(3, 0,"比对结果" );//0行3列
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
//从内存中写入文件
workbook.write();
workbook.close();

读取已存在的Excel表格并插入数据信息

Workbook workbook = Workbook.getWorkbook(file);
WritableWorkbook writableWorkbook = Workbook.createWorkbook(file, workbook);//获取可写的writableWorkbook对象
WritableSheet sheet = writableWorkbook.getSheet(0);//获取表,然后就可以获取行数,然后在原表后面插入数据了

你可能感兴趣的:(Android操作Excel表格)