对excel中的数据进行排序 [英] Sort data like in excel
问题描述
大家好。
我在CSV文件中有数据。
Hello everyone.
I have data in the CSV file.
ELBO;ELBO-17375;90;80;4;0;1,5;109
TUBI;TUBE-8732;0;150;6;0;11,54;184,01
ELBO;ELBO-17375;90;200;8;0;20;17
REDU;REDU-17378-K;0;200;10;8;7,2;2
ELBO;ELBO-17375;90;150;6;0;8,1;25
TUBI;TUBE-8732;0;25;3,5;0;2,15;40,11
ELBO;ELBOW-36-42-81;90;25;3,5;0;0,2;9
TUBI;TUBE-8732;0;200;8;0;41,63;153,06
TUBI;TUBE-8732;0;250;8;0;52,28;262,35
ELBO;ELBO-17375;90;250;8;0;31;25
TUBI;TUBE-8732;0;80;4;0;6,36;716,17
要按照正确的顺序对数据进行排序我正在使用Microsoft.Office.Interop.Excel
数据代码是近似值。
To sort the data in the correct order I am using Microsoft.Office.Interop.Excel
The data code is approximate.
Range range = sheets["A1", string.Concat("L", count)];
Range ranges = sheets[string.Concat("B1:B", count), Missing.Value];
sheets.Sort.SortFields.Add(ranges, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
ranges = sheets[string.Concat("C1:C", count), Missing.Value];
sheets.Sort.SortFields.Add(ranges, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
ranges = sheets[string.Concat("D1:D", count), Missing.Value];
sheets.Sort.SortFields.Add(ranges, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
ranges = sheets[string.Concat("E1:E", count), Missing.Value];
sheets.Sort.SortFields.Add(ranges, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
ranges = sheets[string.Concat("F1:F", count), Missing.Value];
sheets.Sort.SortFields.Add(ranges, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
sheets.Sort.SetRange(range);
sheets.Sort.Orientation = XlSortOrientation.xlSortColumns;
sheets.Sort.SortMethod = XlSortMethod.xlPinYin;
sheets.Sort.Apply();
输出如下
The output is as follows
ELBO ELBO-17375 90 80 4 0 1,5 109
ELBO ELBO-17375 90 150 6 0 8,1 25
ELBO ELBO-17375 90 200 8 0 20 17
ELBO ELBO-17375 90 250 8 0 31 25
ELBO ELBOW-36-42-81 90 25 3,5 0 0,2 9
REDU REDU-17378-K 0 200 10 8 7,2 2
TUBI TUBE-8732 0 25 3,5 0 2,15 40,11
TUBI TUBE-8732 0 80 4 0 6,36 716,17
TUBI TUBE-8732 0 150 6 0 11,54 184,01
TUBI TUBE-8732 0 200 8 0 41,63 153,06
TUBI TUBE-8732 0 250 8 0 52,28 262,35
按顺序依次排列一列。
是否可以在没有excel的情况下进行相同的排序?
也许这些排序有一些库?
谢谢
Sorting is sequentially one column after another.
Is it possible to do the same sort without excel?
Maybe there are some libraries for these sorts?
Thanks
推荐答案
最简单的方法是使用Link to objects。
The easiest way to do it is to uses Link to objects.
myContainer
.OrderBy(x => x.FirstColumn)
.ThenBy(x => x.SecondColumn);
或者,您可以定义自己的比较运算符,它将比较第一列,如果匹配,它将比较下一列,直到找到答案或所有comapred字段相同。
比较函数将如下所示:
Alternatively, you can define your own comparison operator that will compare first column and if there is a match it will compare next column until the answer is found or all comapred fields are identical.
The comparison function will then look like this:
int Compare(T first, T second)
{
int result = first.ColumnOne.Compare(second.ColumnOne);
if (result == 0)
{
result = first.ColumnTwo.Comapre(second.ColumnTwo);
if (result == 0)
{
result = ...
}
}
return result;
}
为了避免契约嵌套,你可以使用早期返回和相反的条件。
To avoid deed nesting, you can uses early returns and the opposite condition.
我建议你使用 OLEDB [ ^ ]使用T-SQL命令的驱动程序这个:
I would suggest you to use OLEDB[^] drivers with T-SQL command like this:
SELECT Field1, Field2, ... FieldN
FROM MyFile.csv
ORDER BY Field1
如何:使用OleDb导入文本文件(tab,csv,custom) [ ^ ]
读取文本文件(txt,csv,日志,制表符,固定长度) [ ^ ]
当然有可能。
例如,您可以将CVS
文件数据导入(内存中)DataTable
然后使用标准SQL
查询对其进行排序。
Of course it is possible.
You might, for instance, import theCVS
file data into a (in-memory)DataTable
and then sort it with a standardSQL
query.
这篇关于对excel中的数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!