ScorpJ
ScorpJ

Reputation: 19

How to automatically remove @ of the excel formula generated by POI setCellFormula

When use setCellFormula set by paramete "CHISQ.TEST(ChiSq_Data!D5:F5,ChiSq_Data!L5:N5)",but the output was "[email protected](ChiSq_Data!D5:F5,ChiSq_Data!L5:N5)", the @ symbol make the formula did not work and shows #VALUE in the result excel. How can I remove the @ automatically?

Upvotes: 0

Views: 384

Answers (1)

Axel Richter
Axel Richter

Reputation: 61860

This is a similar problem as this one: Apache POI Excel Formula entering @ Symbols where they don't belong.

All new functions (introduced after Excel 2007) are prefixed with _xlfn in internally file storage. The GUI does not show that prefix if the Excel version is able to interpret that function. If the Excel version is too old to be able to interpret that function you may see that prefix even in GUI.

Apache POI creates Excel files and that's why writes formulas in file storage directly. Using:

cell.setCellFormula("CHISQ.TEST(ChiSq_Data!D5:F5,ChiSq_Data!L5:N5)");

it writes CHISQ.TEST(ChiSq_Data!D5:F5,ChiSq_Data!L5:N5) into the file storage but the Excel GUI expects _xlfn.CHISQ.TEST(ChiSq_Data!D5:F5,ChiSq_Data!L5:N5). That's why the #NAME? error.

But why the @? The @ is the implicit intersection operator. Implicit intersection is a new feature of Excel 365 (a silly one in my opinion, as well as dynamic array and spilling array behavior). And because Excel 365 does not know the function CHISQ.TEST without the prefix but it contains arrays of cells as parameters, it puts @ in front of it to show that it would use implicit intersection if it would know it.

So the solution is to put the correct prefix before the function name in file storage to make it work:

cell.setCellFormula("_xlfn.CHISQ.TEST(ChiSq_Data!D5:F5,ChiSq_Data!L5:N5)");

Complete example to test:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;

class CreateExcelCHISQ_TEST {
    
 public static void main(String[] args) throws Exception {

  try (
       Workbook workbook = new XSSFWorkbook(); FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   Sheet sheet = workbook.createSheet();
   Row row;
   Cell cell;
   
   // Filling dummy data to another sheet
   Sheet otherSheet = workbook.createSheet("ChiSq_Data");
   row = otherSheet.createRow(4);
   row.createCell(3).setCellValue(123);
   row.createCell(4).setCellValue(456);
   row.createCell(5).setCellValue(78);
   row.createCell(11).setCellValue(122.5);
   row.createCell(12).setCellValue(456.5);
   row.createCell(13).setCellValue(77.5);
   
   row = sheet.createRow(0);
   cell = row.createCell(0);
   //cell.setCellFormula("CHISQ.TEST(ChiSq_Data!D5:F5,ChiSq_Data!L5:N5)"); // wrong
   cell.setCellFormula("_xlfn.CHISQ.TEST(ChiSq_Data!D5:F5,ChiSq_Data!L5:N5)");

   workbook.write(fileout);
  }

 }
}

Upvotes: 1

Related Questions