`
jammy.zhou
  • 浏览: 14973 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

转:POI读取Excel浅谈

阅读更多
1, 对于类似电话号码或手机一类的大数值读取问题
// 取值后会带一个E的问题
double cellValue = row.getCell(k).getNumericCellValue();
fieldValues[k] = new DecimalFormat("#").format(cellValue);


2, 对于数值型单元的纯数值和日期型的处理
case HSSFCell.CELL_TYPE_NUMERIC: // 数值型  
    if (HSSFDateUtil.isCellDateFormatted(cell)) {  
        //  如果是date类型则 ,获取该cell的date值  
        value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();  
    } else { // 纯数字  
        value = String.valueOf(cell.getNumericCellValue());  



转: POI读取Excel浅谈
先看代码,挨句解释:
一般遍历使用两种方式,1:得到总的行数和每行的列数,然后循环。2:使用迭代
先看第一种:
Java代码
package com.golden.test;  
 
import java.io.File;  
import java.io.FileInputStream;  
 
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;  
 
/** 
*  
* @author 崔素强 
*  
*/ 
public class PoiReadXls2 {  
    public static void main(String[] args) {  
        File f = new File("c:\\a.xls");  
        try {  
            FileInputStream is = new FileInputStream(f);  
            HSSFWorkbook wbs = new HSSFWorkbook(is);  
            HSSFSheet childSheet = wbs.getSheetAt(0);  
            // System.out.println(childSheet.getPhysicalNumberOfRows());  
            System.out.println("有行数" + childSheet.getLastRowNum());  
            for (int j = 0; j < childSheet.getLastRowNum(); j++) {  
                HSSFRow row = childSheet.getRow(j);  
                // System.out.println(row.getPhysicalNumberOfCells());  
                // System.out.println("有列数" + row.getLastCellNum());  
                if (null != row) {  
                    for (int k = 0; k < row.getLastCellNum(); k++) {  

                        HSSFCell cell = row.getCell(k);  
                        if (null != cell) {  
                            switch (cell.getCellType()) {  
                            case HSSFCell.CELL_TYPE_NUMERIC: // 数字  
                                System.out.print(cell.getNumericCellValue()  
                                        + "   ");  
                                break;  
                            case HSSFCell.CELL_TYPE_STRING: // 字符串  
                                System.out.print(cell.getStringCellValue()  
                                        + "   ");  
                                break;  
                            case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean  
                                System.out.println(cell.getBooleanCellValue()  
                                        + "   ");  
                                break;  
                            case HSSFCell.CELL_TYPE_FORMULA: // 公式  
                                System.out.print(cell.getCellFormula() + "   ");  
                                break;  
                            case HSSFCell.CELL_TYPE_BLANK: // 空值  
                                System.out.println(" ");  
                                break;  
                            case HSSFCell.CELL_TYPE_ERROR: // 故障  
                                System.out.println(" ");  
                                break;  
                            default:  
                                System.out.print("未知类型   ");  
                                break;  
                            }  
                        } else {  
                            System.out.print("-   ");  
                        }  
                    }  
                }  
                System.out.println();  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  


package com.golden.test;

import java.io.File;
import java.io.FileInputStream;

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;

/**
*
* @author 崔素强
*
*/
public class PoiReadXls2 {
        public static void main(String[] args) {
                File f = new File("c:\\a.xls");
                try {
                        FileInputStream is = new FileInputStream(f);
                        HSSFWorkbook wbs = new HSSFWorkbook(is);
                        HSSFSheet childSheet = wbs.getSheetAt(0);
                        // System.out.println(childSheet.getPhysicalNumberOfRows());
                        System.out.println("有行数" + childSheet.getLastRowNum());
                        for (int j = 0; j < childSheet.getLastRowNum(); j++) {
                                HSSFRow row = childSheet.getRow(j);
                                // System.out.println(row.getPhysicalNumberOfCells());
                                // System.out.println("有列数" + row.getLastCellNum());
                                if (null != row) {
                                        for (int k = 0; k < row.getLastCellNum(); k++) {
                                                HSSFCell cell = row.getCell(k);
                                                if (null != cell) {
                                                        switch (cell.getCellType()) {
                                                        case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                                                                System.out.print(cell.getNumericCellValue()
                                                                                + "   ");
                                                                break;
                                                        case HSSFCell.CELL_TYPE_STRING: // 字符串
                                                                System.out.print(cell.getStringCellValue()
                                                                                + "   ");
                                                                break;
                                                        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                                                                System.out.println(cell.getBooleanCellValue()
                                                                                + "   ");
                                                                break;
                                                        case HSSFCell.CELL_TYPE_FORMULA: // 公式
                                                                System.out.print(cell.getCellFormula() + "   ");
                                                                break;
                                                        case HSSFCell.CELL_TYPE_BLANK: // 空值
                                                                System.out.println(" ");
                                                                break;
                                                        case HSSFCell.CELL_TYPE_ERROR: // 故障
                                                                System.out.println(" ");
                                                                break;
                                                        default:
                                                                System.out.print("未知类型   ");
                                                                break;
                                                        }
                                                } else {
                                                        System.out.print("-   ");
                                                }
                                        }
                                }
                                System.out.println();
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}

得到Excel的文件然后读取,这个很简单。关键有两个地方,也许在网上会看到有的这样使用有的那样使用。
System.out.println("有行数" + childSheet.getLastRowNum());
System.out.println(childSheet.getPhysicalNumberOfRows());
System.out.println("有列数" + row.getLastCellNum());
System.out.println(row.getPhysicalNumberOfCells());
如果人都拷贝代码进行使用了,不知道有什么区别。太多的区别不知道,但是有一点我发现了,那就是如果中间各行或者隔列的话getPhysicalNumberOfRows和getPhysicalNumberOfCells就不能读取到所有的行和列了。
再者,一定要对单元格的格式进行判断switch (cell.getCellType()),不同的单元格格式使用不同的方法。最后加上为止类型,以防万一。
而且在数字类型里,又分为了纯数字和时间格式:
Java代码
case HSSFCell.CELL_TYPE_NUMERIC: // 数值型  
    if (HSSFDateUtil.isCellDateFormatted(cell)) {  
        //  如果是date类型则 ,获取该cell的date值  
        value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();  
    } else { // 纯数字  
        value = String.valueOf(cell.getNumericCellValue());  


case HSSFCell.CELL_TYPE_NUMERIC: // 数值型
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
                //  如果是date类型则 ,获取该cell的date值
                value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();
        } else { // 纯数字
                value = String.valueOf(cell.getNumericCellValue());
}
还有一种迭代的方法:
Java代码
package com.golden.test;  
 
import java.io.File;  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.Iterator;  
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;  
import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
 
/** 
*  
* @author 崔素强 
*  
*/ 
public class PoiReadXls {  
    @SuppressWarnings( { "unchecked", "deprecation" })  
    public static void main(String[] args) {  
        File f = new File("c:\\a.xls");  
        try {  
            InputStream input = new FileInputStream(f);  
            POIFSFileSystem fs = new POIFSFileSystem(input);  
            HSSFWorkbook wb = new HSSFWorkbook(fs);  
            HSSFSheet sheet = wb.getSheetAt(0);  
            Iterator rows = sheet.rowIterator();  
            while (rows.hasNext()) {  
                HSSFRow row = (HSSFRow) rows.next();  
                // System.out.print("行:" + row.getRowNum() + " ");  
                Iterator cells = row.cellIterator();  
                while (cells.hasNext()) {  
                    HSSFCell cell = (HSSFCell) cells.next();  
                    // System.out.println("列:" + cell.getCellNum());  
                    switch (cell.getCellType()) {  
                    case HSSFCell.CELL_TYPE_NUMERIC: // 数字  
                        System.out.print(cell.getNumericCellValue() + "   ");  
                        break;  
                    case HSSFCell.CELL_TYPE_STRING: // 字符串  
                        System.out.print(cell.getStringCellValue() + "   ");  
                        break;  
                    case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean  
                        System.out.println(cell.getBooleanCellValue() + "   ");  
                        break;  
                    case HSSFCell.CELL_TYPE_FORMULA: // 公式  
                        System.out.print(cell.getCellFormula() + "   ");  
                        break;  
                    case HSSFCell.CELL_TYPE_BLANK: // 空值  
                        System.out.println(" ");  
                        break;  
                    case HSSFCell.CELL_TYPE_ERROR: // 故障  
                        System.out.println(" ");  
                        break;  
                    default:  
                        System.out.print("未知类型   ");  
                        break;  
                    }  
                }  
                System.out.println();  
            }  
        } catch (IOException ex) {  
            ex.printStackTrace();  
        }  
    }  


package com.golden.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
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;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
*
* @author 崔素强
*
*/
public class PoiReadXls {
        @SuppressWarnings( { "unchecked", "deprecation" })
        public static void main(String[] args) {
                File f = new File("c:\\a.xls");
                try {
                        InputStream input = new FileInputStream(f);
                        POIFSFileSystem fs = new POIFSFileSystem(input);
                        HSSFWorkbook wb = new HSSFWorkbook(fs);
                        HSSFSheet sheet = wb.getSheetAt(0);
                        Iterator rows = sheet.rowIterator();
                        while (rows.hasNext()) {
                                HSSFRow row = (HSSFRow) rows.next();
                                // System.out.print("行:" + row.getRowNum() + " ");
                                Iterator cells = row.cellIterator();
                                while (cells.hasNext()) {
                                        HSSFCell cell = (HSSFCell) cells.next();
                                        // System.out.println("列:" + cell.getCellNum());
                                        switch (cell.getCellType()) {
                                        case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                                                System.out.print(cell.getNumericCellValue() + "   ");
                                                break;
                                        case HSSFCell.CELL_TYPE_STRING: // 字符串
                                                System.out.print(cell.getStringCellValue() + "   ");
                                                break;
                                        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                                                System.out.println(cell.getBooleanCellValue() + "   ");
                                                break;
                                        case HSSFCell.CELL_TYPE_FORMULA: // 公式
                                                System.out.print(cell.getCellFormula() + "   ");
                                                break;
                                        case HSSFCell.CELL_TYPE_BLANK: // 空值
                                                System.out.println(" ");
                                                break;
                                        case HSSFCell.CELL_TYPE_ERROR: // 故障
                                                System.out.println(" ");
                                                break;
                                        default:
                                                System.out.print("未知类型   ");
                                                break;
                                        }
                                }
                                System.out.println();
                        }
                } catch (IOException ex) {
                        ex.printStackTrace();
                }
        }
}

这种方法,如果数据的紧凑的,使用还是方便的,但是我发现,如果是空行或者是空列,他就会隔过去。具体的自己试试就知道了。
另外,也能看到这里得到Excel文件的方式是通过File,如果要引用到Struts2里,这是很简单的,因为Struts2上传时Action里定义的就是File或者File数组。
分享到:
评论

相关推荐

    利用POI读取excel写入到word

    本篇文章将详细讲解如何利用Apache POI库来读取Excel数据并将其写入到Word文档中。 首先,我们需要了解Apache POI的基本概念。POI提供了一组API,允许程序员在Java应用程序中创建、修改和显示这些文件。对于Excel...

    POI读取Excel带格式数据

    标题 "POI读取Excel带格式数据" 涉及到的是Apache POI库在Java中的使用,这个库允许开发者处理Microsoft Office格式的文件,包括Excel。Apache POI是一个开源项目,提供了API来读取、写入和修改Excel文档。在本场景...

    android5使用poi读取excel

    android5使用poi读取excel,android5使用poi读取excel,android5使用poi读取excel,android5使用poi读取excel

    POI读取excel的内容.zip

    本教程将详细讲解如何使用Apache POI库来读取Excel文件的内容。 首先,为了在Java项目中使用Apache POI,我们需要通过Maven进行依赖管理。在`pom.xml`文件中添加以下依赖: ```xml &lt;groupId&gt;org.apache.poi ...

    POI生成Excel POI操作Excel POI读取Excel POI类库

    2. **数据类型**:Excel中的数据类型包括字符串、数字、日期等,需要正确处理这些类型,防止数据丢失或转换错误。 3. **样式和格式**:POI允许设置单元格的样式,如字体、颜色、边框、对齐方式等,以增强Excel的...

    android POI 读取excel 精简jar

    在Android平台上,使用Apache POI库来读取Excel文件是一种常见的解决方案。Apache POI是一个流行的开源项目,它允许程序员创建、修改和显示Microsoft Office格式的文件,包括Excel(.xls和.xlsx)。在这个“android ...

    poi读取excel文件

    在Java编程环境中,Apache POI库是一个非常重要的工具,它允许开发者读取、写入以及修改Microsoft Office格式的文件,包括Excel的.xls和.xlsx格式。标题提到的"poi读取excel文件",指的是使用Apache POI库来处理...

    POI读取excel的例子

    POI读取excel的例子

    apache POI 读取 Excel

    apache poi 读取 Excel 的 jar 包 博文链接:https://wxinpeng.iteye.com/blog/231895

    poi读取excel并输出到jsp页面

    标题中的“poi读取excel并输出到jsp页面”是指使用Apache POI库来处理Microsoft Excel文件,并将数据在JavaServer Pages (JSP) 页面上显示的技术。Apache POI 是一个开源项目,它允许开发者创建、修改和显示MS ...

    poi 读取excel小实例

    实现了JAVA 窗口,读取EXCEL文件,用poi读取EXCEL内容只是一个小例子

    poi读取excel文件实例(兼容excel2007)

    在“poi读取excel文件实例”中,我们将讨论如何使用Apache POI API来读取和操作Excel 2007文件。以下是一些关键知识点: 1. **创建工作簿对象**:首先,你需要通过`WorkbookFactory`类的`create()`方法打开或创建一...

    POI读取EXCEL教程

    在Java编程环境中,如果你需要读取或写入Excel文件,POI 提供了强大的支持。本教程将深入讲解如何使用POI的HSSF组件来处理Excel 97-2003格式的文件(BIFF8格式)。 一、Excel基础 Excel 97 文件格式,也被称作BIFF8...

    POI读取Excel合并单元格并储存

    本文将深入探讨如何使用Java的Apache POI库来读取Excel文件,并处理其中合并单元格的数据,同时将这些数据存储到实体类中以供后续使用。 Apache POI是一个强大的库,允许程序员使用Java来创建、修改和显示Microsoft...

    poi读取Excel用到的jar包

    在Java编程环境中,Apache POI库是用于读取和写入Microsoft Office格式文件的强大工具,特别是Excel(.xlsx和.xls)文件。标题提到的“poi读取Excel用到的jar包”指的是在Java项目中使用POI库进行Excel操作所需引入...

    java 读取excel表格数据 poi

    java读取excel 表格数据。 public static void main(String[] args) { String[][] content=POIExcelUtils.read("E:\\1.xlsx"); for(int i=0;i;i++) { if(content[i]!=null){ for(int j=0;j[i].length;j...

    poi读取excel2007和2003兼容工具例子

    2. **Excel文件格式**:Excel 2003及其之前的版本使用二进制文件格式存储数据,文件扩展名为.xls。从Excel 2007开始,Microsoft引入了基于Open XML标准的新文件格式,扩展名为.xlsx。这两种格式在结构上存在显著差异...

    java POI读取excel文件数据

    在这个场景中,我们将详细探讨如何使用Java POI读取Excel文件中的数据,包括获取总行数、列数、单元格内容、合并单元格、行高、列宽以及图片等信息。 首先,确保在项目中已经引入了Apache POI的依赖库。如果你使用...

    poi 读取EXCEL所需要的JAR

    在Java环境中,如果你需要使用POI库来读取、写入或操作Excel文件,你需要确保你的项目中包含了正确的JAR文件。在这个场景中,你提到的几个JAR文件都是Apache POI的不同版本和组件: 1. **poi-3.8.jar**:这是Apache...

Global site tag (gtag.js) - Google Analytics