Commit 3c25556f authored by frank.xa.zhang's avatar frank.xa.zhang

POI test file

parent a7a8d2f0
...@@ -16,7 +16,7 @@ import java.util.List; ...@@ -16,7 +16,7 @@ import java.util.List;
@RestController @RestController
@RequestMapping(value = "api/v1/Report") @RequestMapping(value = "api/v1/Report")
public class ReportController { public class ReportController {
@Autowired @Autowired
ReportService reportService; ReportService reportService;
@RequestMapping(value = "template/{organizationID}/{period}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @RequestMapping(value = "template/{organizationID}/{period}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
......
...@@ -19,20 +19,26 @@ public class POITest { ...@@ -19,20 +19,26 @@ public class POITest {
try { try {
FileInputStream fis = new FileInputStream(workbookFile); FileInputStream fis = new FileInputStream(workbookFile);
Workbook workbook = WorkbookFactory.create(fis); Workbook workbook = WorkbookFactory.create(fis);
String[] functionNames = {"TmpFunction"}; String[] functionNames = {"TmpFunction"};
FreeRefFunction[] functionImpls = {new TmpFunction()}; FreeRefFunction[] functionImpls = {new TmpFunction()};
UDFFinder udfs = new DefaultUDFFinder(functionNames, functionImpls); UDFFinder udfs = new DefaultUDFFinder(functionNames, functionImpls);
UDFFinder udfToolpack = new AggregatingUDFFinder(udfs); UDFFinder udfToolpack = new AggregatingUDFFinder(udfs);
workbook.addToolPack(udfToolpack); workbook.addToolPack(udfToolpack);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
int sheetNum = workbook.getNumberOfSheets(); int sheetNum = workbook.getNumberOfSheets();
Sheet st1 = workbook.getSheetAt(0); Sheet st1 = workbook.getSheetAt(0);
for (int i = 0; i < 10; i++) {
if (st1.getRow(i) == null) {
st1.createRow(i);
for (int j = 0; j < 10; j++) {
if (st1.getRow(i).getCell(j) == null) {
st1.getRow(i).createCell(j).setCellFormula("TmpFunction(\"g2\","+j+")+B5-TmpFunction(\"g2\","+i+")");
}
}
}
}
st1.getRow(1).getCell(0).setCellType(CellType.NUMERIC); st1.getRow(1).getCell(0).setCellType(CellType.NUMERIC);
st1.getRow(1).getCell(0).setCellValue(15); st1.getRow(1).getCell(0).setCellValue(15);
evaluator.evaluateAll(); evaluator.evaluateAll();
...@@ -58,14 +64,9 @@ public class POITest { ...@@ -58,14 +64,9 @@ public class POITest {
} }
FileOutputStream excelFileOutPutStream = new FileOutputStream("C:\\source\\test - Copy.xlsx"); FileOutputStream excelFileOutPutStream = new FileOutputStream("C:\\source\\test - Copy.xlsx");
workbook.write(excelFileOutPutStream); workbook.write(excelFileOutPutStream);
excelFileOutPutStream.flush(); excelFileOutPutStream.flush();
excelFileOutPutStream.close(); excelFileOutPutStream.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InvalidFormatException e) { } catch (InvalidFormatException e) {
......
...@@ -5,6 +5,9 @@ import org.apache.poi.ss.formula.eval.*; ...@@ -5,6 +5,9 @@ import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.ss.formula.functions.FreeRefFunction; import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
/*
TmpFunction("g2",5)
*/
public class TmpFunction implements FreeRefFunction { public class TmpFunction implements FreeRefFunction {
@Override @Override
public ValueEval evaluate(ValueEval[] valueEvals, OperationEvaluationContext operationEvaluationContext) { public ValueEval evaluate(ValueEval[] valueEvals, OperationEvaluationContext operationEvaluationContext) {
...@@ -22,10 +25,13 @@ public class TmpFunction implements FreeRefFunction { ...@@ -22,10 +25,13 @@ public class TmpFunction implements FreeRefFunction {
String val1 = OperandResolver.coerceValueToString(v1); String val1 = OperandResolver.coerceValueToString(v1);
int val2 = OperandResolver.coerceValueToInt(v2); int val2 = OperandResolver.coerceValueToInt(v2);
CellReference reference = new CellReference(val1); CellReference reference = new CellReference(val1);
int referenceSheetIndex = operationEvaluationContext.getWorkbook().getSheetIndex(reference.getSheetName());
return new NumberEval(operationEvaluationContext.getWorkbook()
return new NumberEval(operationEvaluationContext.getWorkbook().getSheet(0) .getSheet(referenceSheetIndex)
.getCell(reference.getRow(),reference.getCol()).getNumericCellValue() + val2); .getCell(reference.getRow(), reference.getCol())
.getNumericCellValue()
+ val2);
} catch (EvaluationException e) { } catch (EvaluationException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment