|
@@ -1,6 +1,7 @@
|
|
|
package com.ims.common.utils;
|
|
|
|
|
|
import com.ims.common.function.IFSFunction;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.poi.hssf.usermodel.*;
|
|
|
import org.apache.poi.ss.formula.FormulaParseException;
|
|
|
import org.apache.poi.ss.formula.functions.FreeRefFunction;
|
|
@@ -12,17 +13,24 @@ import org.apache.poi.ss.usermodel.*;
|
|
|
import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
+@Slf4j
|
|
|
public class FormulaUtils {
|
|
|
|
|
|
+ private static Workbook workbook = null;
|
|
|
|
|
|
+ static {
|
|
|
+ if (workbook == null) {
|
|
|
+ workbook = new HSSFWorkbook();
|
|
|
+ }
|
|
|
|
|
|
+ String[] functionNames = { IFSFunction.FUNCTION_NAME } ;
|
|
|
+ FreeRefFunction[] functionImpls = { new IFSFunction() } ;
|
|
|
|
|
|
- /**
|
|
|
- * Sheet 中的每一行
|
|
|
- */
|
|
|
- private static HSSFRow row = null;
|
|
|
+ UDFFinder udfs = new DefaultUDFFinder( functionNames, functionImpls ) ;
|
|
|
+ UDFFinder udfToolpack = new AggregatingUDFFinder( udfs ) ;
|
|
|
|
|
|
- private static FormulaEvaluator formulaEvaluator = null;
|
|
|
+ workbook.addToolPack(udfToolpack);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 计算值
|
|
@@ -30,7 +38,7 @@ public class FormulaUtils {
|
|
|
* @param formula Excel 中的公式,例如:MAX(56-FLOOR(20/6,1),2)
|
|
|
* @return
|
|
|
*/
|
|
|
- public static double caculateFormula1(String formula) {
|
|
|
+ public static double calculateFormula1(String formula) {
|
|
|
if(formula.startsWith("=")){
|
|
|
formula =formula.replace("=","");
|
|
|
}
|
|
@@ -39,8 +47,8 @@ public class FormulaUtils {
|
|
|
|
|
|
HSSFSheet sheet = workbook.createSheet();
|
|
|
|
|
|
- row = sheet.createRow(0);
|
|
|
- formulaEvaluator = new HSSFFormulaEvaluator(workbook);
|
|
|
+ HSSFRow row = sheet.createRow(0);
|
|
|
+ FormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator(workbook);
|
|
|
|
|
|
// 这里必须新建一个对象,否则只有第一个 formula 才有效。查看 formulaEvaluator.evaluate 的源码。
|
|
|
HSSFCell cell = row.createCell(0);
|
|
@@ -52,23 +60,18 @@ public class FormulaUtils {
|
|
|
} catch (FormulaParseException e) {
|
|
|
System.out.print(e.getMessage());
|
|
|
return 0;
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ workbook.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static double caculateFormula(String formula){
|
|
|
- Workbook workbook = null;
|
|
|
+ public static double calculateFormula(String formula){
|
|
|
try {
|
|
|
- workbook = new HSSFWorkbook();
|
|
|
-
|
|
|
- String[] functionNames = { IFSFunction.FUNCTION_NAME } ;
|
|
|
- FreeRefFunction[] functionImpls = { new IFSFunction() } ;
|
|
|
-
|
|
|
- UDFFinder udfs = new DefaultUDFFinder( functionNames, functionImpls ) ;
|
|
|
- UDFFinder udfToolpack = new AggregatingUDFFinder( udfs ) ;
|
|
|
-
|
|
|
- workbook.addToolPack(udfToolpack);
|
|
|
-
|
|
|
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
|
|
|
|
|
|
Sheet sheet = workbook.createSheet();
|
|
@@ -81,14 +84,7 @@ public class FormulaUtils {
|
|
|
return new BigDecimal(value).setScale(2, BigDecimal.ROUND_CEILING).doubleValue();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
- }finally {
|
|
|
- try {
|
|
|
- if(null != workbook){
|
|
|
- workbook.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ log.error("FormulaUtils==excel公式解析异常: ", e.getMessage());
|
|
|
|
|
|
}
|
|
|
|