How to set default date format when load an xls file to SpreadSheetGear workbook?

对着背影说爱祢 提交于 2019-12-13 17:17:15

问题


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



来源:https://stackoverflow.com/questions/4087148/how-to-set-default-date-format-when-load-an-xls-file-to-spreadsheetgear-workbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!