您的位置:首页 > 编程语言 > Java开发

好记性不如烂笔头8-JAVA读取EXCEL文件

2015-01-29 17:04 507 查看
使用poi读取EXCEL的内容
在很多的场合,需要读取EXCEL文件。简单的示例。
需要引入第三方jar包:poi_3.6.jar
package com.daily;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
 
importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 
/**
 *简单读取EXCEL中的内容
 *@author 范芳铭
 */
public class EasyExcelRead {
 
    privateList<Content> readXls(String excelFile) throws IOException {
        InputStreamis = new FileInputStream(new File(excelFile));
        HSSFWorkbookhssfWorkbook = new HSSFWorkbook(is);
        Contentcontent = null;
        List<Content>list = new ArrayList<Content>();
        //循环工作表Sheet
        for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){
            HSSFSheethssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if(hssfSheet == null) {
                continue;
            }
            //循环行Row
            for(int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                HSSFRowhssfRow = hssfSheet.getRow(rowNum);
                if(hssfRow == null) {
                    continue;
                }
                content= new Content();
                //0code;1姓名
                HSSFCellone = hssfRow.getCell(0);
                content.setCode(getValue(one));
 
                HSSFCelltwo = hssfRow.getCell(1);
                content.setName(getValue(two));
                list.add(content);
            }
        }
        returnlist;
    }
 
    /**
     * 得到Excel表中的值
     */
    privateString getValue(HSSFCell hssfCell) {
        if(hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
            //返回布尔类型的值
            returnString.valueOf(hssfCell.getBooleanCellValue());
        }else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
            //返回数值类型的值
            returnString.valueOf(hssfCell.getNumericCellValue());
        }else {
            //返回字符串类型的值
            returnString.valueOf(hssfCell.getStringCellValue());
        }
    }
 
    publicstatic void main(String[] args) throws Exception {
 
        longstart = System.currentTimeMillis();
        EasyExcelReadread = new EasyExcelRead();
        List<Content>Contents = read.readXls("d:/ffm83/easyExcel.xls");
        for(Contentcontent:Contents){
            System.out.println(content.toString());
        }
 
        longend = System.currentTimeMillis();
        System.out.println("生成excel文件耗时:"+ (end - start) + "(毫秒)");
    }
 
    //一个简单的保存简单信息的内部类
    publicclass Content {
        privateString code;
        privateString name;
 
        publicContent() {
        }
 
        publicContent(String code, String name) {
            this.code= code;
            this.name = name;
        }
 
        publicString getCode() {
            returncode;
        }
 
        publicvoid setCode(String code) {
            this.code= code;
        }
 
        publicString getName() {
            returnname;
        }
 
        publicvoid setName(String name) {
            this.name= name;
        }
       
        @Override
        publicString toString(){
            return"code:" + code + ",name:" + name;
        }
    }
 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java excel poi 读取EXCEL