Load the Workbook with specific System Culture Info
Possible Usage Scenarios
Earlier, you have to change the culture info of the entire thread to deal with numbers and dates in particular culture format, but now Aspose.Cells provides LoadOptions.CultureInfo property which you can use to load your workbook with specific culture info without changing the culture info of the entire thread.
Load the Workbook with specific System Culture Info
The following sample code shows how to load the workbook with specific system culture Info to deal with dates.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
using (var inputStream = new MemoryStream()) | |
{ | |
using (var writer = new StreamWriter(inputStream)) | |
{ | |
writer.WriteLine("<html><head><title>Test Culture</title></head><body><table><tr><td>10-01-2016</td></tr></table></body></html>"); | |
writer.Flush(); | |
var culture = new CultureInfo("en-GB"); | |
culture.NumberFormat.NumberDecimalSeparator = ","; | |
culture.DateTimeFormat.DateSeparator = "-"; | |
culture.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy"; | |
LoadOptions options = new LoadOptions(LoadFormat.Html); | |
options.CultureInfo = culture; | |
using (var workbook = new Workbook(inputStream, options)) | |
{ | |
var cell = workbook.Worksheets[0].Cells["A1"]; | |
Assert.AreEqual(CellValueType.IsDateTime, cell.Type); | |
Assert.AreEqual(new DateTime(2016, 1, 10), cell.DateTimeValue); | |
} | |
} | |
} |
The following sample code shows how to load the workbook with specific system culture Info to deal with numbers.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
using (var inputStream = new MemoryStream()) | |
{ | |
using (var writer = new StreamWriter(inputStream)) | |
{ | |
writer.WriteLine("<html><head><title>Test Culture</title></head><body><table><tr><td>1234,56</td></tr></table></body></html>"); | |
writer.Flush(); | |
var culture = new CultureInfo("en-GB"); | |
culture.NumberFormat.NumberDecimalSeparator = ","; | |
culture.DateTimeFormat.DateSeparator = "-"; | |
culture.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy"; | |
LoadOptions options = new LoadOptions(LoadFormat.Html); | |
options.CultureInfo = culture; | |
using (var workbook = new Workbook(inputStream, options)) | |
{ | |
var cell = workbook.Worksheets[0].Cells["A1"]; | |
Assert.AreEqual(CellValueType.IsNumeric, cell.Type); | |
Assert.AreEqual(1234.56, cell.DoubleValue); | |
} | |
} | |
} |