日期:2014-05-17  浏览次数:20774 次

java代码导入excel表格里的数据!

导入excel里面的数据,然后显示出来,求大神,求代码!要质量代码

------解决方案--------------------
需用用到poi的包,自己去下
Java code
package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class POI_Excel {
    //给定一个sheet,获取整个sheet的数据,每一行包装成一个map,key是行号,value是表格的值。然后把map加入list,这样整个sheet是一个list
    public static List<Map<Integer,String>> getExcelDataBySheet(HSSFSheet sheet) {
        List<Map<Integer,String>> table_list = new ArrayList<Map<Integer,String>>();
        int rows = sheet.getLastRowNum();
        int columns = sheet.getRow(0).getLastCellNum();
        //循环列数
        for(int i = 0; i <= rows; i++)
        {
            //得到第i行
            HSSFRow row = sheet.getRow(i);
            Map<Integer,String> map = new HashMap<Integer,String>();
            //循环第i行的数据,从0到columns
            for(int j = 0; j < columns; j++)
            {
                HSSFCell cell = null;
                try {
                    //获取cell,如果报异常,说明整个row是空的null,直接在catch里面捕获,并赋值为空
                    cell = row.getCell(j);
                } catch (NullPointerException e1) {
                    map.put(j, "");
                    continue;
                }
                
                //如果cell为空
                if(null == cell)
                {
                    map.put(j, "");
                    continue;
                }
                //获取cell的类型
                int type = cell.getCellType();
                //如果是空白
                if(type == HSSFCell.CELL_TYPE_BLANK)
                {
                    map.put(j, "");
                }
                //如果是数字型
                else if(type == HSSFCell.CELL_TYPE_NUMERIC)
                {
                    //如果cell里面包含E或者e,说明是科学计数法,要用特殊方法处理
                    if(String.valueOf(cell.getNumericCellValue()).matches(".*[E|e].*"))
                    {
                        DecimalFormat df = new DecimalFormat("#.#");
                        //指定最长的小数点位为10
                        df.setMaximumFractionDigits(10);
                        map.put(j, df.format((cell.getNumericCellValue())));
                    }
                    else
                    {
                        map.put(j, cell.getNumericCellValue()+"");
                    }
                    
                }
                //如果是字符串
                else if(type == HSSFCell.CELL_TYPE_STRING)
                {
                    map.put(j, cell.getStringCellValue());
                }
                //如果是公式型
                else if(type == HSSFCell.CELL_TYPE_FORMULA)
                {
                    String value;
                    try {
                        value = cell.getRichStringCellValue().getString();
                        map.put(j,value);
                    } catch (Exception e) {
                        value = cell.getNumericCellValue()+"";
                        map.put(j, value);
                    }
                }
                else
                {
                    map.put(j, "");
                }
            }
            table_list.add(map);
        }
        return table_list;
    }
    
    //给定一个sheet和行号,列号,获取其值
    public static String getExcelCellData(HSSFSheet sheet,in