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技术可以在 ...
随机推荐
- 前端开发不容错过的jQuery图片滑块插件(转)
作为前端开发者,我们会碰到很到各种各样的jQuery插件.今天要分享的几款jQuery图片滑块插件,也就是jQuery焦点图插件,基本上会在每个网站都有应用,可以下载看看,也许你可以用到. 1.jQu ...
- 【redis】常用命令
三.常用命令 1)连接操作命令 quit:关闭连接(connection) auth:简单密码认证 help cmd: 查看cmd帮助,例如:help quit ...
- coding云进行git push报:permission denied
1.原因可能是 登录其他的git 项目,本地缓存了其他用户的 用户名和密码 认证信息,导致一直权限不通过 解决: git remote add origin http://yourname:passw ...
- 转载:substr() mb_substr() mb_subcut区别与联系
substr() $rest = substr("abcdef", 1); //bcdef $rest = substr("abcdef", 1,5); //b ...
- python接口自动化(二十七)--html 测试报告——上(详解)
简介 上一篇我们批量执行完用例后,生成的测试报告是文本形式的,不够直观,而且报告一般都是发给leader的,所以最好是直观一目了然,为了更好的展示测试报告,最好是生成 HTML 格式的.unittes ...
- Android获取屏幕大小和设置无标题栏
android获取屏幕大小非常常用,例如写个程序,如果要做成通用性很强的程序,适用屏幕很强,一般布局的时候都是根据屏幕的长宽来定义的,所以我把这个总结一下,方便日后忘记的时候查阅.还有就是有时候写程序 ...
- android 布局权重问题(最近布局经常坑爹)
android 布局 权重 With layout_weight you can specify a size ratio between multiple views. E.g. you have ...
- linux系统调用sysconf
1.前言 当前计算机都是多核的,linux2.6提供了进程绑定cpu功能,将进程指定到某个core上执行,方便管理进程.linux提供了sysconf系统调用可以获取系统的cpu个数和可用的cpu个数 ...
- Shell习题100例
每日一文件 https://github.com/aminglinux/shell100/blob/master/ 要求:安照这样的日期格式(xxxx-xx-xx)每日生成一个文件,如生成的文件为20 ...
- unity 主循环
在unity官方文档中看到这个图,感觉很有用,各事件的先后时机看得较清楚. 连接:http://docs.unity3d.com/Manual/ExecutionOrder.html