本文转自: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. 看了一本Unity3D的教程

    国内写的<Unity 3D游戏开发>. 实例挺多,对于有基础的人来说,上手会挺快的: 但进阶的东西没有涉及,可能与书的定位有关吧

  2. 利用PBfunc在Powerbuilder中使用https获取微信的AccessToken

    在前篇中讲解了使用PBFunc在Powerbuilder自己进行http的GET和POST操作. 本篇简单用代码演示下https的微信AccessToken的获取: n_pbfunc_http lnv ...

  3. ahjesus sql手动重新更新ID

    declare cus_cursor cursor scroll for SELECT Id from [dbo].[TLotterySpiderRule] open cus_cursor decla ...

  4. .NET Core 和 ASP.NET 5 RC1 发布

    昨天微软发布了 .NET Core 和 ASP.NET 5 候选版本,支持 Windows,Linux 和 OS X 平台,版本 License 为 "Go Live",,也就是说 ...

  5. [vim] vim入门

    1. 概述 工欲善其事 必先利其器.vim是非常好用的文本编辑器,可以将它看作是vi的进阶.绝大多数Unix系统都会内置vi编辑器,vi是文本编辑器,vim是程序编辑器.相比vi,它可以根据文件的类型 ...

  6. SQL数据库基础(四)

    聚合函数:sum,avg,max,min,count 使用方法示例: group by  分组的使用方法 分组的练习: 数学函数:ABS.ceiling.floor.power.round.sqrt. ...

  7. sass开发过程中遇到的几个坑

    1.安装sass被墙的问题 安装完`ruby`后,打开`ruby cmd` 输入`gem install sass`,安装失败,有可能是镜像源的问题,也有可能是墙的问题. 因为公司内网的奇葩限制,各种 ...

  8. 任意类型转换为IntPtr

    之前,将数组.结构体等转换为IntPtr使用的是Marshal.Copy().Marshal.StructureToPtr(),但是有个问题自定义的结构体数组没法这样转化,一般网上给出的解决方法就是通 ...

  9. GetStartedWithWin10Develop

    GetStartedWithWin10Develop 首先要确保已经配置好win10开发环境,开始第一个win10开发的HelloWorld 1.首先创建你的win10项目(示例的项目名称为 Hell ...

  10. C#实现图标批量下载

    本文略微有些长,花了好几晚时间编辑修改,若在措辞排版上有问题,请谅解.本文共分为四篇,下面是主要内容,也是软件开发基本流程. 阶段 描述 需求分析 主要描述实现本程序的目的及对需求进行分析,即为什么要 ...