本文转自:https://github.com/tonyqus/npoi/blob/master/testcases/main/SS/Formula/TestFunctionRegistry.cs

  /*
  *  ====================================================================
  *    Licensed to the Apache Software Foundation (ASF) under one or more
  *    contributor license agreements.  See the NOTICE file distributed with
  *    this work for additional information regarding copyright ownership.
  *    The ASF licenses this file to You under the Apache License, Version 2.0
  *    (the "License"); you may not use this file except in compliance with
  *    the License.  You may obtain a copy of the License at
  *
  *        http://www.apache.org/licenses/LICENSE-2.0
  *
  *    Unless required by applicable law or agreed to in writing, software
  *    distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *    See the License for the specific language governing permissions and
  *    limitations under the License.
  * ====================================================================
  */
   
  using System;
  using NPOI.HSSF.UserModel;
  using NPOI.SS.Formula;
  using NPOI.SS.Formula.Atp;
  using NPOI.SS.Formula.Eval;
  using NPOI.SS.Formula.Functions;
  using NPOI.SS.UserModel;
  using NUnit.Framework;
   
  namespace TestCases.SS.Formula
  {
      /**
       *
       * @author Yegor Kozlov
       */
      [TestFixture]
      public class TestFunctionRegistry
      {
          [Test]
          public void TestRegisterInRuntime()
          {
              HSSFWorkbook wb = new HSSFWorkbook();
              HSSFSheet sheet = (HSSFSheet)wb.CreateSheet("Sheet1");
              HSSFRow row = (HSSFRow)sheet.CreateRow(0);
              HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
   
              HSSFCell cellA = (HSSFCell)row.CreateCell(0);
              cellA.CellFormula = ("FISHER(A5)");
              CellValue cv;
              try
              {
                  //NPOI
                  //Run it twice in NUnit Gui Window, the first passed but the second failed.
                  //Maybe the function was cached. Ignore it.
                  cv = fe.Evaluate(cellA);
                  Assert.Fail("expectecd exception");
              }
              catch (NotImplementedException)
              {
                  ;
              }
   
              FunctionEval.RegisterFunction("FISHER", new Function1());/*Function() {
              public ValueEval Evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
                  return ErrorEval.NA;
              }
          });*/
   
              cv = fe.Evaluate(cellA);
              Assert.AreEqual(ErrorEval.NA.ErrorCode, cv.ErrorValue);
   
              HSSFCell cellB = (HSSFCell)row.CreateCell(1);
              cellB.CellFormula = ("CUBEMEMBERPROPERTY(A5)");
              try
              {
                  cv = fe.Evaluate(cellB);
                  Assert.Fail("expectecd exception");
              }
              catch (NotImplementedException)
              {
                  ;
              }
   
              AnalysisToolPak.RegisterFunction("CUBEMEMBERPROPERTY", new FreeRefFunction1());/*FreeRefFunction() {
              public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) {
                  return ErrorEval.NUM_ERROR;
              }
          });*/
   
              cv = fe.Evaluate(cellB);
              Assert.AreEqual(ErrorEval.NUM_ERROR.ErrorCode, cv.ErrorValue);
          }
   
          private class Function1 : NPOI.SS.Formula.Functions.Function
          {
              public ValueEval Evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex)
              {
                  return ErrorEval.NA;
              }
          }
   
          private class FreeRefFunction1 : FreeRefFunction
          {
              public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec)
              {
                  return ErrorEval.NUM_ERROR;
              }
          }
   
          class Function2 : NPOI.SS.Formula.Functions.Function
          {
              public ValueEval Evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex)
              {
                  return ErrorEval.NA;
              }
          }
          [Test]
          public void TestExceptions()
          {
              NPOI.SS.Formula.Functions.Function func = new Function2();
              try
              {
                  FunctionEval.RegisterFunction("SUM", func);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("POI already implememts SUM" +
                          ". You cannot override POI's implementations of Excel functions", e.Message);
              }
              try
              {
                  FunctionEval.RegisterFunction("SUMXXX", func);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("Unknown function: SUMXXX", e.Message);
              }
              try
              {
                  FunctionEval.RegisterFunction("ISODD", func);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("ISODD is a function from the Excel Analysis Toolpack. " +
                          "Use AnalysisToolpack.RegisterFunction(String name, FreeRefFunction func) instead.", e.Message);
              }
   
              FreeRefFunction atpFunc = new FreeRefFunction2();/*FreeRefFunction() {
              public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) {
                  return ErrorEval.NUM_ERROR;
              }
          };*/
              try
              {
                  AnalysisToolPak.RegisterFunction("ISODD", atpFunc);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("POI already implememts ISODD" +
                          ". You cannot override POI's implementations of Excel functions", e.Message);
              }
              try
              {
                  AnalysisToolPak.RegisterFunction("ISODDXXX", atpFunc);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("ISODDXXX is not a function from the Excel Analysis Toolpack.", e.Message);
              }
              try
              {
                  AnalysisToolPak.RegisterFunction("SUM", atpFunc);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("SUM is a built-in Excel function. " +
                          "Use FunctoinEval.RegisterFunction(String name, Function func) instead.", e.Message);
              }
          }
          class FreeRefFunction2 : FreeRefFunction
          {
              public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec)
              {
                  return ErrorEval.NUM_ERROR;
              }
          }
      }
   
  }

