[英]Get distinct values from column by column
Have to get each columns distinct data and store to the Dictionary (or array) using Excel.interop. 必须获取每列不同的数据并使用Excel.interop存储到Dictionary(或数组)。 I have tried the following code, but it does not align with Excel.interop. 我尝试了以下代码,但与Excel.interop不符。
var excel = new ExcelQueryFactory("worksheetFileName");
var distinctNames = (from row in excel.WorkSheet() select row["ColB"]).Distinct();
Please provide the Excel.Interop snippet/code to get distinct values column by column and store in array. 请提供Excel.Interop片段/代码以逐列获取不同的值并存储在数组中。
For this operation it does not make sense to using Excel automation, instead the prudent course of action is to work with OleDb unless there is a sound reason for using Excel automation. 对于此操作,使用Excel自动化没有任何意义,而是谨慎的做法是与OleDb一起使用,除非有合理的理由使用Excel自动化。
Example, figure 1 is a function to create a connection string which can be used in any project while figure 2 is for reading data. 例如,图1是创建连接字符串的功能,该功能可在任何项目中使用,而图2用于读取数据。
To work with Excel automation we open ourselves up to objects not being disposed of if there is a crash or that you do not code properly (this I call the two dot rule) when objects can't be released because of how you created and used automation objects which does not happen with OleDb. 为了使用Excel自动化,我们会在出现崩溃或由于您如何创建和使用对象而无法释放对象的情况下,对无法处理的对象开放说明OleDb不会发生的自动化对象。 Now if you wanted formatting than we move to automation. 现在,如果您要格式化,我们将转向自动化。
public string ConnectionString(string FileName, string Header)
{
OleDbConnectionStringBuilder Builder = new OleDbConnectionStringBuilder();
if (System.IO.Path.GetExtension(FileName).ToUpper() == ".XLS")
{
Builder.Provider = "Microsoft.Jet.OLEDB.4.0";
Builder.Add("Extended Properties", string.Format("Excel 8.0;IMEX=1;HDR={0};", Header));
}
else
{
Builder.Provider = "Microsoft.ACE.OLEDB.12.0";
Builder.Add("Extended Properties", string.Format("Excel 12.0;IMEX=1;HDR={0};", Header));
}
Builder.DataSource = FileName;
return Builder.ConnectionString;
}
Code to read the first column in Sheet2 and get distinct values, in this case I am working against a column with dates as string into List where the file resides in the same folder as the app executable 读取Sheet2中第一列并获取不同值的代码,在这种情况下,我正在将日期作为字符串的列与List一起使用,该文件与应用程序位于同一文件夹
private List<string> DemoDistinct()
{
List<string> dateList = new List<string>();
DataTable dt = new DataTable();
using (OleDbConnection cn = new OleDbConnection { ConnectionString = ConnectionString(System.IO.Path.Combine(Application.StartupPath, "WS1.xlsx"), "Yes") })
{
cn.Open();
using (OleDbCommand cmd = new OleDbCommand
{
CommandText = "SELECT DISTINCT [Dates] FROM [Sheet2$]",
Connection = cn
}
)
{
OleDbDataReader dr = cmd.ExecuteReader();
dt.Load(dr);
dateList = dt
.AsEnumerable()
.Select(row => row.Field<DateTime>("Dates").ToShortDateString()).ToList();
}
}
return dateList;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.