简体   繁体   English

Aspose.cells自定义日期格式,与操作系统日期格式无关

[英]Aspose.cells custom date formatting irrespective of OS date format

I am currently using aspose.cells for .Net and need to set a custom format a date based on the current language being used in our app. 我目前正在为.Net使用aspose.cells,需要根据我们的应用程序中使用的当前语言为日期设置自定义格式。 Issue is, we are setting the format via code below, but if the system(OS) date format is different, our custom formatting goes for a toss and the excel displays the date in the system(OS) format. 问题是,我们通过下面的代码设置格式,但是如果system(OS)日期格式不同,我们的自定义格式会折腾,而excel将以system(OS)格式显示日期。 Is there a workaround for this? 有没有解决方法? Below is the code: 下面是代码:

var dateStyle = dataWorksheet.Cells.GetCellStyle(startRowNumber, column);

            dateStyle.Custom = CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat.ShortDatePattern;
            var dateRange = dataWorksheet.Cells.CreateRange(startRowNumber, column, dataTable.Rows.Count + 1, 1);

            dateRange.SetStyle(dateStyle);

Also tried the solution here , but, to no avail. 也尝试过这里的解决方案,但无济于事。

@Gautam, Please note, the statement CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat.ShortDatePattern returns a pattern which is an Excel's built-in format (M/d/yyyy) therefore setting it to a cell (or range of cells) as custom format will auto convert the format to built-in, which will change according to the locale of the machine where the spreadsheet is going to be loaded. @Gautam,请注意,语句CultureInfo.CreateSpecificCulture(“ en-US”)。DateTimeFormat.ShortDatePattern返回的模式是Excel的内置格式(M / d / yyyy),因此将其设置为单元格(或范围为单元格)作为自定义格式将自动将格式转换为内置格式,该格式将根据要加载电子表格的计算机的语言环境而变化。 I would suggest you to use the custom pattern as mm/dd/yyyy which would yield similar result but the format will not change according to the locale. 我建议您将自定义模式用作mm / dd / yyyy,这将产生相似的结果,但是格式不会根据区域设置而改变。 Please check following piece of code as well as the attached snapshot showing the Format Cell dialog. 请检查以下代码以及显示“格式单元格”对话框的所附快照。

C# C#

var book = new Workbook();
var sheet = book.Worksheets[0];
var dateStyle = sheet.Cells.GetCellStyle(0, 0);

dateStyle.Custom = "mm/dd/yyyy";//CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat.ShortDatePattern; //represents M/d/yyyy
var dateRange = sheet.Cells.CreateRange(1, 0, 10, 1);
dateRange.SetStyle(dateStyle);
dateRange.PutValue("26-Sep-2014", true, false);
book.Save(dir + "output.xlsx");

在此处输入图片说明

Note: I am working as Developer Evangelist at Aspose 注意:我在Aspose担任开发人员布道者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM