本文转自: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. 四项技术 助你提高SQL Server的性能

    有时,为了让应用程序运行得更快,所做的全部工作就是在这里或那里做一些很小调整.但关键在于确定如何进行调整!迟早您会遇到这种情况:应用程序中的 SQL 查询不能按照您想要的方式进行响应.它要么不返回数据 ...

  2. Mysql –>EF edmx(model first)–> Sql server table

    一.mysql environment When we create an new database,first We need draw er diagram for somebody to sho ...

  3. 回文串---吉哥系列故事——完美队形II

    HDU  4513 Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出 ...

  4. Spring核心概念之AOP

    一.AOP 的概念 AOP(Aspect Oriented Programming)的缩写,面向切面编程,主要作用就是对代码进行增强处理. 理解面向切面编程的含义:就是在不改变原有程序的基础上为代码增 ...

  5. PHP生成图片验证码demo【OOP面向对象版本】

    下面是我今天下午用PHP写的一个生成图片验证码demo,仅供参考. 这个demo总共分为4个文件,具体代码如下: 1.code.html中的代码: <!doctype html> < ...

  6. 多准则决策模型-TOPSIS方法

    多准则决策–Multiple Criteria Decision Making 多准则决策–Multiple Criteria Decision Making 多准则决策是指在具有相互冲突.不可共度的 ...

  7. js 中{},[]中括号,大括号

    1. { } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或是函数. 如: var LangShen = {"Name":"Langshen",& ...

  8. SharePoint 2013 手动删除爬网项目

    本文介绍如何手动删除某些搜索项目,其实删除搜索项目并不常用,主要还是在刚刚完成爬网,就删除了某些项目,然后有比较敏感需要马上删除的时候.下面,就跟着图文简单了解下手动删除已爬网的项目吧. 1.配置好搜 ...

  9. SharePoint 2013 中的SQL Server 安全

    使用SharePoint很长时间以来,都认为Sql只需要最初始的配置,即不再需要管理和维护:而事实上,Sql的管理和安全,都是和SharePoint环境的稳定性息息相关的,所以,要绝对重视ShareP ...

  10. Mybatis学习记录(七)----Mybatis查询缓存

    1. 什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存. 一级缓存是SqlSession级别的缓存.在操作数据库时需要构造 sql ...