Skip to content

Instantly share code, notes, and snippets.

@ryuta-ito
Created March 9, 2020 13:25
Show Gist options
  • Save ryuta-ito/ae3b398f68c0346aca81473bf9527892 to your computer and use it in GitHub Desktop.
Save ryuta-ito/ae3b398f68c0346aca81473bf9527892 to your computer and use it in GitHub Desktop.
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.formula.*;
import org.apache.poi.ss.formula.ptg.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import java.io.*;
import org.apache.poi.openxml4j.exceptions.*;
public class PoiExample {
public static void main(String[] args) {
try {
Workbook workbook = WorkbookFactory.create(new FileInputStream("book.xlsx"));
Sheet sheet = workbook.getSheetAt(0);
Ptg ptgs[] = FormulaParser.parse( sheet.getRow(0).getCell(0).getCellFormula(),
XSSFEvaluationWorkbook.create((XSSFWorkbook) workbook),
FormulaType.CELL,
0 );
System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0).getCellFormula());
for (int i = 0; i < ptgs.length; i++) {
if (ptgs[i] instanceof Ref3DPxg) {
System.out.println(ptgs[i].toFormulaString());
CellReference cellReference = new CellReference(ptgs[i].toFormulaString());
Row row = workbook.getSheet(cellReference.getSheetName()).getRow(cellReference.getRow());
System.out.println(row.getCell(cellReference.getCol()));
}
}
} catch (InvalidFormatException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment