- 浏览: 76282 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
cloudfile:
谢谢分享!
【转】C# 如何给Winform的button等控件添加快捷键 -
java-xp:
谢谢分享,受益匪浅
java 读取 excel 2003 或 excel 2007
java 读取 excel 2003 或 excel 2007
转载自:梦幻之旅 http://www.blogjava.net/hwpok/archive/2010/01/20/310263.html
package com.common.website.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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
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>();
/** *//** 循环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;
}
/** *//**
* <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:/Book1_shao.xls");
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());
}
}
}
}
转载自:梦幻之旅 http://www.blogjava.net/hwpok/archive/2010/01/20/310263.html
package com.common.website.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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
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>();
/** *//** 循环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;
}
/** *//**
* <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:/Book1_shao.xls");
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());
}
}
}
}
发表评论
-
JAVA资料
2010-03-02 13:49 0梦幻之旅Blog Log4J http://www.blogj ... -
【转】prepareCall()执行存储过程
2009-10-23 10:57 2440原文地址:http://hi.baidu.com/lookxj ... -
垃圾箱
2009-07-27 09:04 0http://cache.baidu.com/c?m=9d78 ... -
MyEclipse license
2009-07-13 20:48 3163Subscriber:Administrator Subsc ... -
AWT学习笔记(三)
2009-03-02 15:40 963Java语言为一些Listen ... -
AWT学习笔记(二)
2009-03-02 15:11 1304java为了实现跨平台的特性并且获得动态的布局效果,jav ... -
AWT学习笔记(一)
2009-03-02 13:14 1188魔兽领主 http://www.xiaoshu ... -
参考文档
2009-02-26 15:09 0DJFocus : http://www.djfocus.cn ...
相关推荐
java读取excel文件内容,可读取2003和2007版本
Java读取Excel内容 v Java读取Excel内容 Java读取Excel内容
JAVA读取 excelJAVA读取 excel
完美解决java读取excel内存溢出问题,希望可以帮到大家
java读取excel数据导入数据库源码 java读取excel数据导入数据库源码
Java读取Excel支持2003和2007。 读取普通字符、数字、日期,并对数字、日期进行相应处理; 可直接运行,含jar、说明文件、测试的excel文件。
java读取excel的工具类型,可同时支持excel2003和2007版本
Java 操作Excel poi读写excel,所需要的jar包 https://blog.csdn.net/u014646662/article/details/83217382
java读取excel,并且兼容2003和2007. 附带实例 可自定义表头,行读取开始位置、结束位置,列开始位置、结束位置
java 读取Excel文件中的内容 java 读取Excel文件中的内容
ExcelDemo_Excel导出_下载_POI读excel_JXL读写excel_java读写excel_列宽_读取合并或拆分单元格内容
java读写Excel,POI.JAR,Word内容读取
java读写excel包括utf8转码为可识别汉字gbk,jxl方式读取excel,生成一个新的excel
通过Java读取Excel文件中的数据时,需要自动判断当前Cell中的数据格式是否日期格式,如果是就要做特殊处理,以便读出数据的格式与预想的相符。
java 读取excel内容 创建excel 插入文字及图片 可以直接运行
java读取excel(兼容2003和2007)[定义].pdf
java读取excel表格的数据,并将其保存!已经调试过了,可以使用!
java读写excel文件,新手参考高手绕道。
java处理2003和2007Excel读写加密源码,有需要的可以参考代码,根据自己需求改造
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...