Just experimented with your test file (thanks for that by the way) and
understand where your concern was coming from. In that workbook, even though
the names existed the cells did not. The previous iteration of the code did
not handle this problem but the newest version (the one above) does do so.
Now, if the name exists within the workbook but the corresponding cell is
null, then the code will create and return a blank cell that you can
initialise with a value. Try adding this method into the class above (you
will also need to modify the constructor so that this method is called
instead of the other two of course);

    private void modifyExisting(String filename) throws IOException {
        File file = null;
        FileInputStream fis = null;
        FileOutputStream fos = null;
        HSSFWorkbook workbook = null;
        HSSFCell cell = null;
        try {
            // Open the workbook.
            file = new File(filename);
            fis = new FileInputStream(file);
            workbook = new HSSFWorkbook(fis);
            fis.close();
            fis = null;

            // Get the named cell. This code simply writes the name of the
            // sheet/cell into the cell to demonstrate that it has been
changed.
            cell = this.getNamedCell("Feuil1!MyRange", workbook);
            if(cell != null) {
                cell.setCellValue("Feuil1!MyRange");
            }
            cell = this.getNamedCell("Feuil2!MyRange", workbook);
            if(cell != null) {
                cell.setCellValue("Feuil2!MyRange");
            }
            cell = this.getNamedCell("Feuil3!MyRange", workbook);
            if(cell != null) {
                // Just to show you are not limited to writing Strings into
the
                // cell, set this one to a numeric value.
                cell.setCellValue(1234.56);
            }

            // Write the workbook out again. Here, the file is being
overwritten
            // but there is nothing preventing you from supplying a
different
            // name/location for the file and writing it there.
            fos = new FileOutputStream(file);
            workbook.write(fos);
        }
        finally {
            if(fos != null) {
                fos.close();
                fos = null;
            }
        }
    }

If you run this against the test file that you forwarded to me, you should
see that it sets the values of each of the named cells before re-saving the
file away. You can write any sort of value you want into the cell that the
getNamedCell() method returns and can set the style/format as you wish; just
treat it as you would a cell created by a call to the row.createCell(int)
method.

Hope this helps.

Yours

Mark B

--
View this message in context: 
http://apache-poi.1045710.n5.nabble.com/Same-named-cells-in-several-sheets-tp5683541p5689322.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to