记录一下平时的开发中使用Java操作Excel文件的方法,包括读取Excel文件和写Excel文件,这里用到了jxl.jar包,下面上代码:
/**获取表头的格式*/ public static WritableCellFormat getWritableHeaderCellFormat(){ WritableFont font = new WritableFont(WritableFont.createFont("楷体"), Global.HEAD_FONT_SIZE, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE); WritableCellFormat headerFormat = new WritableCellFormat(); try { headerFormat.setFont(font); headerFormat.setBackground(Colour.YELLOW); headerFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK); headerFormat.setAlignment(Alignment.CENTRE); } catch (WriteException e) { e.printStackTrace(); } return headerFormat; }以上代码是设置单元格格式的一些方法,包括设置单元格的文字大小,字体,背景色,对齐方式等。
try { WritableWorkbook wb = Workbook.createWorkbook(exportFile); WritableCellFormat headerFormat = getWritableHeaderCellFormat(); WritableSheet sheet = wb.createSheet("点名", 0); sheet.addCell(new Label(0, 0, "学号", headerFormat)); sheet.setColumnView(0, 2 * Global.CELL_META_WIDTH); sheet.addCell(new Label(1, 0, "姓名", headerFormat)); sheet.setColumnView(1, 2 * Global.CELL_META_WIDTH); sheet.addCell(new Label(2, 0, "性别", headerFormat)); sheet.setColumnView(2, 2 * Global.CELL_META_WIDTH); sheet.addCell(new Label(3, 0, "年龄", headerFormat)); sheet.setColumnView(3, 2 * Global.CELL_META_WIDTH); sheet.addCell(new Label(4, 0, "班级", headerFormat)); sheet.setColumnView(4, 2 * Global.CELL_META_WIDTH); sheet.addCell(new Label(5, 0, "电话", headerFormat)); sheet.setColumnView(5, 2 * Global.CELL_META_WIDTH); sheet.addCell(new Label(6, 0, "是否出勤", headerFormat)); sheet.setColumnView(6, 4 * Global.CELL_META_WIDTH); List<StudentBean> allList = getAllStudents(); WritableCellFormat contentFormat = getWritableContentCellFormat(); for(int i = 0; i < allList.size(); i++){ StudentBean bean = allList.get(i); String id = bean.getId(); sheet.addCell(new Label(0, i + 1, bean.getId(), contentFormat)); sheet.addCell(new Label(1, i + 1, bean.getName(), contentFormat)); sheet.addCell(new Label(2, i + 1, bean.getSex(), contentFormat)); sheet.addCell(new Label(3, i + 1, bean.getAge() + "", contentFormat)); sheet.addCell(new Label(4, i + 1, bean.getClassName(), contentFormat)); sheet.addCell(new Label(5, i + 1, bean.getPhone(), contentFormat)); if(idList.contains(id)){ //点名到了 sheet.addCell(new Label(6, i + 1, "是", contentFormat)); }else{ //点名未到 sheet.addCell(new Label(6, i + 1, "否", contentFormat)); } } wb.write(); wb.close(); callBack.onExportFinish(exportFile); } catch (Exception e) { e.printStackTrace(); callBack.onExportFailure(); }以上代码是往Excel文件中写入数据的一些方法,首先需要获取一个WritableWorkbook对象,然后创建sheet,再通过sheet的addCell方法添加单元格,上面的代码中,addCell方法里新建了Label对象,Label的创建需要指定行和列,这里需要注意的是Label的构造方法中,第一个参数是column,第二个参数是row,这里容易搞错。将单元格添加到sheet中之后,需要调用workbook的write方法,才能将数据写入Excel文件。
下面看读取Excel文件的一些方法:
try { File f = new File(filePath); Workbook book = Workbook.getWorkbook(f); Sheet sheet = book.getSheet(0); int rows = sheet.getRows();//行 int cols = sheet.getColumns();//列 List<StudentBean> list = new ArrayList<StudentBean>(); for(int i = 1; i < rows; i++){ StudentBean bean = new StudentBean(); for(int j = 0; j < cols; j++){ //getCell(i, j),i是列,j是行 String title = sheet.getCell(j, 0).getContents().trim(); String con = sheet.getCell(j, i).getContents().trim(); if("学号".equals(title)){ bean.setId(con); }else if("姓名".equals(title)){ bean.setName(con); }else if("性别".equals(title)){ bean.setSex(con); }else if("年龄".equals(title)){ bean.setAge(Integer.parseInt(con)); }else if("班级".equals(title)){ bean.setClassName(con); }else if("电话".equals(title)){ bean.setPhone(con); } } list.add(bean); } if(list.size() > 0){ //将数据加入数据库 db = dbHelper.getWritableDatabase(); String insertClassNameSql = "insert into classes(name) values (?)"; //添加到班级表 if(!isClassNameExist(className)){ db.execSQL(insertClassNameSql, new Object[]{className}); } String insertStuSql = "insert into stus values (?, ?, ?, ?, ?, ?)"; for(int i = 0; i < list.size(); i++){ StudentBean bean = list.get(i); if(!isIdExsit(bean.getId())){ db.execSQL(insertStuSql, new Object[]{bean.getId(), bean.getName(), bean.getSex(), bean.getAge(), bean.getClassName(), bean.getPhone()}); count++; } } handler.obtainMessage(MSG_IMPORT_COMPLETE).sendToTarget(); db.close(); } } catch (Exception e) { e.printStackTrace(); }首先还是获取workbook对象,然后获取sheet,再通过sheet获取Excel文件内容的行数和列数,然后用双重循环读取文件内容,获取某个单元格的内容使用的是sheet.getCell().getContents()方法。