How to set default date format when load an xls fi

2019-08-04 08:17发布

问题:

When I load an xls file, which is created by the user, to SpreadSheetGear workbook it changes date format from dd/mm/yyyy to mm/dd/yyyy. How can I prevent this?

I use the code below (VB.NET) to load xls file:

WorkbookView1.ActiveWorkbook =  
    SpreadsheetGear.Factory.GetWorkbookSet.Workbooks.OpenFromMemory(xlsFile)

xlsFile is a Byte array.

回答1:

I had the problem when importing excel xlsx to my site it was shifting the date from dd/MM/yyyy to MM/dd/yyyy though the system data was set to dd/MM/yyyy, the below method gave me the correct format I want.

 Dim dsFromExcel As DataSet = New DataSet
 Dim mywb As SpreadsheetGear.IWorkbook = SpreadsheetGear.Factory.GetWorkbook(strFileName)
 Dim myws As SpreadsheetGear.IWorksheet = mywb.Sheets(0)
 Dim Myrange As SpreadsheetGear.IRange = myws.Cells

'this will set the format of the first column A to this date format
 Myrange("A:A").NumberFormat = "MM/dd/yyyy HH:mm:ss"

'importing the data to a dataset/datatable
 dsFromExcel = mywb.GetDataSet(SpreadsheetGear.Data.GetDataFlags.FormattedText)

Dim dt As DataTable = New DataTable
dt = dsFromExcel.Tables(0)


回答2:

You should be able to open the workbook using a culture info that defaults to dd/mm/yyyy. This may be your current culture.

Dim myWorkBook As SpreadsheetGear.IWorkbook = SpreadsheetGear.Factory.GetWorkbook(fileName, new System.Globalization.CultureInfo('es-PR'))

This site gives a good overview of .Net Framework culture defaults. http://www.basicdatepicker.com/samples/cultureinfo.aspx