package com.my.login;

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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; /**
* Excel数据读取工具 Description: [支持Excell2003,Excell2007,自动格式化数值型数据,自动格式化日期型数据]
* <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>
*
*/
public class POIExcelUtil {
/** 总行数 */
private int totalRows = 0; /** 总列数 */
private int totalCells = 0; /** 构造方法 */
public POIExcelUtil() {
} /**
* 根据文件名读取excel文件
*
* @param fileName
* @return
*/
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;
} /**
* 根据流读取Excel文件
*
* @param inputStream
* @param isExcel2003
* @return
*/
public List<ArrayList<String>> read(InputStream inputStream,
boolean isExcel2003) {
List<ArrayList<String>> dataLst = null;
try {
/** 根据版本选择创建Workbook的方式 */
Workbook wb = isExcel2003 ? (Workbook) new HSSFWorkbook(inputStream)
: new XSSFWorkbook(inputStream);
dataLst = read(wb);
} catch (IOException e) {
e.printStackTrace();
}
return dataLst;
} /**
* 得到总行数
*
* @return
*/
public int getTotalRows() {
return totalRows;
} /**
* 得到总列数
*
* @return
*/
public int getTotalCells() {
return totalCells;
} /**
* 读取数据
*
* @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>();
/** 循环Excel的列 */
for (short c = 0; c < this.getTotalCells(); 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 = DateUtil
.get4yMdHms(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.toString() + "";
} rowLst.add(cellValue);
}
dataLst.add(rowLst);
}
return dataLst;
} /**
* 正确地处理整数后自动加零的情况
*
* @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;
} /**
* 测试main方法
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
List<ArrayList<String>> dataLst = new POIExcelUtil()
.read("f:/20140530091130.xlsx");
System.out.println("共有" + dataLst.size() + "行"); StringBuffer tab = new StringBuffer();
tab.append("<table border='1'>");
for (ArrayList<String> innerLst : dataLst) {
tab.append("<tr>"); for (String dataStr : innerLst) {
tab.append("<td>"); tab.append(dataStr); tab.append("</td>");
} tab.append("</tr>");
}
tab.append("</table>"); System.out.println(tab.toString());
}
}

转自 http://www.blogjava.net/hwpok/archive/2010/01/20/310263.html

【转】java 读取 excel 2003 或 excel 2007的更多相关文章

  1. Excel 2003 与 Excel 2007之间有什么不同?

    如果您使用Excel 2003已有数年,您可能会意识到使用更多最新版本的Excel(2007.2010.2013或Excel 2016)的人员或组织的数量正在增加.您甚至可能收到了自己的Excel工作 ...

  2. Java读取txt文件、excel文件的方法

    Java读取txt文件.excel文件的方法 1.读取txt文件 public static String getFileContent(String filePath,String charset) ...

  3. POI 读取word (word 2003 和 word 2007) (转)

    最近在给客户做系统的时候,用户提出需求,要能够导入 word 文件,现在 microsoft word 有好几个版本 97.2003.2007的,这三个版本存储数据的格式上都有相当大的差别,而现在 9 ...

  4. POI 读取word (word 2003 和 word 2007)(转,好用)

    POI 读取word (word 2003 和 word 2007)(转,好用) 转做的操作: 将作者文中失效的链接的正确链接放在失效链接的下面. 最近在给客户做系统的时候,用户提出需求,要能够导入 ...

  5. Java Struts2读取Excel 2003/2007/2010例子

    Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版. Apache POI ...

  6. java POI 解析excel 2003和2007 直接转为List<Map> 返回

    1.POI 官网下载jar包,3.5以上 2.项目导入jar包 3.参数:String数组--对应的excel列名对应的KEY,File  excel文件,sheetNumber ---excel的s ...

  7. 读取和导出下载 excel 2003,2007 资料

    protected void Page_Load(object sender, EventArgs e) { //直接在bin add referece search Microsoft.Office ...

  8. java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)

    使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...

  9. JAVA读取、写入Excel表格(含03版)

    引言 工作中可能会遇到对Excel读取和写入,如果我们自己手动写的话,会很麻烦,但是Apache中有poi工具类.poi工具类封装好了对于Excel读取和写入,我们需要用的时候,直接调用该方法就好了. ...

随机推荐

  1. [九省联考2018]一双木棋chess

    题解: 水题吧 首先很显然的是状压或者搜索 考虑一下能不能状压吧 这个东西一定是长成三角形的样子的 所以是可以状压的 相邻两位之间有几个0代表他们差几 这样最多会有2n 然后就可以转移了 由于之前对博 ...

  2. Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)

    A.B都是暴力搞一搞. A: #include<bits/stdc++.h> #define fi first #define se second #define mk make_pair ...

  3. 【Java】 剑指offer(2) 不修改数组找出重复的数字

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至少 ...

  4. POJ 2409 Let it Bead【Polya定理】(模板题)

    <题目链接> 题目大意:用k种颜色对n个珠子构成的环上色,旋转.翻转后相同的只算一种,求不等价的着色方案数. 解题分析: 对于这种等价计数问题,可以用polay定理来解决,本题是一道pol ...

  5. PHP如何支持CURL字符串证书传输

    背景 最近在对接微信支付的时候,需要在退款处用到证书,由于我们是SAAS平台,要支持多方多渠道支付,如果把所有证书文件保存在应用服务器会受到SLB的影响,会导致某台机器文件不同步而阻碍退款流程,但把文 ...

  6. System.currentTimeMillis() uptimeMillis elapsedRealtime 区别

    System.currentTimeMillis()  系统时间,也就是日期时间,可以被系统设置修改,然后值就会发生跳变. uptimeMillis 自开机后,经过的时间,不包括深度睡眠的时间 ela ...

  7. 喵哈哈村的魔法考试 Round 16 (Div.2) 比赛题解

    A 实际上我们for一遍就好. 坑点就是会爆int #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+7; ...

  8. Maven入门指南⑥:将项目发布到私服

    1 . 修改私服中仓库的部署策略 Release版本的项目应该发布到Releases仓库中,对应的,Snapshot版本应该发布到Snapshots仓库中.Maven根据pom.xml文件中版本号&l ...

  9. 网络编程(2)—UDP

    1.UDP协议: 将数据.源.目的封装成数据包,不需要建立连接 每个数据包大小在64K一下 因无需建立连接,所以是不可靠的 发送完毕,无需释放资源,速度快 2.UDP编程步骤: 发送端: 1.创建发送 ...

  10. Object类--equals方法

    equals方法 1.比较的是对象引用的是否指向同一块内存地址 public static void main(String[] args) { HuaWei huawei=new HuaWei(); ...