码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-01-29 17:38:56      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:java   excel   poi   读取excel   

使用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;

        }

    }

 

}

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

标签:java   excel   poi   读取excel   

原文地址:http://blog.csdn.net/ffm83/article/details/43273513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!