夢幻之旅

          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 閱讀(8097) 評論(0)  編輯  收藏 所屬分類: TOOL
          主站蜘蛛池模板: 勐海县| 荣昌县| 德惠市| 贵定县| 阳东县| 仁布县| 海林市| 十堰市| 花莲县| 襄垣县| 汝州市| 西畴县| 山东| 佛坪县| 江源县| 盘山县| 黄大仙区| 青岛市| 黄山市| 斗六市| 琼中| 寿光市| 明星| 都江堰市| 商河县| 石嘴山市| 义马市| 张家港市| 陕西省| 中阳县| 湘潭县| 澜沧| 双城市| 芦溪县| 普安县| 扎赉特旗| 新安县| 枝江市| 广南县| 亚东县| 会理县|