posts - 431,  comments - 344,  trackbacks - 0

          一,JExcelAPI與Apache POI兩者對(duì)比
            1、JExcelAPI不適合商業(yè)應(yīng)用,它都是會(huì)莫名其妙的讀不出來數(shù)據(jù)。
            2、Apache POI是一純Java的實(shí)現(xiàn),不僅能讀微軟的Excel還可以讀Open Office的,新版HSSF3.0還添加了讀取Power Point的功能。總之是一種非常穩(wěn)定,強(qiáng)大的實(shí)現(xiàn)。推薦使用。


          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
          關(guān)鍵是要理解
           java讀取excel文件的順序是:
          Excel文件->工作表->行->單元格 對(duì)應(yīng)到POI中,為:workbook->sheet->row->cell
          注意:
           注意:  
                    1.sheet, 以0開始,以workbook.getNumberOfSheets()-1結(jié)束
             2.row, 以0開始(getFirstRowNum),以getLastRowNum結(jié)束
             3.cell, 以0開始(getFirstCellNum),以getLastCellNum結(jié)束,
                    結(jié)束的數(shù)目不知什么原因與顯示的長(zhǎng)度不同,可能會(huì)偏長(zhǎng)
           
           如何插圖片到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)表示圖片左上角在單元格中的坐標(biāo).
              * ((dx2,dy2)表示圖片右下角在單元格中的坐標(biāo).
              */
             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 周銳 閱讀(474) 評(píng)論(0)  編輯  收藏 所屬分類: ApacheJava
          主站蜘蛛池模板: 正宁县| 花莲市| 湖口县| 长子县| 盐池县| 嘉善县| 威信县| 丰城市| 蕲春县| 论坛| 望奎县| 淮北市| 章丘市| 涡阳县| 双辽市| 田东县| 蒙山县| 凯里市| 云浮市| 衢州市| 建平县| 磴口县| 岱山县| 广宗县| 铁岭县| 枣强县| 南澳县| 常宁市| 大城县| 若羌县| 应用必备| 治多县| 太仓市| 昌江| 嫩江县| 安康市| 广平县| 武川县| 天台县| 平度市| 安福县|