Ytl's Java Blog

          厚積而薄發---每一天都是一個全新的開始
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Java處理Excel數據(1)

          Posted on 2009-04-12 13:57 ytl 閱讀(431) 評論(0)  編輯  收藏 所屬分類: Others

          Java處理Excel數據有很多方式,如Apache的POI或JXL等.

          我首先給出一個Excele數據的讀入的方式(使用的是jxl.jar包)

          package com.ccniit.readexcel;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.IOException;
          import java.io.InputStream;
          import java.util.ArrayList;
          import java.util.HashMap;
          import java.util.List;
          import java.util.Map;

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

          public class ExcelHander {

              
          public static String[] getColName(String desc) {
                  InputStream is 
          = null;
                  String[] colNames 
          = null;
                  
          try {
                      is 
          = new FileInputStream(new File(desc));
                      Workbook wb 
          = Workbook.getWorkbook(is);
                      Sheet sheet 
          = wb.getSheet(0);
                      
          int cols = sheet.getColumns();
                      colNames 
          = new String[cols];
                      
          for (int i = 0; i < cols; i++{
                          colNames[i] 
          = sheet.getCell(i, 0).getContents();
                          
          // System.out.println("列名: " + colNames[i]);
                      }

                      is.close();
                  }
           catch (FileNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           catch (BiffException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

                  
          return colNames;

              }


              
          public List<Map<String, Object>> readExcel(String desc) {
                  List
          <Map<String, Object>> datas = null;
                  
          try {
                      InputStream is 
          = new FileInputStream(new File(desc));
                      Workbook wb 
          = Workbook.getWorkbook(is);
                      
          if(wb == null){
                          
          return null;
                      }

                      Sheet sheet 
          = wb.getSheet(0);
                      
          int cols = sheet.getColumns();
                      
          int rows = sheet.getRows();
                      datas 
          = new ArrayList<Map<String, Object>>();
                      
          for (int i = 1; i < rows; i++{
                          Map
          <String, Object> data = new HashMap<String, Object>();
                          
          for (int j = 0; j < cols; j++{
                              String key 
          = sheet.getCell(j, 0).getContents();
                              
          // System.out.println("key:" + key);
                              Object value = (Object) sheet.getCell(j, i).getContents();
                              
          // System.out.println("value:" + value.toString());
                              data.put(key, value);
                          }

                          datas.add(data);
                      }

                      is.close();
                      wb.close();
                  }
           catch (FileNotFoundException e) {
                      e.printStackTrace();
                  }
           catch (BiffException e) {
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }

                  
          return datas;
              }


          }




          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 垣曲县| 柳江县| 民丰县| 南澳县| 新巴尔虎右旗| 政和县| 梧州市| 曲沃县| 上虞市| 民县| 沂水县| 白银市| 工布江达县| 黎城县| 扎兰屯市| 建平县| 张北县| 鄯善县| 阜新| 白水县| 连州市| 金门县| 霍林郭勒市| 内丘县| 尚义县| 松潘县| 五河县| 青海省| 玉山县| 瑞丽市| 孝义市| 筠连县| 唐海县| 宝丰县| 和田市| 鄂托克旗| 海阳市| 内黄县| 崇左市| 梅州市| 阿坝县|