夢幻之旅

          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
          主站蜘蛛池模板: 山东省| 方山县| 刚察县| 壶关县| 阳新县| 玛纳斯县| 开平市| 太和县| 武穴市| 吴忠市| 太原市| 句容市| 建宁县| 滁州市| 赤峰市| 大港区| 安新县| 寻乌县| 镇平县| 泗阳县| 西盟| 华亭县| 旺苍县| 丰城市| 阿拉善盟| 浮梁县| 新巴尔虎左旗| 遂溪县| 镇宁| 白玉县| 棋牌| 上饶市| 丘北县| 丰台区| 嘉禾县| 江城| 五河县| 丹江口市| 甘孜| 安泽县| 沈阳市|