展开全部

package com.b2bjy.crm.util;

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.xssf.usermodel.XSSFWorkbook;

public class POIExcelUtil

{

/** *//** 总行数 */

private int totalRows = 0;

/** *//** 总列数 */

private int totalCells = 0;

/** *//** 构造方法 */

public POIExcelUtil()

{}

/** *//**

*

*

Description:[根据文件名读取excel文件]

*

Created by [Huyvanpull] [Jan 20, 2010]

*

Midified by [modifier] [modified time]

*

*

* @param fileName

* @return

* @throws Exception

*/

public ArrayList read(String fileName)

{

ArrayList dataLst = new ArrayList();

/** *//** 检查文件名是否为空e68a84e8a2ad3231313335323631343130323136353331333335336536或者是否是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;

}

/** *//**

*

*

Description:[根据流读取Excel文件]

*

Created by [Huyvanpull] [Jan 20, 2010]

*

Midified by [modifier] [modified time]

*

*

* @param inputStream

* @param isExcel2003

* @return

*/

public ArrayList read(InputStream inputStream,

boolean isExcel2003)

{

ArrayList dataLst = null;

try

{

/** *//** 根据版本选择创建Workbook的方式 */

Workbook wb = isExcel2003 ? new HSSFWorkbook(inputStream)

: new XSSFWorkbook(inputStream);

dataLst = read(wb);

}

catch (IOException e)

{

e.printStackTrace();

}

return dataLst;

}

/** *//**

*

*

Description:[得到总行数]

*

Created by [Huyvanpull] [Jan 20, 2010]

*

Midified by [modifier] [modified time]

*

*

* @return

*/

public int getTotalRows()

{

return totalRows;

}

/** *//**

*

*

Description:[得到总列数]

*

Created by [Huyvanpull] [Jan 20, 2010]

*

Midified by [modifier] [modified time]

*

*

* @return

*/

public int getTotalCells()

{

return totalCells;

}

/** *//**

*

*

Description:[读取数据]

*

Created by [Huyvanpull] [Jan 20, 2010]

*

Midified by [modifier] [modified time]

*

*

* @param wb

* @return

*/

private ArrayList read(Workbook wb)

{

ArrayList rowLst = new ArrayList();

/** *//** 得到第一个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;

}

/** *//** 循环Excel的列 */

for (short c = 0; c < 1; c++)

{

Cell cell = row.getCell(c);

String cellValue = "";

if (cell == null)

{

rowLst.add(cellValue);

continue;

}

/** *//** 处理数字型的,自动去零 */

if (Cell.CELL_TYPE_NUMERIC == cell.getCellType())

{

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.toString() + "";

}

rowLst.add(cellValue);

}

}

return rowLst;

}

/** *//**

*

*

Description:[正确地处理整数后自动加零的情况]

*

Created by [Huyvanpull] [Jan 20, 2010]

*

Midified by [modifier] [modified time]

*

*

* @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;

}

/** *//**

*

*

Description:[测试main方法]

*

Created by [Huyvanpull] [Jan 20, 2010]

*

Midified by [modifier] [modified time]

*

*

* @param args

* @throws Exception

*/

public String getPhones(){

ArrayList dataLst = new POIExcelUtil().read("D:\\b.xls");

StringBuffer rowData = new StringBuffer();

for (int i = 0; i < dataLst.size()-1; i++) {

rowData.append(dataLst.get(i)).append(",");

}

rowData.append(dataLst.get(dataLst.size()-1));

if (rowData.length() > 0)

{

System.out.println(rowData);

}

return rowData.toString();

}

public static void main(String[] args) throws Exception

{

System.out.println(new POIExcelUtil().getPhones());

}

}

本回答由电脑网络分类达人 董辉推荐

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

java读取excel2010文件_java如何读写excel2010相关推荐

  1. java读取本地文件_java 读取本地文件实例详解

    java 读取本地文件实例详解 用javax.xml.w3c解析 实例代码: package cn.com.xinli.monitor.utils; import org.w3c.dom.Docume ...

  2. java 读取css文件_java文件读取的两种方式

    JAVA中读取文件(二进制,字符)内容的几种方 JAVA中读取文件内容的方法有很多,比如按字节读取文件内容,按字符读取文件内容,按行读取文件内容,随机读取文件内容等方法,本文就以上方法的具体实现给出代 ...

  3. java 读取本地文件_java 读取本地文件实例详解

    java 读取本地文件实例详解 用javax.xml.w3c解析 实例代码: package cn.com.xinli.monitor.utils; import org.w3c.dom.Docume ...

  4. java读取properties文件_Java读取Properties文件的六种方法

    Java读取Properties文件有以下六种方法: 1.使用java.util.Properties类的load()方法String fileName="E:/system.propert ...

  5. java 读取1m文件_java的FileInputStream类读取文件

    package net.csdn.InputStream; import java.io.File; import java.io.FileInputStream; import java.io.Fi ...

  6. java读取json文件_Java 读取Json文件内容

    读取json文件为String类型: import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logge ...

  7. java读取properties文件_java读取properties文件的几种方法

    一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取 1 Properties p=newProperties() ...

  8. java 读取.sql文件_java解析sql文件

    packagecom.athena.ckx.util;importjava.io.FileInputStream;importjava.io.InputStream;importjava.sql.Co ...

  9. java 读取ppt文件_java使用poi读取ppt文件和poi读取excel、word示例

    Apache的POI项目可以用来处理MS Office文档,codeplex上还有一个它的.net版本.POI项目可创建和维护操作各种基于OOXML和OLE2文件格式的Java API.大多数MS O ...

最新文章

  1. Android.mk 配置参数
  2. Delphi资源文件的应用(转)
  3. 全球及中国健康保险市场运作模式与需求潜力预测报告2022版
  4. python 排序统计滤波器_马尔可夫链+贝叶斯滤波器的Python展示
  5. Mobvista首席架构师蔡超:工作感悟之失败与成功,我的8点总结
  6. 安装nrm,报错request@2.88.2: request has been deprecated, see https://github.com/request/request/issu
  7. mysql 性能剖析_MySQL服务器性能剖析(一)
  8. iphone之使用讯飞语音sdk实现语音识别功能
  9. 接口测试加密解密以及接口签名sign原理
  10. Windows Dos命令大全
  11. 软件测试 查看网页打开时间,网页响应时间101(一)
  12. 我的世界java1.16.1怎么弄材质包_我的世界1.16.1透明矿石材质包
  13. 做微信公众号淘宝客返利系统必须要知道3件事儿
  14. 笔记本快捷键(fl980)
  15. BCDEDIT - 启动配置数据存储编辑器
  16. 卡通鱼(cartoon fish)
  17. 一级消防工程师【技术实务】(爆炸)
  18. 感恩节(Thanksgiving Day)与感恩(组图)
  19. 如何把带图片html转为doc,教您一招:如何将图片里面的文字转成word格式并能编辑...
  20. Spring Security详细讲解(JWT+SpringSecurity登入案例)

热门文章

  1. 110余家企业聚集智博会 专家表示智慧城市需以打破数据堡垒为前提
  2. 如何给网红变现?Instagram正在尝试一种全新的方式
  3. SQLServer镜像状态异常排查
  4. RH413-测试文件的a属性
  5. 基础才是重中之重~.net中的显式事务与隐式事务
  6. oracle中备份package源码
  7. sklearn模型评选择与评估
  8. [IDEA 配置MYSQL数据库连接]
  9. 【收藏】CDH6.2.1 配置Flink1.10(编译安装)
  10. kafka日志和数据分离