poi 抽取execl表面数据源代码工具
package com.dadi.oa.util.poi;import org.apache.poi.ss.usermodel.Cell;/*** poi execl文本抽取接口* @author ao.ouyang**/public interface ExeclExtractor {/*** 抽取单元格文本* @param cell* @return*/public String getText(Cell cell);/*** 公式结果* @param formulasNotResults*/public void setFormulasNotResults(boolean formulasNotResults);/*** 是否抽取注释* @param includeCellComments*/public void setIncludeCellComments(boolean includeCellComments);}
package com.dadi.oa.util.poi;import org.apache.poi.POIOLE2TextExtractor;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFComment;import org.apache.poi.hssf.usermodel.HSSFDataFormatter;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.formula.eval.ErrorEval;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.xssf.usermodel.XSSFCell;/*** Poi操作工具类 03版<br/>* 功能1:获取execl单元格显示的文本<br/>* @author ao.ouyang**/public class HSSFExeclExtractor extends POIOLE2TextExtractor implements ExeclExtractor {private HSSFDataFormatter _formatter;private boolean _shouldEvaluateFormulas = true;private boolean _includeCellComments = false;public HSSFExeclExtractor(HSSFWorkbook wb) {super(wb);_formatter = new HSSFDataFormatter();}public void setFormulasNotResults(boolean formulasNotResults) {_shouldEvaluateFormulas = !formulasNotResults;}@Overridepublic void setIncludeCellComments(boolean includeCellComments) {_includeCellComments = includeCellComments;}/*** 获取单元格格式内容* @param cell* @return*/@Overridepublic String getText(Cell cell) {HSSFCell hssfCell = (HSSFCell) cell;StringBuffer text = new StringBuffer();if(hssfCell != null) {switch(hssfCell.getCellType()) {case HSSFCell.CELL_TYPE_STRING:text.append(hssfCell.getRichStringCellValue().getString());break;case HSSFCell.CELL_TYPE_NUMERIC:text.append(_formatter.formatCellValue(hssfCell));break;case HSSFCell.CELL_TYPE_BOOLEAN:text.append(hssfCell.getBooleanCellValue());break;case HSSFCell.CELL_TYPE_ERROR:text.append(ErrorEval.getText(hssfCell.getErrorCellValue()));break;case HSSFCell.CELL_TYPE_FORMULA:if(!_shouldEvaluateFormulas) {text.append(hssfCell.getCellFormula());} else {switch(cell.getCachedFormulaResultType()) {case HSSFCell.CELL_TYPE_STRING:HSSFRichTextString str = hssfCell.getRichStringCellValue();if(str != null && str.length() > 0) {text.append(str.toString());}break;case HSSFCell.CELL_TYPE_NUMERIC:HSSFCellStyle style = hssfCell.getCellStyle();if(style == null) {text.append( cell.getNumericCellValue() );} else {text.append(_formatter.formatRawCellContents(cell.getNumericCellValue(),style.getDataFormat(),style.getDataFormatString()));}break;case HSSFCell.CELL_TYPE_BOOLEAN:text.append(cell.getBooleanCellValue());break;case HSSFCell.CELL_TYPE_ERROR:text.append(ErrorEval.getText(cell.getErrorCellValue()));break;}}break;}// Output the comment, if requested and existsHSSFComment comment = hssfCell.getCellComment();if(_includeCellComments && comment != null) {// Replace any newlines with spaces, otherwise it// breaks the outputString commentText = comment.getString().getString().replace('\n', ' ');text.append(" Comment by "+comment.getAuthor()+": "+commentText);}}return text.toString();}@Overridepublic String getText() {// TODO Auto-generated method stubreturn null;}}
package com.dadi.oa.util.poi;import java.io.IOException;import java.util.Iterator;import java.util.Locale;import org.apache.poi.POIXMLTextExtractor;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Comment;import org.apache.poi.ss.usermodel.DataFormatter;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;/*** Poi操作工具类 07版<br/>* 功能1:获取execl单元格显示的文本<br/>* @author ao.ouyang**/public class XSSFExeclExtractor extends POIXMLTextExtractor implements ExeclExtractor {private Locale locale;private boolean formulasNotResults = false;private boolean includeCellComments = false;public XSSFExeclExtractor(XSSFWorkbook workbook) {super(workbook);}/*** 获取单元格格式内容* @param cell* @return*/public String getText(Cell cell) {XSSFCell xssfCell = (XSSFCell) cell;DataFormatter formatter;if(locale == null) {formatter = new DataFormatter();} else {formatter = new DataFormatter(locale);}StringBuffer text = new StringBuffer();// Is it a formula one?if(xssfCell!=null){if(xssfCell.getCellType() == Cell.CELL_TYPE_FORMULA) {if (formulasNotResults) {text.append(xssfCell.getCellFormula());} else {if (xssfCell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) {handleStringCell(text, xssfCell);} else {handleNonStringCell(text, xssfCell, formatter);}}} else if(xssfCell.getCellType() == Cell.CELL_TYPE_STRING) {handleStringCell(text, xssfCell);} else {handleNonStringCell(text, xssfCell, formatter);}// Output the comment, if requested and existsComment comment = xssfCell.getCellComment();if(includeCellComments && comment != null) {// Replace any newlines with spaces, otherwise it// breaks the outputString commentText = comment.getString().getString().replace('\n', ' ');text.append(" Comment by ").append(comment.getAuthor()).append(": ").append(commentText);}}return text.toString();}private void handleStringCell(StringBuffer text, Cell cell) {text.append(cell.getRichStringCellValue().getString());}private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) {int type = cell.getCellType();if (type == Cell.CELL_TYPE_FORMULA) {type = cell.getCachedFormulaResultType();}if (type == Cell.CELL_TYPE_NUMERIC) {CellStyle cs = cell.getCellStyle();if (cs.getDataFormatString() != null) {text.append(formatter.formatRawCellContents(cell.getNumericCellValue(), cs.getDataFormat(), cs.getDataFormatString()));return;}}}@Overridepublic void setFormulasNotResults(boolean formulasNotResults) {this.formulasNotResults = formulasNotResults;}@Overridepublic String getText() {// TODO Auto-generated method stubreturn null;}@Overridepublic void setIncludeCellComments(boolean formulasNotResults) {this.includeCellComments = includeCellComments;}}
poi 抽取execl表面数据源代码工具的更多相关文章
- [Hadoop 周边] Hadoop和大数据:60款顶级大数据开源工具(2015-10-27)【转】
说到处理大数据的工具,普通的开源解决方案(尤其是Apache Hadoop)堪称中流砥柱.弗雷斯特调研公司的分析师Mike Gualtieri最近预测,在接下来几年,“100%的大公司”会采用Hado ...
- Hadoop和大数据:60款顶级大数据开源工具
一.Hadoop相关工具 1. Hadoop Apache的Hadoop项目已几乎与大数据划上了等号.它不断壮大起来,已成为一个完整的生态系统,众多开源工具面向高度扩展的分布式计算. 支持的操作系统: ...
- 【转载】Hadoop和大数据:60款顶级大数据开源工具
一.Hadoop相关工具 1. Hadoop Apache的Hadoop项目已几乎与大数据划上了等号.它不断壮大起来,已成为一个完整的生态系统,众多开源工具面向高度扩展的分布式计算. 支持的操作系统: ...
- Linux 上的数据可视化工具
Linux 上的数据可视化工具 5 种开放源码图形化工具简介 Linux® 上用来实现数据的图形可视化的应用程序有很多,从简单的 2-D 绘图到 3-D 制图,再到科学图形编程和图形模拟.幸运的是,这 ...
- 数据集成工具Kettle、Sqoop、DataX的比较
数据集成工具很多,下面是几个使用比较多的开源工具. 1.阿里开源软件:DataX DataX 是一个异构数据源离线同步工具,致力于实现包括关系型数据库(MySQL.Oracle等).H ...
- 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据
1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...
- Java POI 实现Excel相同数据同一颜色,不同数据颜色交替显示
目录 1.效果图 2.具体代码实现 excel 读取工具类 excel写入和测试类 1.效果图 2.具体代码实现 excel 读取工具类 package utils; import java.io.F ...
- 一篇文章看懂TPCx-BB(大数据基准测试工具)源码
TPCx-BB是大数据基准测试工具,它通过模拟零售商的30个应用场景,执行30个查询来衡量基于Hadoop的大数据系统的包括硬件和软件的性能.其中一些场景还用到了机器学习算法(聚类.线性回归等).为了 ...
- java的poi技术读取Excel数据到MySQL
这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...
随机推荐
- dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /private/var/mobile/Conta
dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /private/var/mobile/Cont ...
- python之模块csv之 读取CSV文件(reader和DictReader2个方法)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #读取CSV文件(reader和DictReader2个方法) import csv #csv文件,是一种常用 ...
- MyEclipse下的Debug调试技巧汇总
首先以debug模式启动tomcat,并文件中设断点,然后运行,当程序走到断点处就会转到debug视图下 [1]快捷键(F8)直接执行程序. [2]快捷键(F5)单步执行程序,遇到方法时进入. [3] ...
- linux(red hat)下安装jenkins
Jenkins的安装能够分为在线安装和下载软件本地安装.我这里用的是另外一种方法,将其下载后是一个应用程序直接点击安装就能够.等安装完后配置一下jdk的路径就ok啦!接下来进行具体的说明: 一.前提 ...
- eclipse里面的时间错误,比电脑系统时间慢了8个小时
eclipse里面的时间错误,比电脑系统时间慢了8个小时 解决办法: 打开<eclipse安装目录>/eclipse.ini文件 在文件末尾追加 -Duser.timezone=Asia/ ...
- JavaScript中的数组与伪数组的区别
在JavaScript中,除了5种原始数据类型之外,其他所有的都是对象,包括函数(Function). 基本数据类型:String,boolean,Number,Undefined, Null 引用数 ...
- HDUOJ----(2612)Find a way
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- iOS archiveRootObject 归档失败问题
归档失败问题出在路径上,NSHomeDirectory() NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocument ...
- innerHTML和innerText区分
示例代码:<div id="test"> <span style="color:red">test1</span> test ...
- 搭建Go调试环境(LiteIDE)
安装及配置LiteIDE 将 liteidex32.1.windows-qt5.zip解压到D:\即完成安装. 设置编辑环境 查看->编辑当前环境,确认GOROOT变 ...