[转]NPOI TestFunctionRegistry.cs
本文转自: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的更多相关文章
- NPOI 在指定单元格导入导出图片
		NPOI 在指定单元格导入导出图片 Intro 我维护了一个 NPOI 的扩展,主要用来导入导出 Excel 数据,最近有网友提出了导入 Excel 的时候解析图片的需求,于是就有了本文的探索 导入E ... 
- NPOIHelper.cs (NPOI 2.1.1)
		using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ... 
- C#利用NPOI处理excel的类 NPOIHelper.cs
		个人的NPOIHelp类,包括datatable导出到excel,dataset导出到excel,excel导入到datatable,excel导入到dataset, 更新excel中的数据,验证导入 ... 
- (C#)使用NPOI导出Excel
		在做业务型的软件时,经常需要将某些数据导出,本文介绍了在Winform或Asp.net中使用NPOI(POI 项目的 .NET 版本)来操作Excel文件,而无需安装Office. 首先,需要获取NP ... 
- 使用NPOI从Excel中提取图片及图片位置信息
		问题背景: 话说,在ExcelReport的开发过程中,有一个比较棘手的问题:怎么复制图片呢? 当然,解决这个问题的第一步是:能使用NPOI提取到图片及图片的位置信息.到这里,一切想法都很顺利.但NP ... 
- 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility
		1. ExcelUtility功能:  1.将数据导出到EXCEL(支持XLS,XLSX,支持多种类型模板,支持列宽自适应)  类名:ExcelUtility. Export  2.将EXCEL ... 
- ASP.NET使用NPOI加载Excel模板并导出下载
		1.为什么要使用NPOI导出Excel? 一.解决传统操作Excel遇到的问题: 如果是.NET,需要在服务器端装Office,且及时更新它,以防漏洞,还需要设定权限允许.NET访问COM+,如果在导 ... 
- 使用NPOI将TABLE内容导出到EXCEL
		项目中需要将页面中的table内容导出到EXCEL,在用了几种方法后发现NPO是最快&最好的 需要应用 NPOI.dll 还有个Ionic.Zip.dll不知道有用没,没去研究,两个DLL都放 ... 
- NPOI 读写Excel
		实例功能概述: 1.支持Excel2003以及2007 2.支持Excel读取到DataTable(TableToExcel) 3.支持DataTable导出到Excel(TableToExcel) ... 
随机推荐
- js 当前日期及时间
			返回时间格式 : 2016-07-22 10:22:30 function getNowFormatDate() { var date = new Date(); var seperator1 = & ... 
- http服务&ajax编程
			http服务&ajax编程 1.服务器 前言:通俗的讲,能够提供某种服务的机器(计算机)称为服务器 1.1.服务器类型 按照不同的划分标准,服务可划分为以下类型: 按服务类型可分为:文件服务器 ... 
- SAP_Web_Service开发配置
			第一章 SAP创建WS 1.1 概要技术说明 1.2 创建RFC函数 1.3 创建WS 1.4 外部系统访问配置 第二章 SAP调用WS 2 ... 
- This task is currently locked by a running workflow and cannot be edited
			转自:http://geek.hubkey.com/2007/09/locked-workflow.html 转自:http://blogs.code-counsel.net/Wouter/Lists ... 
- 桥牌笔记L4D17:小心阻塞
			南打3NT. 西的首攻会有3墩黑桃.3墩方块.2付梅花,共8墩到手.看来方块如果3-2分布的话,非常容易就能超一完成. 所以要想着4-1分布的安全打法. 第一墩庄家拿了黑桃J后,明手的黑桃A会阻塞,庄 ... 
- JSON/XML格式化插件比较
			一.引子 Chrome工具里面有很多json格式化的插件,可以让杂乱的json内容变得有序,我们先来看看效果: 正常情况下: 格式化后: 规整多了吧! 二.工具分享+比对 1.JSON Formatt ... 
- android 回调函数一:基本概念
			1.概念 客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数. 一般说来,C不会自己调用B,C提供B的目的就是让S来调用它,而且是C ... 
- UWP开发-重新理解MVVM
			MVVM是一个比较热门的开发框架,尽管已经出现很久了,仍然比较受欢迎.MVVM框架包括: M:Model:Model指的是数据模型,例如你要在页面展示联系人信息,那么Model就是联系人的模型,包括联 ... 
- HDFS主要特性和体系结构
			引言 Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有很多共同点.但同时,它和其他的分布式文件系统 ... 
- IOS之UI--小实例项目--添加商品和商品名(纯代码终结版)
			前言:这个小实例项目是完完全全以MJ视频传授的优化方案一步一个思路从零开始敲出代码的,而且每一步都有思路,都有逻辑所以然.敲代码讲究思路,我个人不建议记忆太多东西,反正我记性很差的. 小贴士:文章末尾 ... 
