- 浏览: 7254578 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (1546)
- 企业中间件 (236)
- 企业应用面临的问题 (236)
- 小布Oracle学习笔记汇总 (36)
- Spring 开发应用 (54)
- IBatis开发应用 (16)
- Oracle基础学习 (23)
- struts2.0 (41)
- JVM&ClassLoader&GC (16)
- JQuery的开发应用 (17)
- WebService的开发应用 (21)
- Java&Socket (44)
- 开源组件的应用 (254)
- 常用Javascript的开发应用 (28)
- J2EE开发技术指南 (163)
- EJB3开发应用 (11)
- GIS&Mobile&MAP (36)
- SWT-GEF-RCP (52)
- 算法&数据结构 (6)
- Apache开源组件研究 (62)
- Hibernate 学习应用 (57)
- java并发编程 (59)
- MySQL&Mongodb&MS/SQL (15)
- Oracle数据库实验室 (55)
- 搜索引擎的开发应用 (34)
- 软件工程师笔试经典 (14)
- 其他杂项 (10)
- AndroidPn& MQTT&C2DM&推技术 (29)
- ActiveMQ学习和研究 (38)
- Google技术应用开发和API分析 (11)
- flex的学习总结 (59)
- 项目中一点总结 (20)
- java疑惑 java面向对象编程 (28)
- Android 开发学习 (133)
- linux和UNIX的总结 (37)
- Titanium学习总结 (20)
- JQueryMobile学习总结 (34)
- Phonegap学习总结 (32)
- HTML5学习总结 (41)
- JeeCMS研究和理解分析 (9)
最新评论
-
lgh1992314:
[u][i][b][flash=200,200][url][i ...
看看mybatis 源代码 -
尼古拉斯.fwp:
图片根本就不出来好吧。。。。。。
Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传 -
ln94223:
第一个应该用排它网关吧 怎么是并行网关, 并行网关是所有exe ...
工作流Activiti的学习总结(八)Activiti自动执行的应用 -
ZY199266:
获取不到任何消息信息,请问这是什么原因呢?
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息 -
xiaoyao霄:
DestinationSourceMonitor 报错 应该导 ...
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息
POI3.8组件研究(一)---基于User API (HSSF and XSSF)解析Excel2003和2007文件
在以前的Excel解析时候,我们通常需要编写Excel解析只能解析一种格式03版或者07版。现在POI3.5以后可以解析两种格式。我们知道在07的excel是基于xml格式的文件。
POI3.5以后的API包括如下几个方面:
The Apache POI Project's mission is to create and maintain Java APIs for manipulating various file formats based upon the Office Open XML standards (OOXML) and Microsoft's OLE 2 Compound Document format (OLE2). In short, you can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java. Apache POI is your Java Excel solution (for Excel 97-2008). We have a complete API for porting other OOXML and OLE2 formats and welcome others to participate.
OLE2 files include most Microsoft Office files such as XLS, DOC, and PPT as well as MFC serialization API based file formats. The project provides APIs for the OLE2 Filesystem (POIFS) and OLE2 Document Properties (HPSF) .
Office OpenXML Format is the new standards based XML file format found in Microsoft Office 2007 and 2008. This includes XLSX, DOCX and PPTX. The project provides a low level API to support the Open Packaging Conventions using openxml4j .
For each MS Office application there exists a component module that attempts to provide a common high level Java api to both OLE2 and OOXML document formats. This is most developed for Excel workbooks (SS=HSSF+XSSF ) . Work is progressing for Word documents (HWPF+XWPF ) and PowerPoint presentations (HSLF+XSLF) .
HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.
HSSF and XSSF provides ways to read spreadsheets create, modify, read and write XLS spreadsheets. They provide:
- low level structures for those with special needs
- an eventmodel api for efficient read-only access
- a full usermodel api for creating, reading and modifying XLS files
使用前提条件:
1.POI的版本必须高于等于3.5.
2.JDK的版本必须高于等于1.5.
本文重点代码讲述一下:
针对读取03和07版本的excel的公共方法如下:
/** * 根据文件的路径创建Workbook对象 * @param filePath */ private Workbook getExcelWorkBook(String filePath) { InputStream ins = null; Workbook book = null; try { ins=new FileInputStream(new File(filePath)); //ins= ExcelService.class.getClassLoader().getResourceAsStream(filePath); book = WorkbookFactory.create(ins); ins.close(); return book; } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (ins != null) { try { ins.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
/** * 以Map的格式存储数�? * 读取Excel文件的数�? * @param filePath excel 文件的 * @param headTitle * @return */ public Map<String,List<Map<String,Object>>> readEXCELMap(String filePath,String[] headTitle){ //获取workbook对象 Workbook workbook=getExcelWorkBook(filePath); //获取sheet页数 int sheetNum=workbook.getNumberOfSheets(); //存储excel相关的数�? Map<String,List<Map<String,Object>>> excelData=new HashMap<String,List<Map<String,Object>>>(); //遍历相关sheet页面获取相关的数�? if(sheetNum>0){ for (int index = 0; index < sheetNum; index++) { //创建sheet Sheet sheet=workbook.getSheetAt(index); //获取sheet的名�? String sheetName=workbook.getSheetName(index); //获取相关的数�? List<Map<String,Object>> sheetData=getExcelMapData(sheet, headTitle); excelData.put(sheetName, sheetData); } } return excelData; }
/** * 获取sheet表中的数�? * @param sheet * @return�?eadTitle 格式�?.1.2....列标做为key */ private List<Map<String,Object>> getExcelMapData(Sheet sheet,String[] headTitle){ //获取�?��和结束行 int startRow=sheet.getFirstRowNum(); int lastRow=sheet.getLastRowNum(); List<Map<String,Object>> allRowMapData=new ArrayList<Map<String,Object>>(); if(startRow!=lastRow){ //忽略第一行数�? startRow=startRow+1; //获取行数�? for(int indexRow=startRow;indexRow<lastRow;indexRow++){ Row row=sheet.getRow(indexRow); if(row==null){ continue; } int firstCellNum=row.getFirstCellNum(); int lastCellNum=row.getLastCellNum(); Map<String,Object> RowDataMap=new HashMap<String,Object>(); //遍历相关的列数据 for (int indexCol = firstCellNum; indexCol <lastCellNum; indexCol++) { Cell cell=row.getCell(indexCol); String cellKey=headTitle[indexCol-firstCellNum]; if(cell==null){ continue; } //获取列的数据的信�? Object cellValue = getCellValue(cell); RowDataMap.put(cellKey, cellValue); } allRowMapData.add(RowDataMap); } } return allRowMapData; }
/** * * 以Bean的方式存储bean对象 * 读取Excel文件的数�? * @param filePath excel 文件的路 * @param headTitle * @param clazz * @return */ public Map<String,List<T>> readEXCELBean(String filePath,String[] headTitle,Class<T> clazz){ //获取workbook对象 Workbook workbook=getExcelWorkBook(filePath); //获取sheet页数 int sheetNum=workbook.getNumberOfSheets(); //存储excel相关的数�? Map<String,List<T>> excelData=new HashMap<String,List<T>>(); //遍历相关sheet页面获取相关的数�? if(sheetNum>0){ for (int index = 0; index < sheetNum; index++) { //创建sheet Sheet sheet=workbook.getSheetAt(index); //获取sheet的名�? String sheetName=workbook.getSheetName(index); //获取相关的数�? List<T> sheetData=getExcelBeanData(sheet, headTitle,clazz); excelData.put(sheetName, sheetData); } } return excelData; }
/** * 获取sheet表中的数�? * @param sheet * @param sheet�?eadTitle bean每列对应的属性数�? * @param clazz bean对应的类 * @throws InstantiationException */ @SuppressWarnings("unused") private List<T> getExcelBeanData(Sheet sheet,String[] headTitle,Class<T> clazz) { //获取�?��和结束行 int startRow=sheet.getFirstRowNum(); int lastRow=sheet.getLastRowNum(); List<T> allRowMapData=new ArrayList<T>(); if(startRow!=lastRow){ //忽略第一行数�? startRow=startRow+1; //获取行数�? for(int indexRow=startRow;indexRow<lastRow;indexRow++){ Row row=sheet.getRow(indexRow); if(row==null){ continue; } int firstCellNum=row.getFirstCellNum(); int lastCellNum=row.getLastCellNum(); T bean=null; try { bean = clazz.newInstance(); //遍历相关的列数据 for (int indexCol = firstCellNum; indexCol <lastCellNum; indexCol++) { Cell cell=row.getCell(indexCol); //indexCol=11 firstCellNum 0 lastCellNum=11 //System.out.println("indexCol="+indexCol+"firstCellNum "+firstCellNum+" lastCellNum="+lastCellNum+" headTitle.length"+headTitle.length); String cellKey=headTitle[indexCol-firstCellNum]; if(cell==null){ continue; } //获取列的数据的信�? Object cellValue = getCellValue(cell); try { BeanUtils.setProperty(bean, cellKey, cellValue); } catch (InvocationTargetException e) { e.printStackTrace(); } } allRowMapData.add(bean); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } } } return allRowMapData; }
评论
这个是怎么来的? 麻烦告知:tanghly@gmail.com
那个根据单元个的类型获取单元格的值。
自己添加了一个方法,测试通过;另外楼主的上面用到了apache的beanutils,需要下载相关jar。添加的getCellValue方法为:
private Object getCellValue(Cell cell){ Object value = null; int cellType = cell.getCellType(); switch(cellType){ case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_BOOLEAN: value = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_ERROR: value = cell.getErrorCellValue(); break; case Cell.CELL_TYPE_FORMULA: value = cell.getCellFormula(); break; case Cell.CELL_TYPE_NUMERIC: value = cell.getNumericCellValue(); break; case Cell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; } return value; }
这个是怎么来的? 麻烦告知:tanghly@gmail.com
发表评论
-
【转】Django resources
2014-01-23 14:35 10717Django resources This page li ... -
使用国内镜像源来加速python pypi包的安装
2014-01-16 11:16 197571pipy国内镜像目前有: http://pypi.d ... -
[转 ]vagrant使用简介
2014-01-10 13:53 256151> 简介: vagrant提供了易于配置,重复性 ... -
[转]在Java中调用Python
2014-01-07 13:08 9143在执行之前都需要把jython对应的包加载进去,这个是必须的 ... -
[转]Jython初探
2014-01-07 11:19 2354转载自: ... -
[转]Eclipse配置PyDev插件
2014-01-02 14:25 2781安装python解释器 安装PyDev: 首 ... -
RestFuse的研究(五) Http请求的封装
2014-06-14 15:50 3517在RestFuse中封装了Http请 ... -
RestFuse的研究(四) Junit的Statement的分析
2013-12-06 11:46 1605在RestFuse提供了多种单 ... -
RestFuse的研究(三) Junit的Rule的使用和分析
2013-12-06 11:01 2191在junit中定义一些可以公用的规则(R ... -
RestFuse的研究(二) Junit的Runner的分类和模式
2013-12-06 10:40 1555在Junit4中的调用JunitCore可以采 ... -
RestFuse的研究(一) HttpJunitRunner的实现
2013-12-06 10:11 1693在RestFuse是一种针对Rest We ... -
[转]An open-source JUnit extension to test HTTP/REST APIs
2013-12-06 09:57 1054http://developer.eclipsesource ... -
TestNG简单的学习(十三)TestNG中Junit的实现
2013-12-04 09:00 3288TestNG和junit的整合 ... -
TestNG简单的学习(十二)TestNG运行
2013-12-03 09:08 51452文档来自官方地址: ... -
TestNG简单的学习(十一)TestNG学习总结
2013-12-03 09:08 13999最近一直在学习关于TestNG方面的知识,根 ... -
TestNG简单的学习(十)TestNG @Listeners 的使用
2013-12-03 09:07 8627TestNG官方网站: http://testng.or ... -
TestNG简单的学习(九)TestNG Method Interceptors 的使用
2013-12-03 09:07 2657TestNG官方网站: http://testng ... -
TestNG简单的学习(八)TestNG Annotation Transformers 的使用
2013-12-03 09:07 2761TestNG官方网站: http://testng.or ... -
TestNG简单的学习(七)TestNG编程方式运行
2013-12-02 09:22 2397TestNG官方网站: http://testng.or ... -
TestNG简单的学习(六)测试工厂注释的使用
2013-12-02 09:22 2717TestNG官方网站: http://testng.or ...
相关推荐
POI-HSSF和POI-XSSF和SXSSF(自POI 3.8 beta3起)-用于访问Microsoft Excel格式文件
内部包括poi-3.8-20120326.jar、poi-ooxml-3.8-20120326.jar、poi-ooxml-schemas-3.8-20120326.jar 测试可用版本,有些下载的不能用
poi-src-3.8-beta5-20111217.tar.gz
poi-3.8-beta3-20110606,poi-examples-3.8-beta3-20110606
NULL 博文链接:https://topmanopensource.iteye.com/blog/1562322
excl上传 poi-ooxml-schemas-3.8,poi-3.8,poi-ooxml jar包合集
POI最新版 3.8-beta4-20110826 可以实现解析word excel ppt等
java中读取word文档需要引用apache的poi开源项目...为方便下载提供6个jar包,其中包含:poi-3.8.jar;poi-ooxml-3.8.jar;poi-ooxml-schemas-3.8.jar;poi-scratchpad-3.8.jar;xmlbeans-2.3.0.jar;dom4j-1.6.1.jar。
Apache POI HSSF和XSSF读写EXCEL总结
poi-3.8-20120326-6个jar包: poi-3.8-20120326.jar poi-examples-3.8-20120326.jar poi-excelant-3.8-20120326.jar poi-ooxml-3.8-20120326.jar poi-ooxml-schemas-3.8-20120326.jar poi-scratchpad-3.8-20120326....
赠送jar包:poi-ooxml-4.1.2.jar; 赠送原API文档:poi-ooxml-4.1.2-javadoc.jar; 赠送源代码:poi-ooxml-4.1.2-sources.jar; 赠送Maven依赖信息文件:poi-ooxml-4.1.2.pom; 包含翻译后的API文档:poi-ooxml-...
NULL 博文链接:https://topmanopensource.iteye.com/blog/1561151
用户poi开发,将excel中的数据导入到数据库中所用的jar包。poi-3.8-20120326.jar 和 poi-excelant-3.8-20120326.jar
文件是解析excel的poi-3.8版本的相关文件。兼容2003和2007版。
POI是Apache开源项目之一,对Office文件的操作拥有强大的功能。附件中为常用的操作之一:Excel解析,支持Excel2003和2007的解析,示例仅供参考。
赠送jar包:poi-ooxml-3.16.jar; 赠送原API文档:poi-ooxml-3.16-javadoc.jar; 赠送源代码:poi-ooxml-3.16-sources.jar; 赠送Maven依赖信息文件:poi-ooxml-3.16.pom; 包含翻译后的API文档:poi-ooxml-3.16-...
POI3.5-HSSF-和XSSF-Excel操作快速入门
poi3.8jar包一套+poi-pdf1.0和poi-core1.0两个jar包,网上搜索的代码就是需要这些jar包才能不借助第三方工具word转PDF
Apache POI HSSF and XSSF 快速指南帮助文档 API poi-3.15
导入excel需要的\poi-ooxml-schemas-3.8-20120326.jar