我在excel表中有如下值。
ID
12_001
12_008
12_010
13_001
如何找到这些值的最大值。我需要结果为'13_001'。有人能帮助我吗?
我试过这个
string ExcelConnection = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source =" + Filepath + " ; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';";
OleDbDataAdapter Id = new OleDbDataAdapter("SELECT MAX(ID) FROM [Sheet1$A2:A4]", ExcelConnection);
DataSet id = new DataSet();
Id.Fill(id);
答案 0 :(得分:2)
you can use the range if u need
range rng = new range;
int64 dblMax =0; rng = Range("a1", Range("a65536").End(xlUp)) ;
dblMax = Application.WorksheetFunction.Max(rng) ;
return dblMax + 1
答案 1 :(得分:0)
如果您可以发送功能,可以使用this:
=MAX(INDEX(Name1!$C$2:$V$2000,0,MATCH(Overview!S$1,ID!$C$1:$V$1,0)))
答案 2 :(得分:0)
基本上,您可以通过将字符串视为文本,按字母顺序按降序排序,然后从列ID中检索第一条记录来获得所需的输出。
下面的代码应该这样做:
string ExcelConnection = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source =" + Filepath + " ; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';";
string Command = "SELECT ID FROM [Sheet1$]";
try
{
OleDbConnection conn = new OleDbConnection(ExcelConnection);
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
using (conn)
{
conn.Open();
cmd = new OleDbCommand(Command, conn);
da = new OleDbDataAdapter(cmd);
DataSet id = new DataSet();
da.Fill(id);
DataTable idtable = id.Tables[0];
idtable.DefaultView.Sort = idtable.Columns[0].ColumnName + " " + "DESC";
idtable = idtable.DefaultView.ToTable();
Console.WriteLine(idtable.Rows[0][0]);
conn.Close();
}
}
catch (Exception ex)
{
}
答案 3 :(得分:0)
这至多可以通过宏来完成。在下面的列表中,我在范围A1中有不同的值:A10如: 12_001 56_021 89_001 10_002 41_005 36_021 95_002 25_025 32-015 85-002 我通过以下代码提取最大值。最大值显示在消息框中:
For i = 1 To 10
If Val(Left(Cells(i, 1), 2)) > Max Then
Max = Val(Left(Cells(i, 1), 2))
Strg = Max & Right(Cells(i, 1), Len(Cells(i, 1)) - 2)
End If
Next
MsgBox Strg