资讯专栏INFORMATION COLUMN

java excel类

hiyayiji / 736人阅读

摘要:单元格各类型数据读取基本类型处理的数据包括字符型数据,数字日期公式等。下面是单元格类型说明实例解析中数据,要求转换为文本方式存储写一个解析的抽象类版本的版本的解析的文件格式有误

1.单元格各类型数据读取

1.1 基本类型

处理的Excel数据包括字符型数据,数字、日期、公式等。

下面是单元格类型说明:

2实例
解析excel中数据,要求转换为文本方式存储
2.1 写一个excel解析的抽象类

public abstract class ExcelParser {

        private String fileName;
        private InputStream inputStream;
    
        private final static String excel2003L = ".xls";//2003- 版本的excel
        private final static String excel2007U = ".xlsx";//2007+ 版本的excel
    
        protected final static DecimalFormat decimalFormat = new DecimalFormat("0");
        protected final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
    
        protected final static String[] columnStr = new String[]{
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
        };
           protected OrderExcelParser(String fileName, InputStream inputStream) {
            this.fileName = fileName;
            this.inputStream = inputStream;
        }
    
        protected Workbook getWorkbook() throws Exception {
            try {
                String fileType = fileName.substring(fileName.lastIndexOf("."));
                if (excel2003L.equals(fileType)) {
                    return new HSSFWorkbook(inputStream); //2003-
                } else if (excel2007U.equals(fileType)) {
                    return new XSSFWorkbook(inputStream); //2007+
                } else {
                    throw new Exception();
                }
            } catch (Exception e) {
                throw new Exception("解析的文件格式有误!");
            }
        }
    
        protected abstract void parseTitles(int rowIndex) throws Exception;
    
        protected String getCellStringValue(Cell cell) throws Exception {
            String cellValue = "";
            if (cell == null) {
                return cellValue;
            } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                cellValue = cell.getStringCellValue();
            } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    double d = cell.getNumericCellValue();
                    Date date = HSSFDateUtil.getJavaDate(d);
                    cellValue = simpleDateFormat.format(date);
                } else {
                    cellValue = decimalFormat.format((cell.getNumericCellValue()));
                }
            } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                cellValue = "";
            } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                cellValue = String.valueOf(cell.getBooleanCellValue());
            } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
                cellValue = "";
            } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                cellValue = cell.getCellFormula();
            }
           
            return cellValue.trim();
        }
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67072.html

相关文章

  • Java对象和Excel转换工具XXL-EXCEL

    摘要:一行代码完成对象和之间的转换。说明属性列名称四版本更新日志版本,新特性导出支持对象装换为,并且支持字节数组等多种导出方式导入支持转换为对象,并且支持文件路径等多种导入方式版本,新特性字段支持类型。 《Java对象和Excel转换工具XXL-EXCEL》 showImg(https://segmentfault.com/img/remote/1460000012470335);showI...

    mj 评论0 收藏0
  • java 导出 excel 最佳实践,java 大文件 excel 避免OOM(内存溢出) exce

    摘要:消费之后,多线程处理文件导出,生成文件后上传到等文件服务器。前端直接查询并且展现对应的任务执行列表,去等文件服务器下载文件即可。这客户体验不友好,而且网络传输,系统占用多种问题。拓展阅读导出最佳实践框架 产品需求 产品经理需要导出一个页面的所有的信息到 EXCEL 文件。 需求分析 对于 excel 导出,是一个很常见的需求。 最常见的解决方案就是使用 poi 直接同步导出一个 exc...

    K_B_Z 评论0 收藏0
  • POI技术—用于java开发解析excel的抽象

    摘要:单元格各类型数据读取基本类型处理的数据包括字符型数据,数字日期公式等。下面是单元格类型说明实例解析中数据,要求转换为文本方式存储写一个解析的抽象类版本的版本的解析的文件格式有误 1.单元格各类型数据读取 1.1 基本类型 处理的Excel数据包括字符型数据,数字、日期、公式等。 下面是单元格类型说明: showImg(https://segmentfault.com/img/bVMjd...

    xfee 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<