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;
}
}