基于Apache POI 从xlsx读出数据
2015-08-29 14:36
537 查看
【0】写在前面
0.1) these codes are from 基于Apache POI 的从xlsx读出数据0.2) this idea is from http://cwind.iteye.com/blog/2187670 , adding some comments for easy understanding proves to be my work.
package com.cwind.poi; import java.io.File; import java.io.FileInputStream; import java.util.Iterator; 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.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class SimpleDatasheetReader { public static void main(String[] args){ try { File excel = new File("E:/bench-cluster/temp-resource/RunningMan.xlsx"); FileInputStream fis = new FileInputStream(excel);
//创建工作簿
XSSFWorkbook book = new XSSFWorkbook(fis);
//创建工作簿下的第一页纸张
XSSFSheet sheet = book.getSheetAt(0);
// 基于行的迭代器
Iterator<Row> itr = sheet.iterator(); System.out.println(itr.hasNext());
// Iterating over Excel file in Java
while (itr.hasNext()) {
//得到行
Row row = itr.next(); System.out.println(row.getLastCellNum());
// Iterating over each column of Excel file
// 基于行创建单元格 迭代器
Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) {
//依次 获取某行的单元格
Cell cell = cellIterator.next(); switch (cell.getCellType()) {
//下面是依据不同数据类型 打印出单元格的 数据
case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t"); break; case Cell.CELL_TYPE_NUMERIC: if(DateUtil.isCellDateFormatted(cell)){ System.out.print(cell.getDateCellValue() + "\t"); }else{ System.out.print(cell.getNumericCellValue() + "\t"); } break; case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\t"); break; default: } } System.out.println(""); } }catch(Exception ex){ ex.printStackTrace(); } } }
相关文章推荐
- 基于Apache POI 从xlsx读出数据
- 基于Apache POI 向xlsx写入数据
- 基于Apache POI 向xlsx写入数据
- 配置mac自带Apache和php
- Apache的prefork模式和worker模式
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT) – 整理
- Centos6.6下SVN配合Apache
- Linux平台上apache服务器的搭建和应用
- 分布式日志收集系统Apache Flume的设计详细介绍
- Apache源码分析
- linux下的apache、mysql自启动
- Linux系统安装apache
- 实现类似于域名访www.a.com问本地的空间,而不是放在apache下的htocs文件夹下,或者是wamp下的www文件下 工具/原料 win7系统 wampserver环境 方
- apache配置域名访问本地空间
- Apache Pig 中文教程集合 (转)
- java.lang.ClassNotFoundException: org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver
- linux 下安装配置apache + ph
- java.lang.ClassNotFoundException: org.apache.axiom.om.OMNode
- windows 下 安装 apache2.4.16
- 分布式日志收集系统Apache Flume的设计介绍