在Java中读取EXCEL文件 JExcelAPI与Apache POI两者对比

一,JExcelAPI与Apache POI两者对比
  1、JExcelAPI不适合商业应用,它都是会莫名其妙的读不出来数据。
  2、Apache POI是一纯Java的实现,不仅能读微软的Excel还可以读Open Office的,新版HSSF3.0还添加了读取Power Point的功能。总之是一种非常稳定,强大的实现。推荐使用。


Jexcelapi的使用参考:http://blog.csdn.net/yfhdsz/archive/2006/10/30/1356670.aspx

apache POI 参考:http://blog.csdn.net/yuansicau/archive/2006/08/31/1150046.aspx
关键是要理解
 java读取excel文件的顺序是:
Excel文件->工作表->行->单元格 对应到POI中,为:workbook->sheet->row->cell
注意:
 注意:  
          1.sheet, 以0开始,以workbook.getNumberOfSheets()-1结束
   2.row, 以0开始(getFirstRowNum),以getLastRowNum结束
   3.cell, 以0开始(getFirstCellNum),以getLastCellNum结束,
          结束的数目不知什么原因与显示的长度不同,可能会偏长
 
 如何插图片到Excel中
 public static void createPicture() {
  FileOutputStream fileOut = null;
  BufferedImage bufferImg = null;
  BufferedImage bufferImg1 = null;
  try {
   ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
   ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
   bufferImg = ImageIO.read(new File("C:/Documents and Settings/All Users/Documents/My Pictures/示例图片/Sunset.jpg"));
   bufferImg1 = ImageIO.read(new File("C:/Documents and Settings/All Users/Documents/My Pictures/示例图片/Water lilies.jpg"));
   ImageIO.write(bufferImg, "jpeg", byteArrayOut);
   ImageIO.write(bufferImg1, "jpeg", byteArrayOut1);

   HSSFWorkbook wb = new HSSFWorkbook();
   HSSFSheet sheet1 = wb.createSheet("new sheet");
   // HSSFRow row = sheet1.createRow(2);
   HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
   HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 512, 255,
     (short) 0, 0, (short) 10, 10);
   HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 512, 255,
     (short) 0, 15, (short) 10, 20);
   /**
    * public HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short
    * col1, int row1, short col2, int row2)
    * Creates a new client anchor
    * and sets the top-left and bottom-right coordinates of the anchor.
    *
    * Parameters: dx1 - the x coordinate within the first cell.
    * dy1 - the y coordinate within the first cell.
    * dx2 - the x coordinate within the second cell.
    * dy2 - the y coordinate within the second cell.
    * col1 - the column (0 based) of the first cell.
    * row1 - the row (0 based) of the first cell.
    * col2 - the column (0 based) of the second cell.
    * row2 - the row (0 based) of the second cell.
    * 注意:(col1,row1)表示图片左上角所在单元格
    * (col2,row2)表示图片右下角所在单元格,
    * 先是列,再行,顺序不要反了.
    * (dx1,dy1)表示图片左上角在单元格中的坐标.
    * ((dx2,dy2)表示图片右下角在单元格中的坐标.
    */
   patriarch.createPicture(anchor, wb.addPicture(byteArrayOut
     .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
   patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut1
     .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

   fileOut = new FileOutputStream("e:/study/workbook.xls");
   wb.write(fileOut);
   fileOut.close();

  } catch (IOException io) {
   io.printStackTrace();
   System.out.println("io erorr :  " + io.getMessage());
  } finally {
   if (fileOut != null)
    try {
     fileOut.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  }
 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值