Ytl's Java Blog

          厚積而薄發(fā)---每一天都是一個全新的開始

          Java處理Excel數(shù)據(jù)(1)

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

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

          我首先給出一個Excele數(shù)據(jù)的讀入的方式(使用的是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;
              }


          }




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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 行唐县| 布尔津县| 福海县| 潢川县| 涡阳县| 长春市| 海兴县| 定陶县| 西乡县| 宁强县| 乌兰浩特市| 石城县| 宁国市| 水城县| 丰顺县| 分宜县| 绥中县| 镇巴县| 陆川县| 芜湖县| 卢龙县| 江永县| 抚松县| 临海市| 贡山| 蒙山县| 阜新市| 马龙县| 年辖:市辖区| 前郭尔| 连江县| 英超| 明溪县| 商洛市| 河北省| 嘉兴市| 平原县| 玉山县| 江北区| 东源县| 宣武区|