1

我正在用 Aspose 创建一个 excel。我希望日期将根据操作系统的区域设置进行格式化,这意味着如果 excel 将在美国或欧洲打开,则日期将以不同的格式显示,例如如果您在 excel 中选择带有星号的格式: printscreen of " Excel 中的“单元格格式”屏幕

这是我的代码:

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

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

如何设置单元格样式?

谢谢 :)

4

1 回答 1

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.

于 2015-12-28T18:56:02.037 回答