Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从excel中的列获取最小和最大日期。C#_C#_Vba_Excel - Fatal编程技术网

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

从excel中的列获取最小和最大日期。C#,c#,vba,excel,C#,Vba,Excel,我试图在excel工作表的列中返回最小和最大日期。 我有一个用VBA创建的程序,我正在用C#重写,这方面我不太确定。 我使用的原始代码是 WB.Activate DataInputRows = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count ActiveSheet.Range("S2:T" & Cells(Rows.Count, "A").End(xlUp).Row).NumberForma

我试图在excel工作表的列中返回最小和最大日期。 我有一个用VBA创建的程序,我正在用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")
我已经涵盖了排序和格式区域,但我不能完全确定“SYear=”或“EYear=”位

有人知道从列中求最小值和最大值的“简单”方法吗

到目前为止,我在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让我思考了一下。我很确定它不应该像看起来那么难,而且命令应该足够相似。所以我做了一点挖掘,找到了我错在哪里(或者更具体地说,我没有错在哪里)。编辑了我的代码,以显示工作变量,以防将来有人需要它,并显示+1'd拉尔夫的大脑中的踢。:)


在c#中使用excel应用程序类,然后使用相同的前缀,例如excel.application x,然后是x.activeworkbook?你管理过代码吗?另外,可能会更幸运地在c#中发布使用
WorksheetFunction.Min(
WorksheetFunction.Max(
Application.Min(
Application.Max(
)的错误是什么?你确定它将范围内的数据视为日期而不是字符串吗?WorksheetFunction只给我“等于”或“引用等于”在这段时间之后。@DDuffy谢谢你的提议。我很乐意接受这方面的积分。但是,我不认为这是诚实的,因为当你提到
SYear=Format(WorksheetFunction.Min(Range(“T1:t”和DataInputRows)),“dd/MM/yyyyy”)时,你已经在你的问题中找到了答案。
(这已经是正确的方向,只需要一些代码调整)。因此,我建议您要么删除该问题,要么自己回答该问题(如果答案得分为3分或以上,则赢得下一个徽章)。再次感谢您的提议。自学永远是最好的,我们在此鼓励您。
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());