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;
}
@Override
public void setIncludeCellComments(boolean includeCellComments) {
_includeCellComments = includeCellComments;
}
/**
* 获取单元格格式内容
* @param cell
* @return
*/
@Override
public 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 exists
HSSFComment comment = hssfCell.getCellComment();
if(_includeCellComments && comment != null) {
// Replace any newlines with spaces, otherwise it
// breaks the output
String commentText = comment.getString().getString().replace('\n', ' ');
text.append(" Comment by "+comment.getAuthor()+": "+commentText);
}
}
return text.toString();
}
@Override
public String getText() {
// TODO Auto-generated method stub
return 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 exists
Comment comment = xssfCell.getCellComment();
if(includeCellComments && comment != null) {
// Replace any newlines with spaces, otherwise it
// breaks the output
String 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;
}
}
}
@Override
public void setFormulasNotResults(boolean formulasNotResults) {
this.formulasNotResults = formulasNotResults;
}
@Override
public String getText() {
// TODO Auto-generated method stub
return null;
}
@Override
public 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技术可以在 ...
随机推荐
- 使用spring的@Async异步执行方法
应用场景: 1.某些耗时较长的而用户不需要等待该方法的处理结果 2.某些耗时较长的方法,后面的程序不需要用到这个方法的处理结果时 在spring的配置文件中加入对异步执行的支持 <beans x ...
- CentOS 6.5上使用gdb调试时出现Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.i686 .
在CentOS6.5上用gdb调试时提示Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.i686先修改 ...
- python实现的电影票房数据可视化
代码地址如下:http://www.demodashi.com/demo/14275.html 详细说明: Tushare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从数据采 ...
- ocat 资源路径-时间控件
http://www.htmleaf.com/jQuery/Calendar-Date-Time-picker/201504251737.html
- 错误号:1364 错误信息:Field 'platId' doesn't have a default value
1. 错误描写叙述 错误号:1364 错误信息:Field 'platId' doesn't have a default value insert into `use`.`t_platform_sc ...
- 在sys用户下执行的sql脚本创建了摁多个表和序列, 怎么回退?
一个个删除, 暂时不会别的方法...
- GitHub搭建博客过程
1.参考 我的 Github 个人博客是怎样炼成的 http://www.jianshu.com/p/4fd3cb0a11da 到了第三节"三.使用 Jekyll 搭建个人博客"时 ...
- HDUOJ------------1051Wooden Sticks
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 摘:VC开发数据库基础之ADO篇
一.ADO简介ADO(ActiveX Data Object)是Microsoft数据库应用程序开发的新接口,是建立在OLE DB之上的高层数据库访问技术,请不必为此担心,即使你对OLE DB,COM ...
- shell 提取mysql指定数据库下表创建语句为单文件
dbcn="mysql -h172.16.1.194 -uroot -p123456"; db=dsp_ad_center; ii=0; ct=`$dbcn -N -e " ...