Java POI读取Excel 2003/2007/2010例子

Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包。

Apache POI 代码例子地址:http://poi.apache.org/spreadsheet/quick-guide.html

需要的PIO jar 地址(Maven):

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
	<dependency>
	    <groupId>org.apache.poi</groupId>
	    <artifactId>poi</artifactId>
	    <version>3.16</version>
	</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->  
	<dependency>  
	    <groupId>org.apache.poi</groupId>  
	    <artifactId>poi-ooxml</artifactId>  
	    <version>3.15</version>  
	</dependency> 
<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
	<dependency>
	    <groupId>org.apache.xmlbeans</groupId>
	    <artifactId>xmlbeans</artifactId>
	    <version>2.6.0</version>
	</dependency>

本例子可以读取Microsoft Office Excel 2003/2007/2010,具体代码及注释如下:

读取“.xls”格式使用  import org.apache.poi.hssf.usermodel.*;包的内容,例如:HSSFWorkbook
读取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:XSSFWorkbook

读取两种格式使用    import org.apache.poi.ss.usermodel.*    包的内容,例如:Workbook

Workbook------工作表,通过WorkbookFactory的create(FileInputStream fis)方法获取,

Sheet------------表格,Workbook实例的getSheetAt(int num)方法获取,

Row--------------行,Sheet实例的getRow(int num)方法获取,

Cell--------------单元格,Row实例的getCell(int num)方法获取,

引入包如下:

import org.apache.poi.ss.usermodel.Cell;  
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;  
import org.apache.poi.ss.usermodel.DateUtil;  

/** 
 * 读取Excel测试,兼容 Excel 2003/2007/2010 
 */  
public String readExcel()  
{  
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");  
    try {  
        //同时支持Excel 2003、2007  
        File excelFile = new File("/home/zht/test.xls"); //创建文件对象  
        FileInputStream is = new FileInputStream(excelFile); //文件流  
        Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的  
        int sheetCount = workbook.getNumberOfSheets();  //Sheet的数量  
        //遍历每个Sheet  
        for (int s = 0; s < sheetCount; s++) {  
            Sheet sheet = workbook.getSheetAt(s);  
            int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数  
            //遍历每一行  
            for (int r = 0; r < rowCount; r++) {  
                Row row = sheet.getRow(r);  
                int cellCount = row.getPhysicalNumberOfCells(); //获取总列数  
                //遍历每一列  
                for (int c = 0; c < cellCount; c++) {  
                    Cell cell = row.getCell(c);  
                    int cellType = cell.getCellType();  
                    String cellValue = null;  
                    switch(cellType) {  
                        case Cell.CELL_TYPE_STRING: //文本  
                            cellValue = cell.getStringCellValue();  
                            break;  
                        case Cell.CELL_TYPE_NUMERIC: //数字、日期  
                            if(DateUtil.isCellDateFormatted(cell)) {  
                                cellValue = fmt.format(cell.getDateCellValue()); //日期型  
                            }  
                            else {  
                                cellValue = String.valueOf(cell.getNumericCellValue()); //数字  
                            }  
                            break;  
                        case Cell.CELL_TYPE_BOOLEAN: //布尔型  
                            cellValue = String.valueOf(cell.getBooleanCellValue());  
                            break;  
                        case Cell.CELL_TYPE_BLANK: //空白  
                            cellValue = cell.getStringCellValue();  
                            break;  
                        case Cell.CELL_TYPE_ERROR: //错误  
                            cellValue = "错误";  
                            break;  
                        case Cell.CELL_TYPE_FORMULA: //公式  
                            cellValue = "错误";  
                            break;  
                        default:  
                            cellValue = "错误";  
                    }  
                    System.out.print(cellValue + "    ");  
                }  
                System.out.println();  
            }  
        }  
  
    }  
    catch (Exception e) {  
        e.printStackTrace();  
    }  
  
    return Action.SUCCESS;  
}  



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值