zoukankan      html  css  js  c++  java
  • [NPOI2.0] 使用NPOI读取和导出Excel文件

    先来说下一直使用的 Microsoft.Office.Interop.Excel ,在系统正式使用时遇到的问题

    1:Excel读取数据BUG

    由于使用Microsoft.Office.Interop.Excel 出现读取数据的Bug,导致经常要为此问题浪费时间去向客户解释原因和指导客户如何处理Excel文档来适应读取的Bug问题。

    2:客户环境部署

    客户环境(多为工厂机器)没有安装 Mircosoft Office(600M以上)或 AccessDatabaseEngine(20M左右),部署时候需要至少5分钟进行读取Excel引擎的安装。

    3:解决了 32位、64位 "未在本机上注册Mircosoft.ACE.OLEDB 1X.0" 的系统所安装Excel的版本问题

    由于客户环境各不相同,有64位系统,但安装 32位的 Office 软件;亦有64位系统,安装 64位系统的 Office软件;

    使用NPOI可以解决以上3个问题。

    =========================

    以下我列出 在NPOI中遇到的难点,

    其他的代码 我会发出整个项目到百度云盘 : http://pan.baidu.com/s/1mgwxexM

    一、NPOI 基本类

    IWorkbook -- 一般读取什么文件就使用下列哪种类
    |__ HSSFWorkbook -- 对应于 XLS
    |__ XSSFWorkbook -- 对应于 XLSX

    ICell -- 单元格基本使用次类,本人暂未使用下面细分的2种单元格

    |__ HSSFCell -- XLS

    |__ XSSFCell -- XLSX

    二、NPOI对于读取 数值 或 日期

    由于NPOI对读取单元格中日期(时间)和数值都使用了 CellType.Numeric,故在对日期(时间)的读取本人加上了以下的判断。

     1 private static object GetValueType(ICell cell)
     2  {
     3             if (cell == null)
     4             {
     5                 return null;
     6             }
     7 
     8             switch (cell.CellType)
     9             {
    10                 case CellType.Blank:
    11                     return null;
    12                 case CellType.Boolean:
    13                     return cell.BooleanCellValue;
    14                 case CellType.String:
    15                     return cell.StringCellValue;
    16                 case CellType.Error:
    17                     return cell.ErrorCellValue;
    18                 case CellType.Formula:
    19                     return cell.NumericCellValue;
    20                 //return cell.CellFormula; // 少数情况下 会直接读取公式
    21 
    22                 case CellType.Numeric: // 缺点 NPOI对日期处理比较薄弱
    23                     {
    24                         short format = cell.CellStyle.DataFormat;
    25                         if ( // TODO : 继续研究下获取日期 or 数值 !!
    26                              // 可能不同文件自定义不同
    27                             format == 14 || // 日期 yyyy-m-d
    28                             format == 22 || // 自定义 yyyy-m-d h:mm
    29                             format == 31 ||
    30                             format == 57 ||
    31                             format == 58 ||
    32                             format == 177 || // 自定义 yyyy年y月d日
    33                             format == 178 // 自定义 yyyy-mm-dd hh:mm:ss
    34                             )
    35                         { return cell.DateCellValue; }
    36                         else { return cell.NumericCellValue; }
    37                     }
    38                 default:
    39                     return "=" + cell.CellFormula;
    40             }
    41 }
  • 相关阅读:
    把影响集中到一个点
    How to avoid Over-fitting using Regularization?
    适定性问题
    Numerical Differentiation 数值微分
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    Generally a good method to avoid this is to randomly shuffle the data prior to each epoch of training.
    What is the difference between iterations and epochs in Convolution neural networks?
    Every norm is a convex function
    Moore-Penrose Matrix Inverse 摩尔-彭若斯广义逆 埃尔米特矩阵 Hermitian matrix
    perl 类里的函数调用其他类的函数
  • 原文地址:https://www.cnblogs.com/howesdomo/p/4453791.html
Copyright © 2011-2022 走看看