• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小折腾
博客园    首页    新随笔    联系   管理    订阅  订阅

在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();
    }
  }
 }

posted @ 2010-04-06 22:25  小折腾  阅读(1645)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3