import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/** *//**
* <ul>
* <li>Title:[POI基础上的Excel数据读取工具]</li>
* <li>Description: [支持Excell2003,Excell2007,自动格式化数值型数据,自动格式化日期型数据]</li>
* <li>Copyright 2009 RoadWay Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [惠万鹏] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
*
* <li>所需Jar包列表</li>
* <li>poi-3.6-20091214.jar</li>
* <li>poi-contrib-3.6-20091214.jar</li>
* <li>poi-examples-3.6-20091214.jar</li>
* <li>poi-ooxml-3.6-20091214.jar</li>
* <li>poi-ooxml-schemas-3.6-20091214.jar</li>
* <li>poi-scratchpad-3.6-20091214.jar</li>
* <li>xmlbeans-2.3.0.jar</li>
* <ul>
*
* @version 1.0
*/
public class POIExcelUtil
{
/** *//** 总行数 */
private int totalRows = 0;
/** *//** 总列数 */
private int totalCells = 0;
/** *//** 构造方法 */
public POIExcelUtil()
{}
/** *//**
* <ul>
* <li>Description:[根据文件名读取excel文件]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @param fileName
* @return
* @throws Exception
*/
public List<ArrayList<String>> read(String fileName)
{
List<ArrayList<String>> dataLst = new ArrayList<ArrayList<String>>();
/** *//** 检查文件名是否为空或者是否是Excel格式的文件 */
if (fileName == null || !fileName.matches("^.+\\.(?i)((xls)|(xlsx))$"))
{
return dataLst;
}
boolean isExcel2003 = true;
/** *//** 对文件的合法性进行验证 */
if (fileName.matches("^.+\\.(?i)(xlsx)$"))
{
isExcel2003 = false;
}
/** *//** 检查文件是否存在 */
File file = new File(fileName);
if (file == null || !file.exists())
{
return dataLst;
}
try
{
/** *//** 调用本类提供的根据流读取的方法 */
dataLst = read(new FileInputStream(file), isExcel2003);
}
catch (Exception ex)
{
ex.printStackTrace();
}
/** *//** 返回最后读取的结果 */
return dataLst;
}
/** *//**
* <ul>
* <li>Description:[根据流读取Excel文件]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @param inputStream
* @param isExcel2003
* @return
*/
public List<ArrayList<String>> read(InputStream inputStream,
boolean isExcel2003)
{
List<ArrayList<String>> dataLst = null;
try
{
/** *//** 根据版本选择创建Workbook的方式 */
Workbook wb = isExcel2003 ? new HSSFWorkbook(inputStream)
: new XSSFWorkbook(inputStream);
dataLst = read(wb);
}
catch (IOException e)
{
e.printStackTrace();
}
return dataLst;
}
/** *//**
* <ul>
* <li>Description:[得到总行数]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @return
*/
public int getTotalRows()
{
return totalRows;
}
/** *//**
* <ul>
* <li>Description:[得到总列数]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @return
*/
public int getTotalCells()
{
return totalCells;
}
/** *//**
* <ul>
* <li>Description:[读取数据]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @param wb
* @return
*/
private List<ArrayList<String>> read(Workbook wb)
{
List<ArrayList<String>> dataLst = new ArrayList<ArrayList<String>>();
/** *//** 得到第一个shell */
Sheet sheet = wb.getSheetAt(0);
this.totalRows = sheet.getPhysicalNumberOfRows();
if (this.totalRows >= 1 && sheet.getRow(0) != null)
{
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}
/** *//** 循环Excel的行 */
for (int r = 0; r < this.totalRows; r++)
{
Row row = sheet.getRow(r);
if (row == null)
{
continue;
}
ArrayList<String> rowLst = new ArrayList<String>();
int cells = row.getPhysicalNumberOfCells();
/** *//** 循环Excel的列 */
for (short c = 0; c < cells; c++)
{
Cell cell = row.getCell(c);
String cellValue = "";
if (cell == null)
{
rowLst.add(cellValue);
continue;
}
/** *//** 处理数字型的,自动去零 */
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType())
{
/** *//** 在excel里,日期也是数字,在此要进行判断 */
if (HSSFDateUtil.isCellDateFormatted(cell))
{
cellValue = getRightStr(cell.getDateCellValue() + "");
}
else
{
cellValue = getRightStr(cell.getNumericCellValue() + "");
}
}
/** *//** 处理字符串型 */
else if (Cell.CELL_TYPE_STRING == cell.getCellType())
{
cellValue = cell.getStringCellValue();
}
/** *//** 处理布尔型 */
else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType())
{
cellValue = cell.getBooleanCellValue() + "";
}
/** *//** 其它的,非以上几种数据类型 */
else
{
cellValue = cell.getBooleanCellValue() + "";
}
rowLst.add(cellValue);
}
dataLst.add(rowLst);
}
return dataLst;
}
/** *//**
* <ul>
* <li>Description:[正确地处理整数后自动加零的情况]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @param sNum
* @return
*/
private String getRightStr(String sNum)
{
DecimalFormat decimalFormat = new DecimalFormat("#.000000");
String resultStr = decimalFormat.format(new Double(sNum));
if (resultStr.matches("^[-+]?\\d+\\.[0]+$"))
{
resultStr = resultStr.substring(0, resultStr.indexOf("."));
}
return resultStr;
}
/** *//**
* <ul>
* <li>Description:[测试main方法]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @param args
* @throws Exception
*/
/* public static void main(String[] args) throws Exception
{
List<ArrayList<String>> dataLst = new POIExcelUtil()
.read("E:\\ysdata.xlsx");
for (ArrayList<String> innerLst : dataLst)
{
StringBuffer rowData = new StringBuffer();
for (String dataStr : innerLst)
{
rowData.append(",").append(dataStr);
}
if (rowData.length() > 0)
{
System.out.println(rowData.deleteCharAt(0).toString());
}
}
}*/
====================================================================================================================== public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream(new File("E:\\我参与的项目\\北京cpi\\HTA081218TJJGCJ\\1-系统开发实施\\(1)需求\\5需求分析\\导入导出\\3.2.1PDA后台数据导入\\ysdata.xlsx"));
//根据输入流创建Workbook对象
Workbook wb = WorkbookFactory.create(is);
//get到Sheet对象
Sheet sheet = wb.getSheetAt(0);
//这个必须用接口
for(Row row : sheet){
for(Cell cell : row){
//cell.getCellType是获得cell里面保存的值的type
//如Cell.CELL_TYPE_STRING
switch(cell.getCellType()){
case Cell.CELL_TYPE_BOOLEAN:
//得到Boolean对象的方法
System.out.print(cell.getBooleanCellValue()+" ");
break;
case Cell.CELL_TYPE_NUMERIC:
//先看是否是日期格式
if(DateUtil.isCellDateFormatted(cell)){
//读取日期格式
System.out.print(cell.getDateCellValue()+" ");
}else{
//读取数字
System.out.print(cell.getNumericCellValue()+" ");
}
注意:该类,会把,Excel中的,日期,也当成数字,所以在读取时,一定要判断是否是日期格式,如果是,则格式化成日期。我在项目中就碰到如此的,问题,几天都没有弄明白,很恼火,
break;
case Cell.CELL_TYPE_FORMULA:
//读取公式
System.out.print(cell.getCellFormula()+" ");
break;
case Cell.CELL_TYPE_STRING:
//读取String
System.out.print(cell.getRichStringCellValue().toString()+" ");
break;
}
}
System.out.println("");
}
}
分享到:
相关推荐
利用poi3.6 解析excel2003文件的程序代码.
收集了poi3.6跟poi3.7解析excel2007用到的包
poi3.6 兼容office 2003,2007
poi3.6官方帮助文档 poi3.6官方帮助文档 poi3.6官方帮助文档
excel poi3.6 excel poi3.6 excel poi3.6
poi-3.6-20091214.jar
poi 3.6 zuixinde API chm格式
本资源是从之前的资源综合整理出来的代码,之前的代码不全,需要下载两次,为了减少用户的下载量,减少用户的下载资源分,我已经将代码整合好了,运行类为:com.telesound.dataimport.excel.TestReadExcel...
利用POI读取excel写入到word 利用POI读取excel写入到word 利用POI读取excel写入到word,压缩包里包含了jar包
网上找了很久 poi3.6 excel2007操作 使用event api的例子
JAVA IO操作 POI读取excel的内容
该poi提供了对Excel表格的读取,是这的学习的一个文件。
POI-3.6 poi解析Excel(2003\2010)的jar集合,支持office2003\2010
使用poi读取excel内容 所需jar包
POI生成Excel POI读取Excel java操作Excel Servlet生成Excel web项目,包含含读取Excel与生成Excel方法
apache poi 读取 Excel 的 jar 包 博文链接:https://wxinpeng.iteye.com/blog/231895
NULL 博文链接:https://yxhcquedu.iteye.com/blog/828707
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...
读取Excel的poi包,,读取Excel的poi包,,读取Excel的poi包,读取Excel的poi包读
android5使用poi读取excel,android5使用poi读取excel,android5使用poi读取excel,android5使用poi读取excel