• poi 抽取execl表面数据源代码工具



    开发中 ,导入导出execl避免不了数据类型格式的校验,在使用poi要使用抽取表面数据,poi暂时不支持单元格抽取,查询poi源码抽取工具类如下,如使用jxl就不必使用,jxl取出的单元格数据已是抽取后的表面数据

    注:poi版本 - 3.10


    接口 ExeclExtractor
    1. package com.dadi.oa.util.poi;
    2. import org.apache.poi.ss.usermodel.Cell;
    3. /**
    4. * poi execl文本抽取接口
    5. * @author ao.ouyang
    6. *
    7. */
    8. public interface ExeclExtractor {
    9. /**
    10. * 抽取单元格文本
    11. * @param cell
    12. * @return
    13. */
    14. public String getText(Cell cell);
    15. /**
    16. * 公式结果
    17. * @param formulasNotResults
    18. */
    19. public void setFormulasNotResults(boolean formulasNotResults);
    20. /**
    21. * 是否抽取注释
    22. * @param includeCellComments
    23. */
    24. public void setIncludeCellComments(boolean includeCellComments);
    25. }

    03版抽取方法 HSSFExeclExtractor

    1. package com.dadi.oa.util.poi;
    2. import org.apache.poi.POIOLE2TextExtractor;
    3. import org.apache.poi.hssf.usermodel.HSSFCell;
    4. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    5. import org.apache.poi.hssf.usermodel.HSSFComment;
    6. import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
    7. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
    8. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    9. import org.apache.poi.ss.formula.eval.ErrorEval;
    10. import org.apache.poi.ss.usermodel.Cell;
    11. import org.apache.poi.xssf.usermodel.XSSFCell;
    12. /**
    13. * Poi操作工具类 03版<br/>
    14. * 功能1:获取execl单元格显示的文本<br/>
    15. * @author ao.ouyang
    16. *
    17. */
    18. public class HSSFExeclExtractor extends POIOLE2TextExtractor implements ExeclExtractor {
    19. private HSSFDataFormatter _formatter;
    20. private boolean _shouldEvaluateFormulas = true;
    21. private boolean _includeCellComments = false;
    22. public HSSFExeclExtractor(HSSFWorkbook wb) {
    23. super(wb);
    24. _formatter = new HSSFDataFormatter();
    25. }
    26. public void setFormulasNotResults(boolean formulasNotResults) {
    27. _shouldEvaluateFormulas = !formulasNotResults;
    28. }
    29. @Override
    30. public void setIncludeCellComments(boolean includeCellComments) {
    31. _includeCellComments = includeCellComments;
    32. }
    33. /**
    34. * 获取单元格格式内容
    35. * @param cell
    36. * @return
    37. */
    38. @Override
    39. public String getText(Cell cell) {
    40. HSSFCell hssfCell = (HSSFCell) cell;
    41. StringBuffer text = new StringBuffer();
    42. if(hssfCell != null) {
    43. switch(hssfCell.getCellType()) {
    44. case HSSFCell.CELL_TYPE_STRING:
    45. text.append(hssfCell.getRichStringCellValue().getString());
    46. break;
    47. case HSSFCell.CELL_TYPE_NUMERIC:
    48. text.append(
    49. _formatter.formatCellValue(hssfCell)
    50. );
    51. break;
    52. case HSSFCell.CELL_TYPE_BOOLEAN:
    53. text.append(hssfCell.getBooleanCellValue());
    54. break;
    55. case HSSFCell.CELL_TYPE_ERROR:
    56. text.append(ErrorEval.getText(hssfCell.getErrorCellValue()));
    57. break;
    58. case HSSFCell.CELL_TYPE_FORMULA:
    59. if(!_shouldEvaluateFormulas) {
    60. text.append(hssfCell.getCellFormula());
    61. } else {
    62. switch(cell.getCachedFormulaResultType()) {
    63. case HSSFCell.CELL_TYPE_STRING:
    64. HSSFRichTextString str = hssfCell.getRichStringCellValue();
    65. if(str != null && str.length() > 0) {
    66. text.append(str.toString());
    67. }
    68. break;
    69. case HSSFCell.CELL_TYPE_NUMERIC:
    70. HSSFCellStyle style = hssfCell.getCellStyle();
    71. if(style == null) {
    72. text.append( cell.getNumericCellValue() );
    73. } else {
    74. text.append(
    75. _formatter.formatRawCellContents(
    76. cell.getNumericCellValue(),
    77. style.getDataFormat(),
    78. style.getDataFormatString()
    79. )
    80. );
    81. }
    82. break;
    83. case HSSFCell.CELL_TYPE_BOOLEAN:
    84. text.append(cell.getBooleanCellValue());
    85. break;
    86. case HSSFCell.CELL_TYPE_ERROR:
    87. text.append(ErrorEval.getText(cell.getErrorCellValue()));
    88. break;
    89. }
    90. }
    91. break;
    92. }
    93. // Output the comment, if requested and exists
    94. HSSFComment comment = hssfCell.getCellComment();
    95. if(_includeCellComments && comment != null) {
    96. // Replace any newlines with spaces, otherwise it
    97. // breaks the output
    98. String commentText = comment.getString().getString().replace(' ', ' ');
    99. text.append(" Comment by "+comment.getAuthor()+": "+commentText);
    100. }
    101. }
    102. return text.toString();
    103. }
    104. @Override
    105. public String getText() {
    106. // TODO Auto-generated method stub
    107. return null;
    108. }
    109. }


    07 版抽取方法 XSSFExeclExtractor

    1. package com.dadi.oa.util.poi;
    2. import java.io.IOException;
    3. import java.util.Iterator;
    4. import java.util.Locale;
    5. import org.apache.poi.POIXMLTextExtractor;
    6. import org.apache.poi.ss.usermodel.Cell;
    7. import org.apache.poi.ss.usermodel.CellStyle;
    8. import org.apache.poi.ss.usermodel.Comment;
    9. import org.apache.poi.ss.usermodel.DataFormatter;
    10. import org.apache.poi.xssf.usermodel.XSSFCell;
    11. import org.apache.poi.xssf.usermodel.XSSFSheet;
    12. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    13. /**
    14. * Poi操作工具类 07版<br/>
    15. * 功能1:获取execl单元格显示的文本<br/>
    16. * @author ao.ouyang
    17. *
    18. */
    19. public class XSSFExeclExtractor extends POIXMLTextExtractor implements ExeclExtractor {
    20. private Locale locale;
    21. private boolean formulasNotResults = false;
    22. private boolean includeCellComments = false;
    23. public XSSFExeclExtractor(XSSFWorkbook workbook) {
    24. super(workbook);
    25. }
    26. /**
    27. * 获取单元格格式内容
    28. * @param cell
    29. * @return
    30. */
    31. public String getText(Cell cell) {
    32. XSSFCell xssfCell = (XSSFCell) cell;
    33. DataFormatter formatter;
    34. if(locale == null) {
    35. formatter = new DataFormatter();
    36. } else {
    37. formatter = new DataFormatter(locale);
    38. }
    39. StringBuffer text = new StringBuffer();
    40. // Is it a formula one?
    41. if(xssfCell!=null){
    42. if(xssfCell.getCellType() == Cell.CELL_TYPE_FORMULA) {
    43. if (formulasNotResults) {
    44. text.append(xssfCell.getCellFormula());
    45. } else {
    46. if (xssfCell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) {
    47. handleStringCell(text, xssfCell);
    48. } else {
    49. handleNonStringCell(text, xssfCell, formatter);
    50. }
    51. }
    52. } else if(xssfCell.getCellType() == Cell.CELL_TYPE_STRING) {
    53. handleStringCell(text, xssfCell);
    54. } else {
    55. handleNonStringCell(text, xssfCell, formatter);
    56. }
    57. // Output the comment, if requested and exists
    58. Comment comment = xssfCell.getCellComment();
    59. if(includeCellComments && comment != null) {
    60. // Replace any newlines with spaces, otherwise it
    61. // breaks the output
    62. String commentText = comment.getString().getString().replace(' ', ' ');
    63. text.append(" Comment by ").append(comment.getAuthor()).append(": ").append(commentText);
    64. }
    65. }
    66. return text.toString();
    67. }
    68. private void handleStringCell(StringBuffer text, Cell cell) {
    69. text.append(cell.getRichStringCellValue().getString());
    70. }
    71. private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) {
    72. int type = cell.getCellType();
    73. if (type == Cell.CELL_TYPE_FORMULA) {
    74. type = cell.getCachedFormulaResultType();
    75. }
    76. if (type == Cell.CELL_TYPE_NUMERIC) {
    77. CellStyle cs = cell.getCellStyle();
    78. if (cs.getDataFormatString() != null) {
    79. text.append(formatter.formatRawCellContents(
    80. cell.getNumericCellValue(), cs.getDataFormat(), cs.getDataFormatString()
    81. ));
    82. return;
    83. }
    84. }
    85. }
    86. @Override
    87. public void setFormulasNotResults(boolean formulasNotResults) {
    88. this.formulasNotResults = formulasNotResults;
    89. }
    90. @Override
    91. public String getText() {
    92. // TODO Auto-generated method stub
    93. return null;
    94. }
    95. @Override
    96. public void setIncludeCellComments(boolean formulasNotResults) {
    97. this.includeCellComments = includeCellComments;
    98. }
    99. }


  • 相关阅读:
    android游戏开发框架libgdx的使用(十二)—TiledMap地图的使用
    android游戏开发框架libgdx的使用(十一)—Skin和UI配置文件的使用
    子句判断、启动强度和去模糊化AForge.NET框架的使用(三)
    分享从网上收集的一些游戏资源,以RPG类为主
    android游戏开发框架libgdx的使用(十六)—使用TexturePacker工具加快开发速度
    android游戏开发框架libgdx的使用(十三)—TiledMap中的角色和角色移动
    Ajax实现评论的顶和踩功能
    Jelastic支持java的PaaS
    真心好用的VS扩展NuGet
    分享几篇文章(PDF版)
  • 原文地址:https://www.cnblogs.com/signheart/p/6595686.html
Copyright © 2020-2023  润新知