Sun River
          Topics about Java SE, Servlet/JSP, JDBC, MultiThread, UML, Design Pattern, CSS, JavaScript, Maven, JBoss, Tomcat, ...
          posts - 78,comments - 0,trackbacks - 0
          POI
          Example One:創(chuàng)建XLS

          群眾:笑死人了,這還要你教么,別丟人現(xiàn)眼了,用FileOutputStream就可以了,寫個(gè)文件,擴(kuò)展名為xls就可以了,哈哈,都懶得看你的,估計(jì)又是個(gè)水貨上來瞎喊,下去,喲貨~~

          小筆:無聊的人一邊去,懶得教你,都沒試過,還雞叫雞叫,&^%&**(()&%$#$#@#@


              HSSFWorkbook wb = new HSSFWorkbook();//構(gòu)建新的XLS文檔對象
              FileOutputStream fileOut = new FileOutputStream("workbook.xls");
              wb.write(fileOut);//注意,參數(shù)是文件輸出流對象
              fileOut.close();



          Example Two:創(chuàng)建Sheet

          群眾:有點(diǎn)責(zé)任心好吧,什么是Sheet?欺負(fù)我們啊?

          小筆:花300塊去參加Office 培訓(xùn)班去,我不負(fù)責(zé)教預(yù)科


             HSSFWorkbook wb = new HSSFWorkbook();//創(chuàng)建文檔對象
              HSSFSheet sheet1 = wb.createSheet("new sheet");//創(chuàng)建Sheet對象,參數(shù)為Sheet的標(biāo)題
              HSSFSheet sheet2 = wb.createSheet("second sheet");//同上,注意,同是wb對象,是一個(gè)XLS的兩個(gè)Sheet
              FileOutputStream fileOut = new FileOutputStream("workbook.xls");
              wb.write(fileOut);
              fileOut.close();


          Example Three:創(chuàng)建小表格,并為之填上數(shù)據(jù)

          群眾:什么是小表格啊?

          小筆:你用過Excel嗎?人家E哥天天都穿的是網(wǎng)格襯衫~~~~


                            HSSFWorkbook document = new HSSFWorkbook();//創(chuàng)建XLS文檔

          HSSFSheet salary = document.createSheet("薪水");//創(chuàng)建Sheet

          HSSFRow titleRow = salary.createRow(0);//創(chuàng)建本Sheet的第一行



          titleRow.createCell((short) 0).setCellValue("工號(hào)");//設(shè)置第一行第一列的值
          titleRow.createCell((short) 1).setCellValue("薪水");//......
          titleRow.createCell((short) 2).setCellValue("金額");//設(shè)置第一行第二列的值


          File filePath = new File(baseDir+"excel/example/");

          if(!filePath.exists())
          filePath.mkdirs();

          FileOutputStream fileSystem = new FileOutputStream(filePath.getAbsolutePath()+"/Three.xls");

          document.write(fileSystem);

          fileSystem.close();

           

          Example Four :帶自定義樣式的數(shù)據(jù)(eg:Date)

          群眾:Date!么搞錯(cuò),我昨天已經(jīng)插入成功了~

          小筆:是么?那我如果要你5/7/06 4:23這樣輸出你咋搞?

          群眾:無聊么?能寫進(jìn)去就行了!

          小筆:一邊涼快去~


                            HSSFWorkbook document = new HSSFWorkbook();

          HSSFSheet sheet = document.createSheet("日期格式");

          HSSFRow row = sheet.createRow(0);


          HSSFCell secondCell = row.createCell((short) 0);

          /**
           * 創(chuàng)建表格樣式對象
           */
          HSSFCellStyle style = document.createCellStyle();

          /**
           * 定義數(shù)據(jù)顯示格式
           */
          style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));

          /**
           * setter
           */
          secondCell.setCellValue(new Date());

          /**
           * 設(shè)置樣式
           */
          secondCell.setCellStyle(style);



          File filePath = new File(baseDir+"excel/example/");

          if(!filePath.exists())
          filePath.mkdirs();

          FileOutputStream fileSystem = new FileOutputStream(filePath.getAbsolutePath()+"/Four.xls");

          document.write(fileSystem);

          fileSystem.close();

          Example Five:讀取XLS文檔


          File filePath = new File(baseDir+"excel/example/");

          if(!filePath.exists())
          throw new Exception("沒有該文件");
          /**
           * 創(chuàng)建對XLS進(jìn)行讀取的流對象
           */
          POIFSFileSystem reader = new POIFSFileSystem(new FileInputStream(filePath.getAbsolutePath()+"/Three.xls"));
          /**
           * 從流對象中分離出文檔對象
           */
          HSSFWorkbook document = new HSSFWorkbook(reader);
          /**
           * 通過文檔對象獲取Sheet
           */
          HSSFSheet sheet = document.getSheetAt(0);
          /**
           * 通過Sheet獲取指定行對象
           */
          HSSFRow row = sheet.getRow(0);
          /**
           * 通過行、列定位Cell
           */
          HSSFCell cell = row.getCell((short) 0);

          /**
           * 輸出表格數(shù)據(jù)
           */
          log.info(cell.getStringCellValue());


          至此,使用POI操作Excel的介紹告一段落,POI是一個(gè)仍然在不斷改善的項(xiàng)目,有很多問題,比如說中文問題,大數(shù)據(jù)量內(nèi)存溢出問題等等,但這個(gè)Pure Java的庫的性能仍然是不容質(zhì)疑的,是居家旅行必備良品。

          而且開源軟件有那么一大點(diǎn)好處是,可以根據(jù)自己的需要自己去定制。如果大家有中文、性能等問題沒解決的,可以跟我索要我已經(jīng)改好的庫。當(dāng)然,你要自己看原代碼,我也不攔你。


          posted on 2007-08-09 12:37 Sun River 閱讀(304) 評論(0)  編輯  收藏 所屬分類: Struts
          主站蜘蛛池模板: 广平县| 报价| 连山| 维西| 桂东县| 肥东县| 罗甸县| 新和县| 霍林郭勒市| 左权县| 丰县| 房产| 莫力| 洛扎县| 彰化市| 酒泉市| 育儿| 拉萨市| 延川县| 醴陵市| 凤城市| 固原市| 沐川县| 虹口区| 运城市| 洛扎县| 金乡县| 湾仔区| 长顺县| 澜沧| 南通市| 广汉市| 宜兰市| 邯郸县| 平陆县| 清苑县| 盖州市| 定州市| 平凉市| 沁水县| 罗城|