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技术可以在 ...
随机推荐
- 关联更新SQL语句
update F_A_Info set level=b.level from F_A_Info a,F_A_Info_QUAN b where a.id=b.id
- Chrome 编译错误汇总
由于各种你懂的原因,訪问google的服务总是出错,先是hosts不工作.代理也不好使,最后最终能够短暂訪问了.我的版本号还是採用svn维护的,直接svn update也不行.试试git吧,一晚上才下 ...
- LinkedHashMap插入无序且链式操作
Iterator<Entry<Integer, Integer>> ite=lhmap.entrySet().iterator(); ite.next(); ite.remov ...
- PAT 1085 Perfect Sequence
PAT 1085 Perfect Sequence 题目: Given a sequence of positive integers and another positive integer p. ...
- ubuntu 中数据的迁移
1.先停止mysql /etc/init.d/mysql stop
- http://www.cnblogs.com/txw1958/p/alipay-f2fpay.html
一.条码支付及二维码支付介绍 1. 条码支付 条码支付是支付宝给到线下传统行业的一种收款方式.商家使用扫码枪等条码识别设备扫描用户支付宝钱包上的条码/二维码,完成收款.用户仅需出示付款码,所有收款操作 ...
- 【php】基础学习1
其中包括php基础.字符串和正则表达式的学习.具体如下: <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta h ...
- LINUX使用DVD光盘或者ISO作为本地YUM源
先把光盘或 ISO 文件挂载到文件系统: # mkdir /media/iso 挂载光盘: # mount /dev/cdrom /media/iso 或挂载 ISO 文件: mount -o loo ...
- OAF_OAF控件系列5 - Train的实现(案例)
2014-06-02 Created By BaoXinjian
- JSP页面中文乱码
近期搭建了一个JAVA WEB项目,引入了国际化的ResourceBundle. 顺便赞一个,Eclipse Mars,自带了中文转unicode编码的功能. ① 根据语言选择,初始化Resource ...