com.aspose.cells.Cell setStyle no effect

I use following code to set an excel cell style to custom dd.mm.yyyy but fail.

...
in = this.getClass().getResourceAsStream("/resources/Progress Report.xlsx");	
wb = new Workbook(in);
in.close();
ws = wb.getWorksheets().get(0);
Cell c = ws.getCells().get(1,2);
c.putValue(result[j]);  // result[j] is an object type storing a date
Style s = c.getStyle();
s.setCustom("dd.mm.yyyy");					
c.setStyle(s);
...

when output, I right click the cell, select Format cell, and in number tab, it show “General”, not “Custom” with type “dd.mm.yyyy”

why?

@tong123123

It seems that you are trying to set a custom date format for a cell in an Excel file using Aspose.Cells, but the format is not being applied as expected. The issue may arise from how the style is being set on the cell.

In your code, you are creating a Style object from the cell, setting the custom format, and then applying that style back to the cell. However, it is important to ensure that the number format is also applied correctly. Here’s how you can modify your code to ensure that the custom format is applied properly:

import com.aspose.cells.*;

InputStream in = this.getClass().getResourceAsStream("/resources/Progress Report.xlsx");
Workbook wb = new Workbook(in);
in.close();
Worksheet ws = wb.getWorksheets().get(0);
Cell c = ws.getCells().get(1, 2);
c.putValue(result[j]);  // result[j] is an object type storing a date

// Create a new style
Style s = wb.createStyle();
s.setCustom("dd.mm.yyyy"); // Set the custom date format
c.setStyle(s); // Apply the style to the cell

// Save the workbook
wb.save("Output.xlsx");

In this modified code, I used wb.createStyle() to create a new style instance instead of modifying the existing style of the cell. This ensures that the custom format is applied correctly. After setting the custom format, make sure to save the workbook to see the changes reflected in the output file.

If you still encounter issues, ensure that the cell’s value is indeed a date object, as the formatting will only apply correctly if the underlying value is recognized as a date.

Feel free to reach out if you have any further questions or need additional assistance!

@tong123123
Please refer to the following example code and check the result file. out_java.zip (6.7 KB)

Workbook wb = new Workbook();
Worksheet ws = wb.getWorksheets().get(0);
Cell c = ws.getCells().get(1,2);
c.putValue(123456);
Style s = c.getStyle();
s.setCustom("dd.mm.yyyy");					
c.setStyle(s);
wb.save(filePath + "out_java.xlsx");

If you still have questions, please provide sample file and complete runnable test code, and we will check it soon.