从excel的列中获取最小和最大date。 C#

我正在尝试返回Excel表格中列的最小和最大date。 我有一个程序,我在VBA中创build,我在C#中重写,我不太确定这部分。 我使用的原始代码是;

WB.Activate DataInputRows = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count ActiveSheet.Range("S2:T" & Cells(Rows.Count, "A").End(xlUp).Row).NumberFormat = "dd/mm/yyyy" ActiveSheet.Range("V2:V" & Cells(Rows.Count, "A").End(xlUp).Row).NumberFormat = "dd/mm/yyyy" ActiveSheet.Range("A1:" & LastColumn & Cells(Rows.Count, "A").End(xlUp).Row).Sort _ Key1:=Range("T1"), Header:=xlYes SYear = Format(WorksheetFunction.Min(Range("T1:T" & DataInputRows)), "dd/MM/yyyy") EYear = Format(WorksheetFunction.Max(Range("T1:T" & DataInputRows)), "dd/MM/yyyy") 

我有sorting和格式覆盖面积,但我不能完全得到“SYear =”或“EYear =”位钉。

有没有人知道从列中find最小值和最大值的“简单”方法?

我在C#到目前为止的代码是;

  DataInputRows = InputSheet.UsedRange.Rows.Count; Excel.Range rng = (Excel.Range)OutputSheet.Cells[2, 19]; rng.EntireColumn.NumberFormat = "dd/MM/yyyy"; rng = (Excel.Range)OutputSheet.Cells[2, 20]; rng.EntireColumn.NumberFormat = "dd/MM/yyyy"; rng = (Excel.Range)OutputSheet.Cells[2, 22]; rng.EntireColumn.NumberFormat = "dd/MM/yyyy"; SourceRange.Sort(SourceRange.Columns[20, Type.Missing], Excel.XlSortOrder.xlAscending, Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortOrder.xlAscending, Excel.XlYesNoGuess.xlYes, Type.Missing, Type.Missing, Excel.XlSortOrientation.xlSortColumns, Excel.XlSortMethod.xlPinYin, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal); var SYear = (This is where i need the minimum value of column T (20)) 

得到它了!!

  DataInputRows = InputSheet.UsedRange.Rows.Count; Excel.Range rng = (Excel.Range)InputSheet.Cells[2, 19]; rng.EntireColumn.NumberFormat = "dd/MM/yyyy"; rng = (Excel.Range)InputSheet.Cells[2, 20]; rng.EntireColumn.NumberFormat = "dd/MM/yyyy"; rng = (Excel.Range)InputSheet.Cells[2, 22]; rng.EntireColumn.NumberFormat = "dd/MM/yyyy"; SourceRange.Sort(SourceRange.Columns[20, Type.Missing], Excel.XlSortOrder.xlAscending, Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortOrder.xlAscending, Excel.XlYesNoGuess.xlYes, Type.Missing, Type.Missing, Excel.XlSortOrientation.xlSortColumns, Excel.XlSortMethod.xlPinYin, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal); Excel.WorksheetFunction wsf = ObjApp.WorksheetFunction; rng = (Excel.Range)InputSheet.Cells[2, 20]; var SYear = wsf.Min(rng); DateTime dt = DateTime.FromOADate(SYear); MessageBox.Show(dt.ToString()); 

感谢拉尔夫的启动。

所以@Ralph让我思考。 我非常肯定它应该和看起来一样困难,命令应该足够相似。 所以我做了一点挖掘,发现我哪里错了(或更具体地说,我不去)。 编辑我的代码,以显示未来任何人需要它的工作变体,并为Ralph +1大脑的踢。 🙂

 DataInputRows = InputSheet.UsedRange.Rows.Count; Excel.Range rng = (Excel.Range)InputSheet.Cells[2, 19]; rng.EntireColumn.NumberFormat = "dd/MM/yyyy"; rng = (Excel.Range)InputSheet.Cells[2, 20]; rng.EntireColumn.NumberFormat = "dd/MM/yyyy"; rng = (Excel.Range)InputSheet.Cells[2, 22]; rng.EntireColumn.NumberFormat = "dd/MM/yyyy"; SourceRange.Sort(SourceRange.Columns[20, Type.Missing], Excel.XlSortOrder.xlAscending, Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortOrder.xlAscending, Excel.XlYesNoGuess.xlYes, Type.Missing, Type.Missing, Excel.XlSortOrientation.xlSortColumns, Excel.XlSortMethod.xlPinYin, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal); Excel.WorksheetFunction wsf = ObjApp.WorksheetFunction; rng = (Excel.Range)InputSheet.Cells[2, 20]; var SYear = wsf.Min(rng); DateTime dt = DateTime.FromOADate(SYear); MessageBox.Show(dt.ToString());