`

poi-3.7读取excel,支持excel 97~03 / excel 07

    博客分类:
  • POI
阅读更多
/**

* 需要如下jar包.
* poi-3.7-20101029.jar, poi-ooxml-3.7-20101029.jar,
* xmlbeans-2.3.0.jar, poi-ooxml-schemas-3.7-20101029.jar, dom4j-1.6.1.jar
*
* 需要将xbean-2.2.0.jar更新到xbean-2.3.0.jar,否则读取2007时会出现jar包冲突异常
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class POIExcelReader {   
    public POIExcelReader() {
    }

    /**
     * 读取excel,支持excel 97~03 / excel 07
     * @param fileName : 文件名
     */
    public void read(String fileName) {
       Workbook wb = null;
       File f = new File(fileName);
       FileInputStream is;
       try {
           is = new FileInputStream(f);
           wb = WorkbookFactory.create(is);
           readWB(wb);
           is.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
       e.printStackTrace();
       } catch (InvalidFormatException e) {
       e.printStackTrace();
       }     
    }
   
    /**  
     * 读取Workbook  
     * @param wb  
     * @throws Exception  
     */  
    private void readWB(Workbook wb){   
        try {   
        // 读取sheet0
            //for (int k = 0; k < wb.getNumberOfSheets(); k++) {      
                //sheet   
                //Sheet sheet = wb.getSheetAt(k);  
             Sheet sheet = wb.getSheetAt(0);
             int totalRow = sheet.getPhysicalNumberOfRows();
             System.out.println("totalRow=" + totalRow);
            
             readDatas(sheet); // 按行读取
             //-- test
             /*
             System.out.println("PhysicalNumberOfRows:"+sheet.getPhysicalNumberOfRows());
             System.out.println("FirstRowNum:"+sheet.getFirstRowNum());
             System.out.println("LastRowNum:"+sheet.getLastRowNum());
             */           
            //}   
        } catch (Exception e) {      
        e.printStackTrace();
        }   
    }

    /**
     * 读取excel数据
     * @param sheet
     */
    private void readDatas(Sheet sheet) {
    for (Row row : sheet) {
for (Cell cell : row) {
// System.out.println("列索引:"+cell.getColumnIndex());

switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print(cell.getDateCellValue());
} else {
System.out.print(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.print(cell.getCellFormula()); //取得公式
System.out.println("公式值:"+cell.getNumericCellValue()); //取得公式值
break;
default:
System.out.println("");
}
}
System.out.println("\n");
}
    }   
  
/**
* 获取合并单元格的值
* @param sheet
* @param row
* @param column
* @return
*/
public String getMergedRegionValue(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions();

for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress ca = sheet.getMergedRegion(i);
int firstColumn = ca.getFirstColumn();
int lastColumn = ca.getLastColumn();
int firstRow = ca.getFirstRow();
int lastRow = ca.getLastRow();

if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
Row fRow = sheet.getRow(firstRow);
Cell fCell = fRow.getCell(firstColumn);

return getCellValue(fCell);
}
}
}

return null;
}
   
    public static void main(String[] args) {
// POIExcelReader poie = new POIExcelReader();
// poie.read("c:\\users.xlsx");
   
}
分享到:
评论
1 楼 wmm890122 2012-04-19  
需要将xbean-2.2.0.jar更新到xbean-2.3.0.jar,否则读取2007时会出现jar包冲突异常 .

是直接引入的包为
xbean-2.3.0.jar就可以了吗?

相关推荐

    POI-3.7+3.8jar包

    - `poi-3.8-20120326.jar` 和 `poi-3.7-20101029.jar`:这是POI的核心库,提供对Excel、Word和PowerPoint的基本支持。 - `poi-scratchpad-3.8-20120326.jar` 和 `poi-scratchpad-3.7-20101029.jar`:这个模块包含...

    poi-bin-3.7-支持07excel

    在"poi-bin-3.7-支持07excel"这个压缩包中,我们聚焦于它对Excel 2007及以上版本(即XLSX格式)的支持。这个版本的POI库允许开发者通过Java代码来读取、写入和操作这些新格式的电子表格。 首先,Apache POI使用了...

    poi-ooxml-schemas-3.7-20101029.jar

    Java POI读取Office excel (2003,2007)及相关jar包 其中必要的poi-ooxml-schemas-3.7-20101029.jar

    poi-3.7.jar

    - 使用POI,可以轻松读取Excel工作表中的单元格数据,包括数值、字符串、日期等类型。 - 对Word文档进行解析,提取文本、段落、样式等信息。 - PowerPoint幻灯片的读取,包括文本、图片、动画、超链接等元素。 3...

    poi-ooxml-3.7.jar 包下载

    poi-ooxml-3.7.jar包下载,最新可用支持office各种文档,Java POI读取Office excel (2003,2007)及相关jar包 其中的poi-ooxml-3.7.jar

    poi-3.7所需jar包

    2. **Word处理**:除了Excel,POI还支持处理Microsoft Word文档。使用HWPF(Horrible Word Processor Format)API,开发者可以操作DOC格式的文件,而XWPF(XML Word Processor Format)API则用于处理DOCX格式。 3. ...

    poi-3.7源代码 最新版

    通过这些API,你可以创建、修改和读取Excel的工作表、单元格、公式等。 2. **处理Word文档**:使用 POI 的 HWPF 和 XWPF API,可以对 Microsoft Word 文档进行读取和写入操作,支持段落、表格、图片等元素的处理。 ...

    poi-src-3.7-20101029

    通过研究"poi-src-3.7-20101029"的源代码,开发者可以了解到如何使用Java实现与Excel的交互,如何处理不同类型的单元格,以及如何优化性能,这对于在Java环境中处理Excel任务的开发者来说,是一份宝贵的参考资料。

    poi-ooxml-3.7-20101029.jar

    Java POI读取Office excel (2003,2007)及相关jar包 其中的poi-ooxml-3.7-20101029.jar

    poi-bin-3.7-20101029.zip

    1. `poi-3.7-20101029.jar`:这是Apache POI的核心库,提供了对HSSF(Horrible Spreadsheet Format)和XSSF(XML Spreadsheet Format)的支持,用于读写Excel的97-2003格式(.xls)和2007及以上版本的.xlsx格式。...

    poi-3.7.rar

    2. poi-3.7-20101029.jar:这是Apache POI的核心库,提供了对HSSF(旧版的Excel格式)和XSSF(OOXML Excel格式)的支持,可以进行读写操作。 3. poi-scratchpad-3.7-20101029.jar:这部分代码包含了一些实验性的或者...

    struts2 POI-3.7 excel上传下载

    POI-3.7是POI项目的某个稳定版本,支持读写Excel 97-2003 (.xls)格式的文件。在项目中,我们需要添加对应的依赖,如Maven的pom.xml文件: ```xml &lt;groupId&gt;org.apache.poi&lt;/groupId&gt; &lt;artifactId&gt;poi&lt;/...

    Java 基于poi-3.7的Excel上传工具包(Util)

    在这个版本3.7中,开发者可以利用其API来创建、修改和读取Excel工作簿、工作表以及单元格。 **知识点一:Apache POI介绍** Apache POI提供了一系列的Java API,使得开发人员能够操作Microsoft Office格式的文件。在...

    Apache POI -3.7下载

    4. poi-ooxml-3.7-20101029.jar:这个库扩展了核心的POI库,添加了对Open XML格式的支持,使得开发者可以处理基于OOXML的文件。 5. poi-examples-3.7-20101029.jar:这个文件包含了一些示例代码,帮助开发者了解...

    poi-3.7-.jar

    1. `poi-3.7-20101029.jar`:这是Apache POI的核心库,主要包含了处理HSSF(Horizontally-Scattered Stream Format,用于旧版Excel 97-2003的BIFF格式)和XSSF(XML Spreadsheet Format,用于Excel 2007及以上版本的...

    poi-src-3.7正式版+源码

    9. **记录解析器(Record Parsers)**:Apache POI的源码中包含了对Excel文件中各种记录的解析逻辑,这些解析器是实现读取Excel功能的核心部分。 10. **版本兼容性**:3.7版本的Apache POI支持较旧的Excel格式,但...

    poi3.7和对应依赖jar包操作excel

    在给定的资源中,我们有五个相关的jar包,包括两个版本的poi-3.7.jar,两个不同部分的OOXML支持(poi-ooxml-schemas-3.7.jar和poi-ooxml-3.7.jar),以及Apache Commons Collections和IO库的不同版本(commons-...

    poi-3.7-20101029

    "poi-3.7-20101029" 标识的是该项目的一个特定版本,发布于2010年10月29日。在这个版本中,用户可以找到处理这些文件格式所需的API和库。 1. **Excel处理**: - HSSF (Horrible Spreadsheet Format):这是Apache ...

    java读取Excel转化TXT所需的poi-3.7所有jar包,亲测可用

    通过以上步骤,你就能使用Java和Apache POI 3.7版本的jar包读取Excel文件并将其转换成TXT格式。在实际应用中,你可能需要根据具体需求进行错误处理和优化,但这个基础示例应该能帮助你开始这个过程。

Global site tag (gtag.js) - Google Analytics