NPOI 笔记

前言

文档:http://npoi.codeplex.com/documentation

示例:https://npoi.svn.codeplex.com/svn/

下载:https://www.nuget.org/packages/NPOI/

版本:2.2.1 (通过nuget进行安装的)

 应用场景:https://github.com/zhaoqingqing/TableML  解析Excel数据成tsv(类似csv)

基础知识

workbook(工作薄):一个excel文件可以包含多张表(sheet)

image

行:Row

列:Column

单元格:Cell

 

 

NPOI Cannot get a text value from a numeric formula cell

当单元格的内容是由公式组成时,获取值时报以下Error:

未经处理的异常:  System.InvalidOperationException: Cannot get a text value from a numeric formula cell
   在 NPOI.XSSF.UserModel.XSSFCell.get_RichStringCellValue()
   在 NPOI.XSSF.UserModel.XSSFCell.get_StringCellValue()

image

解决方法:

通过上面的调试信息可以看到 cell.CachedFormulaResultType 有表示这个公式单元格的类型

switch (cell.CachedFormulaResultType)
{
    //已测试:SUM,& 
        case CellType.Numeric:
            return cell.NumericCellValue.ToString();
        case  CellType.String:
            return cell.StringCellValue;
}

posted @ 2018-02-08 16:35  赵青青  阅读(1680)  评论(1编辑  收藏  举报