2

I want to remove the cell content but keeping its style but it's always remove the style and content. Here's my code:

private static void RefreshReport(String sheetName, String resultColumn) {
        try {
            InputStream inp = new FileInputStream(getExcelFile());
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheet(sheetName);
            int colResult = getColumnResult(sheet, resultColumn);
            int rowStart = getRowResult(sheet, resultColumn);
            int rowEnd = Math.max(20, sheet.getLastRowNum());
            for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
                Row r = sheet.getRow(rowNum);
                if (r != null) {
                    Cell cell = r.createCell(colResult);
                    if (cell.getCellType() != Cell.CELL_TYPE_BLANK) {
                        CellStyle cs = wb.createCellStyle();
                        cs = cell.getCellStyle();
                        r.removeCell(cell);
                        cell = r.createCell(colResult);
                        cell.setCellStyle(cs);
                    }
                }
            }
            FileOutputStream fileOut = new FileOutputStream(getExcelFile());
            wb.write(fileOut);
            fileOut.close();
            inp.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Any help would be great.

5
  • What is your problem then... Please share what you want which this code is not able to do... Nov 19, 2014 at 4:43
  • @Sankumarsingh I want to delete the content of Cell but keeping its style.
    – Anh Nguyen
    Nov 19, 2014 at 4:47
  • 2
    I'm just wondering why don't you just set the cell value instead of removing it ?
    – zawhtut
    Nov 19, 2014 at 5:35
  • @zawhtut set cell value will remove the cell style too
    – Anh Nguyen
    Nov 19, 2014 at 7:23
  • That does not sound right, but if true: pull the style out, set the content, and set the style again from where you have stored it temporarily.
    – Darius X.
    Nov 21, 2014 at 21:36

2 Answers 2

3

The reason that your method isn't working is that you are always calling createCell if the the row r is not null. This will overwrite any Cell that already exists, including contents and styling.

What you need to do is call getCell instead. This will retrieve the Cell at that column index, provided it exists, or null if it doesn't exist. Then if it exists, you can process the Cell as you are doing.

if (r != null) {
   Cell cell = r.getCell(colResult);
   if (cell != null && cell.getCellType() != CellType.BLANK) {
       // rest of your code here

In an alternative to the delete/re-create method you have, you can just set the cell type to BLANK. This will set the contents to blank, but it will also preserve the cell style currently on that cell. There is no need to delete the cell and re-create it.

if (cell.getCellType() != CellType.BLANK) {
   cell.setCellType(CellType.BLANK);
}
0

Related to the rgettman's comment. But in POI 5.0, you should use cell.setBlank() instead, because cell.setCellType(CellType.BLANK) is deprecated.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 28 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.