Ytl's Java Blog

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

          Java處理Excel數據(1)

          Posted on 2009-04-12 13:57 ytl 閱讀(427) 評論(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;
              }


          }




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


          網站導航:
           
          主站蜘蛛池模板: 阿合奇县| 云阳县| 翼城县| 太和县| 班戈县| 东莞市| 原平市| 永和县| 绥化市| 克什克腾旗| 阳朔县| 新建县| 宁晋县| 台东县| 宁化县| 大城县| 中山市| 桐乡市| 边坝县| 桦川县| 沂水县| 疏勒县| 普兰店市| 雷州市| 昭平县| 康马县| 顺义区| 微博| 丰县| 望江县| 炎陵县| 上思县| 万盛区| 郁南县| 固始县| 巴林右旗| 唐海县| 石泉县| 胶州市| 陵川县| 阳江市|