Quantcast
Channel: Java Programming Forum - Learn Java Programming - Apache POI
Viewing all articles
Browse latest Browse all 120

Return a range of cells with Apache POI Java API, Excel and Java

$
0
0
I have 3 named ranges on excel, say,

Name COUNT_FAM Refers To "COUNTA(Sheet1!$X$X)"
Name FAMILIES Refers To "INDIRECT(Sheet1!I2:I"&COUNT_FAM")"
Name FLEX_SOM Refers To "OFFSET(Sheet1!$Y$2, 0, 0, COUNT_FAM-1, 3)".
Now, I need a function that returns to me the range of cells (like A1:A4) or just display the cell(s) if I pass the workbook and the Refers To string as parameters. How could this be coded in Java using Apache POI Java API?

I tried to write a method that takes the file location and name of the range as inputs instead of workbook and refers to. Here is what I have coded:

public static void formulafn(String fileName, String rangeName) throws FileNotFoundException, IOException {

FileInputStream file = new FileInputStream(new File(fileName));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFName[] names = new XSSFName[workbook.getNumberOfNames()];
for (int i = 0; i < names.length; i++) {
names[i] = workbook.getNameAt(i);
}

for (int i = 0; i < names.length; i++) {
if (names[i].getNameName().matches(rangeName)) {
XSSFEvaluationWorkbook hssfew = XSSFEvaluationWorkbook.create(workbook);
Ptg[] ptg = FormulaParser.parse(names[i].getRefersToFormula(), hssfew, FormulaType.NAMEDRANGE, 0);
System.out.println(names[i].getNameName()+"\t"+names[i].getRefersToFormula());
for(int y=0;y<ptg.length;y++){
System.out.print(ptg[y]+"\t");
}
Ptg tempptg = ptg[ptg.length-1];
if(tempptg instanceof FuncVarPtg){
FuncVarPtg ref = (FuncVarPtg) tempptg;
if(ref.getName().equalsIgnoreCase("counta")){
}
}
}
}
}
I don't know how to proceed after this.

Thanks!

Viewing all articles
Browse latest Browse all 120

Latest Images

Trending Articles



Latest Images