posts - 42,  comments - 84,  trackbacks - 0

读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();方法得到列数

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

单元格风格:

设置边框—
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好
posted on 2008-02-01 16:16 Dong 阅读(4974) 评论(2)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: