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技术可以在 ...
随机推荐
- Unix 网络编程 读书笔记3
第四章 基本tcp 套接口编程 注意区分AF_XXX 和PF_XXX,AF代表address family, PF代表protocol family. 1 socket 函数 2 connect 函数 ...
- maven 找不到或无法加载主类
maven 找不到或无法加载主类 CreateTime--2018年4月19日22:58:14 Author:Marydon 1.情景还原: 在maven管理的web项目中,单独运行Java类报错 ...
- C# 获取今天,昨天,上周,下周,上月,下月等等一些日期格式
C#里内置的DateTime基本上都可以实现这些功能,巧用DateTime会使你处理这些事来变轻松多了 今天 DateTime.Now.Date ...
- DIV+CSS布局重新学习之float/margin/padding
之前对div的css布局一直有点半知半解,只其然却不知其所以然,到网上下载了“十天学会DIV+CSS(WEB标准)”的chm电子版,然后跟着练习了一下,特在此记录,备忘. <!DOCTYPE h ...
- 获取泛型类对应的class类型
自己写来备忘的,如有错误,请指正! public class Demo<T> { private Class<T> clazz; public Demo() { Paramet ...
- office-word去掉效验红色的波浪线
工作中,总是能发现不足.能再次学习到知识和经验!
- Dubbo Monitor 配置
1. Dubbo Monitor 下载dubbo-monitor-simple-2.5.3-assembly.tar.gz 链接:http://pan.baidu.com/s/1gf88wDX 密码: ...
- Matlab 调用Oracle数据库
本文分两部分,1.通过sql语句操作数据库.2.通过ddl操作数据库 一 通过ODBC实现到oracle的连接1)控制面板->管理工具->ODBC数据源->在系统DSN中添加orac ...
- matplot模块中的pylab
pylab的目的 Pylab combines the functionality of pyplot with the capabilities of NumPy in a single names ...
- iOS 排序算法总结、二分法查找
1.插入排序 在要排序的一组数中,假设前面(n-1) [n>=2] 个数已经是排好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数也是排好顺序的.如此反复循环,直到全部排好顺序. 直接插 ...