[转]NPOI TestFunctionRegistry.cs的更多相关文章

  1. NPOI 在指定单元格导入导出图片

    NPOI 在指定单元格导入导出图片 Intro 我维护了一个 NPOI 的扩展,主要用来导入导出 Excel 数据,最近有网友提出了导入 Excel 的时候解析图片的需求,于是就有了本文的探索 导入E ...

  2. NPOIHelper.cs (NPOI 2.1.1)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  3. C#利用NPOI处理excel的类 NPOIHelper.cs

    个人的NPOIHelp类,包括datatable导出到excel,dataset导出到excel,excel导入到datatable,excel导入到dataset, 更新excel中的数据,验证导入 ...

  4. (C#)使用NPOI导出Excel

    在做业务型的软件时,经常需要将某些数据导出,本文介绍了在Winform或Asp.net中使用NPOI(POI 项目的 .NET 版本)来操作Excel文件,而无需安装Office. 首先,需要获取NP ...

  5. 使用NPOI从Excel中提取图片及图片位置信息

    问题背景: 话说,在ExcelReport的开发过程中,有一个比较棘手的问题:怎么复制图片呢? 当然,解决这个问题的第一步是:能使用NPOI提取到图片及图片的位置信息.到这里,一切想法都很顺利.但NP ...

  6. 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility

    1. ExcelUtility功能:  1.将数据导出到EXCEL(支持XLS,XLSX,支持多种类型模板,支持列宽自适应)  类名:ExcelUtility. Export  2.将EXCEL ...

  7. ASP.NET使用NPOI加载Excel模板并导出下载

    1.为什么要使用NPOI导出Excel? 一.解决传统操作Excel遇到的问题: 如果是.NET,需要在服务器端装Office,且及时更新它,以防漏洞,还需要设定权限允许.NET访问COM+,如果在导 ...

  8. 使用NPOI将TABLE内容导出到EXCEL

    项目中需要将页面中的table内容导出到EXCEL,在用了几种方法后发现NPO是最快&最好的 需要应用 NPOI.dll 还有个Ionic.Zip.dll不知道有用没,没去研究,两个DLL都放 ...

  9. NPOI 读写Excel

    实例功能概述: 1.支持Excel2003以及2007 2.支持Excel读取到DataTable(TableToExcel) 3.支持DataTable导出到Excel(TableToExcel) ...

随机推荐

  1. spring注解配置启动过程

    最近看起spring源码,突然想知道没有web.xml的配置,spring是怎么通过一个继承于AbstractAnnotationConfigDispatcherServletInitializer的 ...

  2. [moka同学笔记]window下redis的安装以及php-redis详细配置(摘录)

    (注意对应的版本)下载地址:https://github.com/phpredis/phpredis/downloads 首先下载redis安装,windows下安装软件都是下一步下一步over,就不 ...

  3. Android5.0新特性——Material Design简介

    Material Design Material Design简介 Material Design是谷歌新的设计语言,谷歌希望寄由此来统一各种平台上的用户体验,Material Design的特点是干 ...

  4. C#枚举类型和结构体

    注意:枚举类型和结构体都属于值类型. 结构体:就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样. 一.定义的方法: struct student { public int nianl ...

  5. [WP8] Binding时,依照DataType选择DataTemplate

    [WP8] Binding时,依照DataType选择DataTemplate 范例下载 范例程序代码:点此下载 问题情景 在开发WPF.WP8...这类应用程序的时候,透过Binding机制搭配Da ...

  6. swift基础一

    // swift中导入类库使用import,不再使用<>和"" import Foundation // 输出 print("Hello, World!&qu ...

  7. 当碰到非ARC写的文件时在ARC环境下运行报错时解决办法

  8. Android线程管理(三)——Thread类的内部原理、休眠及唤醒

    线程通信.ActivityThread及Thread类是理解Android线程管理的关键. 线程,作为CPU调度资源的基本单位,在Android等针对嵌入式设备的操作系统中,有着非常重要和基础的作用. ...

  9. javascript之工厂方式定义对象

    每一个函数对象都有一个length属性,表示该函数期望接收的参数个数. <html> <head> <script type="text/javascript& ...

  10. Peer-to-Peer 综述

    Peer-To-Peer 网络介绍 最近几年,Peer-to-Peer (对等计算,简称P2P) 迅速成为计算机界关注的热门话题之一,财富杂志更将P2P列为影响Internet未来的四项科技之一. “ ...