如何使用数组排名功能? [英] How Can I Use Rank Function With Array?
本文介绍了如何使用数组排名功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int [] list = new int [] { 6 , 50 , 99 , 35 , 1 , 2 , 7 };
Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.WorksheetFunction wsf = xl.WorksheetFunction;
foreach ( var item 在列表中)
{
// wsf.Rank(ItemToRank,RangeOflist,订单);
// 错误:wsf.Rank(item,此参数中的错误是什么我应该通过这里,0);
double rank = wsf.Rank(item,list, 0 跨度>);
}
输入:6,50,99,35,1,2,7
输出:3,5,6,4,1,2,4
解决方案
请查看以下链接。
C#中的VBA / Excel RANK [ ^ ]
工作表功能.Rank Method(Excel) [ ^ ]
您好,
您需要传递一个excel范围对象作为第二个参数。有关Excel排名功能的详细信息,请参阅以下链接。
https://msdn.microsoft.com/en-us/library/office/ff840358.aspx [ ^ ]
请参考下面的代码并查看范围设置并用作Rank()函数的参数。
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用Excel = Microsoft.Office.Interop.Excel;
命名空间ConsoleApplication1
{
class程序
{
static void Main(string [] args)
{
int [ ] list = new int [] {6,50,99,35,1,2,7};
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
Excel.Workbook xlWb = xlApp.Workbooks.Add()as Excel.Workbook;
Excel.Worksheet xlSheet = xlWb.Sheets [1] as Excel.Worksheet;
Microsoft.Office.Interop.Excel.WorksheetFunction wsf1 = xlApp.WorksheetFunction;
Excel.Range range = xlSheet.get_Range(A1:A7);
for(int i = 0; i< list.Count(); i ++)
{
range.Cells [i + 1] = list [i];
}
for(int i = 0; i< list.Count(); i ++)
{
Console.WriteLine({ 0} - Rank为:{1},list [i],wsf1.Rank(list [i],range.get_Range(A1:A7),0));
}
// xlWb.Close(SaveChanges:false);
// xlApp.Visible = false;
// xlApp = null;
Console.ReadKey();
}
}
}
我不建议使用任何特定的库,除非它是标准的.net。
根据您的要求,我建议使用索引:
int [] list = new int [] {< span class =code-digit> 6 , 50 , 99 , 35 , 1 , 2 , 7 跨度>};
var qry = list
.OrderBy(i => i)
.Select((i,index )=>新
{
number = i,
rank = index + 1
});
结果:
数字等级
1 1
2 2
< span class =code-digit> 6 3
7 4
35 5
50 6
99 7
int[] list = new int[] { 6,50,99,35,1,2,7};
Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.WorksheetFunction wsf = xl.WorksheetFunction;
foreach (var item in list)
{
// wsf.Rank(ItemToRank,RangeOflist,Order);
//Error: wsf.Rank(item, Error in this argument what should i pass here ,0);
double rank= wsf.Rank(item,list, 0);
}
Input: 6,50,99,35,1,2,7
Ouput: 3,5,6,4,1,2,4
解决方案
Hi,
Kindly review following links for the same.
VBA/Excel RANK in C#[^]
WorksheetFunction.Rank Method (Excel)[^]
Hi,
You need to pass an excel range object as the second argument . Please refer below link for details on Excel Rank function.
https://msdn.microsoft.com/en-us/library/office/ff840358.aspx[^]
Please refer below code and see how Range is set and used as an argument for Rank() function .
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Excel= Microsoft.Office.Interop.Excel; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[] list = new int[] { 6, 50, 99, 35, 1, 2, 7 }; Excel.Application xlApp = new Excel.Application(); xlApp.Visible = true; Excel.Workbook xlWb = xlApp.Workbooks.Add() as Excel.Workbook; Excel.Worksheet xlSheet = xlWb.Sheets[1] as Excel.Worksheet; Microsoft.Office.Interop.Excel.WorksheetFunction wsf1 = xlApp.WorksheetFunction; Excel.Range range = xlSheet.get_Range("A1:A7"); for (int i = 0; i < list.Count();i++) { range.Cells[i+1] = list[i]; } for (int i = 0; i < list.Count(); i++) { Console.WriteLine(" {0} - Rank is : {1} ", list[i] ,wsf1.Rank(list[i], range.get_Range("A1:A7"),0)); } // xlWb.Close(SaveChanges:false); // xlApp.Visible = false; // xlApp = null; Console.ReadKey(); } } }
I do not recommend to use any specific library, unless it's standard .net.
Based on your requirements, i'd suggest to use "index":
int[] list = new int[] {6,50,99,35,1,2,7}; var qry = list .OrderBy(i=>i) .Select((i, index)=>new { number = i, rank = index+1 });
Result:
number rank 1 1 2 2 6 3 7 4 35 5 50 6 99 7
Note, you can always write extension method[^].
这篇关于如何使用数组排名功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文