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;
@RestController
@RequestMapping(value = "api/v1/Report")
public class ReportController {
@Autowired
@Autowired
ReportService reportService;
@RequestMapping(value = "template/{organizationID}/{period}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
......
......@@ -19,20 +19,26 @@ public class POITest {
try {
FileInputStream fis = new FileInputStream(workbookFile);
Workbook workbook = WorkbookFactory.create(fis);
String[] functionNames = {"TmpFunction"};
FreeRefFunction[] functionImpls = {new TmpFunction()};
UDFFinder udfs = new DefaultUDFFinder(functionNames, functionImpls);
UDFFinder udfToolpack = new AggregatingUDFFinder(udfs);
workbook.addToolPack(udfToolpack);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
int sheetNum = workbook.getNumberOfSheets();
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).setCellValue(15);
evaluator.evaluateAll();
......@@ -58,14 +64,9 @@ public class POITest {
}
FileOutputStream excelFileOutPutStream = new FileOutputStream("C:\\source\\test - Copy.xlsx");
workbook.write(excelFileOutPutStream);
excelFileOutPutStream.flush();
excelFileOutPutStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
......
......@@ -5,6 +5,9 @@ import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.util.CellReference;
/*
TmpFunction("g2",5)
*/
public class TmpFunction implements FreeRefFunction {
@Override
public ValueEval evaluate(ValueEval[] valueEvals, OperationEvaluationContext operationEvaluationContext) {
......@@ -22,10 +25,13 @@ public class TmpFunction implements FreeRefFunction {
String val1 = OperandResolver.coerceValueToString(v1);
int val2 = OperandResolver.coerceValueToInt(v2);
CellReference reference = new CellReference(val1);
int referenceSheetIndex = operationEvaluationContext.getWorkbook().getSheetIndex(reference.getSheetName());
return new NumberEval(operationEvaluationContext.getWorkbook().getSheet(0)
.getCell(reference.getRow(),reference.getCol()).getNumericCellValue() + val2);
return new NumberEval(operationEvaluationContext.getWorkbook()
.getSheet(referenceSheetIndex)
.getCell(reference.getRow(), reference.getCol())
.getNumericCellValue()
+ val2);
} catch (EvaluationException e) {
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