JXL简单使用:
1.读Excel文件
Workbook workbook = null; try { workbook = Workbook.getWorkbook(new File(path)); } catch(Exception e) { System.out.println(e); } Sheet sheet = workbook.getSheet(0); Cell cell=sheet.getCell(i,j);//读第i列,第j行表格的值 System.out.println(cell.getContents()); 对于循环读出可以使用 sheet.getRows();方法得到行数 sheet.getColumns();方法得到列数
2.写Excel:
Workbook wb = Workbook.getWorkbook(new File(path)); WritableCellFormat wcf = new WritableCellFormat(); book = Workbook.createWorkbook(new File(path, wb); sheet = book.getSheet(0); jxl.write.Label name = new jxl.write.Label(i,j,"abc",wcf);//在第i列,第j行写入"abc"值,风格为wcf,如果写入的是数字则使用jxl.write.Number sheet.addCell(name); try { book.write(); book.close(); } catch(WriteException e){} catch(IOException e){}
3.单元格风格:
设置边框— WritableCellFormat wcf = new WritableCellFormat(); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setBorder(Border.RIGHT,BorderLineStyle.THIN); wcf.setBorder(Border.LEFT,BorderLineStyle.THIN); wcf.setBorder(Border.BOTTOM,BorderLineStyle.THIN); 合并单元格- sheet.mergeCells(0,1,0,2) 合并(0,1)、(0,2)两个单元格 设置字体- WritableFont wf = new WritableFont(WritableFont.ARIAL,12, WritableFont.BOLD, false); WritableCellFormat wcf = new WritableCellFormat(wf); 以及其它各种单元格样式,如设置背景颜色 都可以通过设置WritableCellFormat来设置
注:JXL组件对于公式的支持似乎比Apache的poi好