posts - 431,  comments - 344,  trackbacks - 0

          一,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 on 2008-07-04 11:38 周銳 閱讀(473) 評論(0)  編輯  收藏 所屬分類: ApacheJava
          主站蜘蛛池模板: 闻喜县| 南溪县| 汶川县| 东兰县| 嵩明县| 彩票| 大冶市| 通榆县| 饶平县| 峨眉山市| 手游| 故城县| 西城区| 兴和县| 永泰县| 平江县| 息烽县| 荔波县| 怀安县| 思南县| 沂水县| 宣城市| 西丰县| 营口市| 时尚| 中方县| 东乌| 靖西县| 丰台区| 巫山县| 曲沃县| 巴中市| 北京市| 嵊泗县| 西畴县| 民勤县| 辛集市| 左云县| 南皮县| 迁安市| 本溪|