java-读取-excel-2003-或-excel-2007.doc_第1页
java-读取-excel-2003-或-excel-2007.doc_第2页
java-读取-excel-2003-或-excel-2007.doc_第3页
java-读取-excel-2003-或-excel-2007.doc_第4页
java-读取-excel-2003-或-excel-2007.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

本文由感觉随风去贡献 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; /* */* * * Title:POI基础上的Excel数据读取工具 * Description: 支持Excell2003,Excell2007,自动格式化数值型数据,自动格式化日期型数据 * Copyright 2009 RoadWay Co., Ltd. * All right reserved. * Created by 惠万鹏 Jan 20, 2010 * Midified by modifier modified time * * 所需Jar包列表 * poi-3.6-20091214.jar * poi-contrib-3.6-20091214.jar * poi-examples-3.6-20091214.jar * poi-ooxml-3.6-20091214.jar * poi-ooxml-schemas-3.6-20091214.jar * poi-scratchpad-3.6-20091214.jar * xmlbeans-2.3.0.jar * * * version 1.0 */ 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 List read(String fileName) List dataLst = new ArrayList(); /* */* 检查文件名是否为空或者是否是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 List read(InputStream inputStream, boolean isExcel2003) List 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 List read(Workbook wb) List dataLst = 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; ArrayList rowLst = new ArrayList(); /* */* 循环Excel的列 */ for (short c = 0; c dataLst = new POIExcelUtil() .read(e:/Book1_shao.xls); for (ArrayList innerLst : dataLst) StringBuffer ro

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论