Hi,

When I create a HSSFWorkbook with a named range and then set the sheet name
with Workbook.setSheetName() all the named ranges' formulas are renamed
also.

When I do this with a XSSFWorkbook the named ranges' formulas are not
renamed and thus are broken (have the value of #REF!").

Is this a bug or divergent behaviour? Any workarounds?

Here is a JUnit tests that creates a file with such a broken named range.

    @Test
    public void create() throws IOException
    {
        String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
        // HSSFWorkbook handles rename
        // Workbook wb = new HSSFWorkbook();
        //File file = new File("c:\\test.xls");

        // XSSFWorkbook does not handle the rename
        Workbook wb = new XSSFWorkbook();
        File file = new File("c:\\test.xlsx");

        Sheet sheet = wb.createSheet(sname);
        sheet.createRow(0).createCell((short) 0).setCellValue(cvalue);

        // 1. create named range for a single cell using areareference
        Name namedCell = wb.createName();
        namedCell.setNameName(cname);
        String reference = sname+"!A1:A1"; // area reference
        namedCell.setRefersToFormula(reference);

        // 2. rename the sheet
        wb.setSheetName(wb.getSheetIndex(sheet), "newName");

        FileOutputStream fout = new FileOutputStream(file);
        wb.write(fout);
    }

Regards,
Carl Pritchett

Reply via email to