1

I am creating an excel with Aspose. I would like that the date will be formatted according to the OS's Locale, meaning that if the excel will be open in US or Europe the dates will appear in a different format, like if you select the format with asterisk in excel: printscreen of "Format Cells" screen in Excel

This is my code:

style.setCustom(????);
cell.setStyle(style);

String date ="2/23/2015";
DateTime myDate = new DateTime(new Date(date));
cell.setValue(myDate);

How shall I set the cell style?

thanks :)

Shirly
  • 11
  • 3

1 Answers1

1

In order to make the format adaptable to the operating system's locale, you have to use the built-in number formats. Please review the detailed article on setting date & number formats using Aspose.Cells for Java APIs and check the built-in number formats from 14 to 17 for date literals. See the sample code below:

Workbook book = new Workbook();
Worksheet sheet = book.getWorksheets().get(0); 
Cells cells = sheet.getCells(); 
Style style = book.createStyle(); 
//style.setCustom("m/d/yyyy");
style.setNumber(14); 
Cell cell = cells.get("A1"); 
cell.setStyle(style); 
String date = "2/23/2015"; 
DateTime myDate = new DateTime(new Date(date));
cell.setValue(myDate); 
book.save("output.xlsx");

I am working as Support developer/ Evangelist at Aspose.

Amjad Sahi
  • 1,813
  • 1
  • 10
  • 15