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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章