夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.IOException;
          import java.io.InputStream;
          import java.util.HashMap;

          import jxl.Cell;
          import jxl.Sheet;
          import jxl.Workbook;
          import jxl.read.biff.BiffException;

          import org.apache.struts.upload.FormFile;

          /**
           * 
          @author Hay Vanpull
           * 
           
          */
          public class JXLTOOL
          {
              
          private Workbook                workbook   = null// 工作部對象
                                                                 
              
          private HashMap<String, String> mapData    = null// data數據
                                                                 
              
          private Sheet                   sheet      = null// 工作表
                                                                 
              
          public int                      totalRows  = 0;   // 總行數
                                                                 
              
          public int                      totalCells = 0;   // 總列數
                                                                 
              
          /**
               * 以一個InputStream為參數的構造器
               * 
               * 
          @param inputStream
               * 
          @throws IOException
               * 
          @throws BiffException
               
          */
              
          public JXLTOOL(InputStream inputStream) throws BiffException, IOException
              {
                  
          this.workbook = Workbook.getWorkbook(inputStream);
                  
          this.sheet = this.workbook.getSheet(0);
                  
          this.getRows();
                  
          this.getCells();
              }
              
              
          /**
               * 以一個Struts FormFile為參數的構造器
               * 
               * 
          @param file
               * 
          @throws IOException
               * 
          @throws FileNotFoundException
               * 
          @throws BiffException
               
          */
              
          public JXLTOOL(FormFile file) throws FileNotFoundException, IOException,
                      BiffException
              {
                  
          this(file.getInputStream());
              }
              
              
          /**
               * 以一個File為參數的構造器
               * 
               * 
          @param file
               * 
          @throws IOException
               * 
          @throws BiffException
               
          */
              
          public JXLTOOL(File file) throws BiffException, IOException
              {
                  
          this(new FileInputStream(file));
              }
              
              
          /**
               * 以一個文件路徑path的構造器
               * 
               * 
          @param filePath
               * 
          @throws IOException
               * 
          @throws BiffException
               
          */
              
          public JXLTOOL(String filePath) throws BiffException, IOException
              {
                  
                  
          this(new File(filePath));
              }
              
              
          /**
               * 把所有數據放到一個map中去,key為行號加列號
               * 
               * 
          @return
               
          */
              
          public HashMap<String, String> getExcelDate()
              {
                  mapData 
          = new HashMap<String, String>();
                  
          for (int i = 0; i < this.totalRows; i++)
                  {
                      
          for (int j = 0; j < this.totalCells; j++)
                      {
                          
          this.mapData.put(i + "" + j, this.getData(j, i));
                      }
                  }
                  
          return this.mapData;
              }
              
              
          /**
               * 得到總行數
               
          */
              
          private void getRows()
              {
                  
          this.totalRows = sheet.getRows();
              }
              
              
          /**
               * 得到總列數
               
          */
              
          private void getCells()
              {
                  
          this.totalCells = this.sheet.getColumns();
              }
              
              
          /**
               * 得到數據
               * 
               * 
          @param cell
               * 
          @param row
               * 
          @return
               
          */
              
          private String getData(int cell, int row)
              {
                  Cell rs 
          = this.sheet.getCell(cell, row);
                  
          return rs.getContents();
              }
          }
          posted on 2008-08-13 17:32 HUIKK 閱讀(8096) 評論(0)  編輯  收藏 所屬分類: TOOL
          主站蜘蛛池模板: 双城市| 景泰县| 夏河县| 湘潭市| 汶上县| 宁都县| 日喀则市| 大新县| 济阳县| 保康县| 沙田区| 德兴市| 拉孜县| 航空| 永安市| 海原县| 沙湾县| 武平县| 昌都县| 乌海市| 丰顺县| 女性| 阳信县| 长春市| 达拉特旗| 灵石县| 平定县| 河津市| 柘城县| 绥滨县| 噶尔县| 政和县| 工布江达县| 东丰县| 蒙阴县| 容城县| 常德市| 南充市| 河间市| 定南县| 射阳县|