From 2344fa97c48335f24046bade80aeba1ae6a70690 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Tue, 26 Mar 2019 12:22:09 -0400 Subject: [PATCH] Add basic maven pom.xml and test --- .gitignore | 1 + pom.xml | 109 ++++++++++++++++++++++++++ src/test/java/CalculationTest.java | 118 +++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 pom.xml create mode 100644 src/test/java/CalculationTest.java diff --git a/.gitignore b/.gitignore index 7445b5d91..fe5b32e0f 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ bin out .idea .gradle +target diff --git a/pom.xml b/pom.xml new file mode 100644 index 000000000..4e31b9b7b --- /dev/null +++ b/pom.xml @@ -0,0 +1,109 @@ + + + + + + 4.0.0 + com.moparisthebest.poi + poi-fast-calc + 3.16 + jar + Apache POI + http://poi.apache.org/ + Apache POI - Java API To Access Microsoft Format Files + + + + POI Users List + user-subscribe@poi.apache.org + user-unsubscribe@poi.apache.org + http://mail-archives.apache.org/mod_mbox/poi-user/ + + + POI Developer List + dev-subscribe@poi.apache.org + dev-unsubscribe@poi.apache.org + http://mail-archives.apache.org/mod_mbox/poi-dev/ + + + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + Apache Software Foundation + http://www.apache.org/ + + + + + commons-logging + commons-logging + 1.2 + provided + true + + + log4j + log4j + 1.2.17 + runtime + true + + + commons-codec + commons-codec + 1.10 + + + + org.hamcrest + hamcrest-core + test + 1.3 + + + junit + junit + test + 4.12 + + + org.apache.commons + commons-collections4 + 4.1 + + + + + src/java + + + src/resources/main + + + + diff --git a/src/test/java/CalculationTest.java b/src/test/java/CalculationTest.java new file mode 100644 index 000000000..66f12d187 --- /dev/null +++ b/src/test/java/CalculationTest.java @@ -0,0 +1,118 @@ +//package org.apache.poi.hssf.usermodel.examples; + +import org.junit.Test; +import org.apache.poi.ss.SpreadsheetVersion; +import org.apache.poi.ss.usermodel.*; +//import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +public class CalculationTest { + public static void main(String[] args) { + new CalculationTest().test(); + } + + @Test + public void test() { + runAssert(5000, 519.4700000000496); + runAssert(20000, 2019.469999999259); + runAssert(65536, 6573.070000006382); + runAssert(65537, 6573.170000006382); + runAssert(65538, 6573.270000006382); + runAssert(65539, 6573.370000006383); + runAssert(100000, 10019.470000018919); + runAssert(SpreadsheetVersion.EXCEL2007.getLastRowIndex() - 5, 104876.47000161673); + runAssert(SpreadsheetVersion.EXCEL2007.getLastRowIndex(), 104876.97000161676); + runAssert(SpreadsheetVersion.EXCEL2007.getMaxRows(), 104877.07000161677); + //runAssert(SpreadsheetVersion.EXCEL2007.getMaxRows() + 1, 104877.07000161677); // java.lang.IllegalArgumentException: Invalid row number (1048576) outside allowable range (0..1048575) + } + + /* + public static Workbook newXSSFWorkbookNoFormulaValidation() { + final org.apache.poi.xssf.usermodel.XSSFWorkbook wb = new XSSFWorkbook(); + wb.setCellFormulaValidation(false); + return wb; + } + */ + + public static void runAssert(final int maxRow, final double expected) { + //runAssert(new org.apache.poi.hssf.usermodel.GenericSSEvaluationWorkbook(), maxRow, expected); + runAssert(new org.apache.poi.hssf.usermodel.HSSFWorkbook(SpreadsheetVersion.EXCEL2007), maxRow, expected); + if(maxRow <= SpreadsheetVersion.EXCEL97.getMaxRows()) + runAssert(new org.apache.poi.hssf.usermodel.HSSFWorkbook(), maxRow, expected); + //runAssert(newXSSFWorkbookNoFormulaValidation(), maxRow, expected); + } + + public static void runAssert(final Workbook wb, final int maxRow, final double expected) { + + final FormulaEvaluator formulaEval = wb.getCreationHelper().createFormulaEvaluator(); + final Sheet sheet = wb.createSheet(); + int rowNum = -1, cellNum; + Row row; + Cell formulaCell; + CellValue evaluatedCell = null; + + row = sheet.createRow(++rowNum); + cellNum = -1; + row.createCell(++cellNum).setCellValue("Product1"); + row.createCell(++cellNum).setCellValue(10.67); + row.createCell(++cellNum).setCellValue(8); + + double price = 19.57; + int quantity = 3; + + final int maxRows = maxRow - 1; // because we created 1 above + + final long start = System.currentTimeMillis(); + + //rowNum = maxRow - 3; + //for (int _x = maxRow - 3; _x < maxRows; ++_x) { + for (int _x = 0; _x < maxRows; ++_x) { + row = sheet.createRow(++rowNum); + cellNum = -1; + final int rowNumUse = rowNum + 1; + row.createCell(++cellNum).setCellValue("Product" + rowNumUse); + price += 0.1; + ++quantity; + row.createCell(++cellNum).setCellValue(price); + row.createCell(++cellNum).setCellValue(quantity); + + formulaCell = row.createCell(++cellNum); + //formulaCell.setCellFormula("VLOOKUP(\"Product" + rowNum + "\",$A:$C,2,0)"); + formulaCell.setCellFormula("VLOOKUP(\"Product" + rowNumUse + "\",A" + rowNumUse + ":C" + rowNumUse + ",2,0)"); + //formulaCell.setCellFormula("B" + rowNumUse); + //System.out.println("formulaCell: " + formulaCell); + + evaluatedCell = formulaEval.evaluate(formulaCell); + //System.out.println("evaluatedCell: " + evaluatedCell); + if (evaluatedCell == null || evaluatedCell.getNumberValue() != price) { + throw new AssertionError(String.format("Expected %f but got %s at rowNum: %d, rowNumUse: %d", price, evaluatedCell, rowNum, rowNumUse)); + //System.out.println(String.format("Expected %f but got %s at rowNum: %d, rowNumUse: %d", price, evaluatedCell, rowNum, rowNumUse)); + //break; + } + } + + //System.out.println("evaluatedCell: " + evaluatedCell); + + final long elapsed = System.currentTimeMillis() - start; + System.out.printf("impl: %s, maxRow: %d, elapsed: %dms, %ds, %dmin%n", wb.getClass().getSimpleName(), maxRow, elapsed, + elapsed / 1000, elapsed / 1000 / 60); + + if (evaluatedCell == null || evaluatedCell.getNumberValue() != expected) { + throw new AssertionError("Expected " + expected + " but got " + evaluatedCell); + //System.out.println("Expected " + expected + " but got " + evaluatedCell); + } + + /* + if(wb instanceof XSSFWorkbook) { + try (FileOutputStream fos = new FileOutputStream("test.xlsx")) { + wb.write(fos); + } catch (Throwable e) { + e.printStackTrace(); + } + } + */ + } +}