这是我的代码:
'HomeP form has the excelapp, excelwb, excelws objects initialized.
HomeP.excelws.Range("C1", "C900000").NumberFormat = "dd-MMM-yyyy"
HomeP.excelws.Range("D1", "D900000").NumberFormat = "dd-MMM-yyyy"
我正在使用文本框进行输入。
HomeP.excelws.Cells(2, 3).Value = TextBox3.Text
HomeP.excelws.Cells(2, 4).Value = TextBox4.Text
[编辑]:现在问题是,如果我输入'23 -12-2001'那么它将在excel中存储为'23 -Dec-2001'但是如果我输入'01 -11-2001'那么它将会存储为'11 -Jan-2001'。我不认为它是关于输入的,但当日期小于12时Excel如何解释它。我的系统日期格式是dd-mm-yyyy。我希望excel cell中的日期格式为'dd-MMM-yyyy'
答案 0 :(得分:1)
感谢TnTinMin和Aladein,问题已经解决了。我已创建此方法以获得所需格式的日期。
Public Function GetDateProperly(ByVal tb As TextBox)
Try
Dim timee As DateTime = tb.Text
Dim Formatt As String = "dd-MMM-yyyy"
Dim newdate = timee.ToString(Formatt)
Return CDate(newdate)
Catch ex As System.InvalidCastException
Catch ex As NullReferenceException
End Try
End Function
答案 1 :(得分:0)
我想你可以试试:
Dim Timee As DateTime = HomeP.excelws.Cells(2, 3).Value
Dim Formatt As String = "dd-MM-yyyy"
Dim newdate = Timee.ToString(Formatt)
TextBox3.Text = newdate
请告诉我它是否有效!!