我的空間,寫我所寫,禪我所藏

          與我一起遨游吧

           

          2007年6月28日

          POI 簡介及簡單應用

          由于項目需要從EXCEL文件中導入數據,所以這幾天上網收集了一下這方面的資料!

          于是找到了POI這個玩意,本來想用JXL的,但了解到它對處理數據量大的時候,效率不行!.于是選擇了POI!

          要求:JDK 1.4+POI開發包

          可以到 http://www.apache.org/dyn/closer.cgi/jakarta/poi/ 下載

          Jakarta POI

          Jakarta POI可以讓你使用Java來讀寫MS Excel ,Word文件  

          相關文檔

          官方網站: http://jakarta.apache.org/poi/ 
          http://www.matrix.org.cn/down_view.asp?id=14 


          www.matrix.org.cn上的東西一向很不錯!!



          創建Excel 文檔

            示例1將演示如何利用Jakarta POI API 創建Excel 文檔。 

            示例1程序如下:

          import org.apache.poi.hssf.usermodel.HSSFWorkbook;
          import org.apache.poi.hssf.usermodel.HSSFSheet;
          import org.apache.poi.hssf.usermodel.HSSFRow;
          import org.apache.poi.hssf.usermodel.HSSFCell;
          import java.io.FileOutputStream;
          public class CreateXL {

           /** Excel 文件要存放的位置,假定在D盤下*/

           public static String outputFile="D:\\test.xls";

           public static void main(String argv[]){

           try{

            // 創建新的Excel 工作簿

            HSSFWorkbook workbook = new HSSFWorkbook();

            // 在Excel工作簿中建一工作表,其名為缺省值
                // 如要新建一名為"效益指標"的工作表,其語句為:
                // HSSFSheet sheet = workbook.createSheet("效益指標");

            HSSFSheet sheet = workbook.createSheet();

            // 在索引0的位置創建行(最頂端的行)

            HSSFRow row = sheet.createRow((short)0);

            //在索引0的位置創建單元格(左上端)
            HSSFCell cell = row.createCell((short) 0);
            // 定義單元格為字符串類型
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            // 在單元格中輸入一些內容
            cell.setCellValue("增加值");
            // 新建一輸出文件流
            FileOutputStream fOut = new FileOutputStream(outputFile);
            // 把相應的Excel 工作簿存盤
            workbook.write(fOut);
            fOut.flush();
            // 操作結束,關閉文件
            fOut.close();
            System.out.println("文件生成...");

           }catch(Exception e) {
            System.out.println("已運行 xlCreate() : " + e );
           }
          }
          }
            

          讀取Excel文檔中的數據

            示例2將演示如何讀取Excel文檔中的數據。假定在D盤JTest目錄下有一個文件名為test1.xls的Excel文件。

          示例2程序如下:

          import org.apache.poi.hssf.usermodel.HSSFWorkbook;
          import org.apache.poi.hssf.usermodel.HSSFSheet;
          import org.apache.poi.hssf.usermodel.HSSFRow;
          import org.apache.poi.hssf.usermodel.HSSFCell;
          import java.io.FileInputStream;
          public class ReadXL {
           /** Excel文件的存放位置。注意是正斜線*/
           public static String fileToBeRead="D:\\test1.xls";
           public static void main(String argv[]){ 
           try{
            // 創建對Excel工作簿文件的引用
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
            // 創建對工作表的引用。
            // 本例是按名引用(讓我們假定那張表有著缺省名"Sheet1")
            HSSFSheet sheet = workbook.getSheet("Sheet1");
            // 也可用getSheetAt(int index)按索引引用,
            // 在Excel文檔中,第一張工作表的缺省索引是0,
            // 其語句為:HSSFSheet sheet = workbook.getSheetAt(0);
            // 讀取左上端單元
            HSSFRow row = sheet.getRow(0);
            HSSFCell cell = row.getCell((short)0);
            // 輸出單元內容,cell.getStringCellValue()就是取所在單元的值
            System.out.println("左上端單元是: " + cell.getStringCellValue()); 
           }catch(Exception e) {
            System.out.println("已運行xlRead() : " + e );
           }
          }
          }
            設置單元格格式

            在這里,我們將只介紹一些和格式設置有關的語句,我們假定workbook就是對一個工作簿的引用。在Java中,第一步要做的就是創建和設置字體和單元格的格式,然后再應用這些格式:

            1、創建字體,設置其為紅色、粗體:

          HSSFFont font = workbook.createFont();
          font.setColor(HSSFFont.COLOR_RED);
          font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            2、創建格式

          HSSFCellStyle cellStyle= workbook.createCellStyle();
          cellStyle.setFont(font);
            3、應用格式 

          HSSFCell cell = row.createCell((short) 0);
          cell.setCellStyle(cellStyle);
          cell.setCellType(HSSFCell.CELL_TYPE_STRING);
          cell.setCellValue("標題 "); 


          處理WORD文檔

          import java.io.*; 
          import org.textmining.text.extraction.WordExtractor;
          import org.apache.poi.hssf.usermodel.HSSFWorkbook;
          import org.apache.poi.hssf.usermodel.HSSFSheet;
          import org.apache.poi.hssf.usermodel.HSSFRow;
          import org.apache.poi.hssf.usermodel.HSSFCell;

          public class TestPoi { 
          public TestPoi() { 

          public static void main(String args[]) throws Exception 

          FileInputStream in = new FileInputStream ("D:\\a.doc"); 
          WordExtractor extractor = new WordExtractor(); 
          String str = extractor.extractText(in); 
          //System.out.println("the result length is"+str.length()); 
          System.out.println(str); 

          posted @ 2007-07-04 23:22 imcb 閱讀(665) | 評論 (0)編輯 收藏

          流程代碼前,代碼后的處理

          在審批流程中,加入處理前和處理后的數據處理,可以將審批流程外的業務處理或是流程額外程序出來分離出來。放在代碼前,代碼后處理。

          public WfActivity assignComplete(WfTranstion wfTrans,String procId, String activityId,
             String touserId,String memo,HttpServletRequest request)throws WfException {
            WfActivity wfAct = null;
            try {

                                  CodeFormula.parseBeforeCode(wfTrans.getConnection(),procId,activityId,CodeFormula.apply_code,request);
             CheckAgree.execute(wfTrans,procId, activityId, new WfUser(uname, pwd),流程自己處理的方法
               touserId,memo);
                               CodeFormula.parseAfterCode(wfTrans.getConnection(),procId,activityId,CodeFormula.apply_code,request);
            } catch (WfException e) {
             wfAct = null;
             throw e;
            }
            return wfAct;
           }

          posted @ 2007-06-28 19:38 imcb 閱讀(309) | 評論 (0)編輯 收藏

          java讀取文件夾下的所有文件夾和文件

          package com.borland.samples.welcome;

          import java.io.FileNotFoundException;
          import java.io.IOException;
          import java.io.File;

          public class ReadFile {
            public ReadFile() {}

            /**
             * 刪除某個文件夾下的所有文件夾和文件
             * @param delpath String
             * @throws FileNotFoundException
             * @throws IOException
             * @return boolean
             */
            public static boolean deletefile(String delpath) throws FileNotFoundException,
                IOException {
              try {

                File file = new File(delpath);
                if (!file.isDirectory()) {
                  System.out.println("1");
                  file.delete();
                }
                else if (file.isDirectory()) {
                  System.out.println("2");
                  String[] filelist = file.list();
                  for (int i = 0; i < filelist.length; i++) {
                    File delfile = new File(delpath + "\\" + filelist[i]);
                    if (!delfile.isDirectory()) {
                      System.out.println("path=" + delfile.getPath());
                      System.out.println("absolutepath=" + delfile.getAbsolutePath());
                      System.out.println("name=" + delfile.getName());
                      delfile.delete();
                      System.out.println("刪除文件成功");
                    }
                    else if (delfile.isDirectory()) {
                      deletefile(delpath + "\\" + filelist[i]);
                    }
                  }
                  file.delete();

                }

              }
              catch (FileNotFoundException e) {
                System.out.println("deletefile()   Exception:" + e.getMessage());
              }
              return true;
            }

            /**
             * 刪除某個文件夾下的所有文件夾和文件
             * @param delpath String
             * @throws FileNotFoundException
             * @throws IOException
             * @return boolean
             */
            public static boolean readfile(String filepath) throws FileNotFoundException,
                IOException {
              try {

                File file = new File(filepath);
                if (!file.isDirectory()) {
                  System.out.println("文件");
                  System.out.println("path=" + file.getPath());
                  System.out.println("absolutepath=" + file.getAbsolutePath());
                  System.out.println("name=" + file.getName());

                }
                else if (file.isDirectory()) {
                  System.out.println("文件夾");
                  String[] filelist = file.list();
                  for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    if (!readfile.isDirectory()) {
                      System.out.println("path=" + readfile.getPath());
                      System.out.println("absolutepath=" + readfile.getAbsolutePath());
                      System.out.println("name=" + readfile.getName());
                     
                    }
                    else if (readfile.isDirectory()) {
                      readfile(filepath + "\\" + filelist[i]);
                    }
                  }

                }

              }
              catch (FileNotFoundException e) {
                System.out.println("readfile()   Exception:" + e.getMessage());
              }
              return true;
            }

            public static void main(String[] args) {
              try {
                readfile("D:/file");
                //deletefile("D:/file");
              }
              catch (FileNotFoundException ex) {
              }
              catch (IOException ex) {
              }
              System.out.println("ok");
            }

          }

          posted @ 2007-06-28 15:46 imcb 閱讀(416) | 評論 (0)編輯 收藏

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 项城市| 兰坪| 绵竹市| 长宁县| 南投县| 永吉县| 兴安盟| 潜山县| 南通市| 莒南县| 常熟市| 东乡| 洪雅县| 鹰潭市| 莫力| 会泽县| 定日县| 突泉县| 洪江市| 阳泉市| 长垣县| 西宁市| 芮城县| 垦利县| 元氏县| 瓦房店市| 平果县| 子洲县| 洛川县| 宁城县| 禹州市| 双峰县| 内乡县| 武宣县| 襄樊市| 郯城县| 旅游| 台南县| 阳山县| 濮阳县| 图片|