How can I just clear value not formula in Java?

Hello~

I’d like to clear(or delete) just Value(Data) in Cell not Formula.

I’ve tried to clearContents(), deleteRange().but both functions delete even data and formula in the Cell.

Could you tell me how keep the formula in the Cell?

Thank you

@goodhosik,

In MS Excel, when you delete cell’s value, its underlying formula (if it has) would also be removed. This is MS Excel’s behavior. Similarly when you update the value of the cell, its formula would also be removed and only value is set to the cell. For your needs, you may create style object and set some custom string (e.g " " in your case) to hide the cell value, then apply that style to the cell. Now the formula would remain and its value would be hidden as per your custom formatting. See the sample code for your reference:
e.g
Sample code:

Workbook workbook = new Workbook("e:\\test2\\Bktest1.xlsx");
		Worksheet worksheet = workbook.getWorksheets().get(0);
		Cells cells = worksheet.getCells();
		
		//Suppose A4 cell has formula, and you want to hide its calculated value and underlying formula should be retained.
		//You need to create style object with custom string to hide its (calculated) value.
		Cell cell= cells.get("A4");
		Style style = workbook.createStyle();
		style.setCustom(" ");
		cell.setStyle(style);
		
		workbook.save("f:\\files\\outBktest.xlsx");

Hope, this helps a bit.