Ytl's Java Blog

厚积而薄发---每一天都是一个全新的开始
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Java处理Excel数据(1)

Posted on 2009-04-12 13:57 ytl 阅读(405) 评论(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;
    }


}




只有注册用户登录后才能发表评论。


网站导航: