Home > Net >  C # how to query data in excle?
C # how to query data in excle?

Time:11-04


This figure, for example, input id card, can I get a number a few?
I type 19910101, for example, by number 1,
C # how to implement? Various for baidu can not find,,
Feel bosses!

CodePudding user response:

Suggest the execl first inside all columns read encapsulated together, want to read either such as: number, name, id card, encapsulated into objects, all on the list... As for how to get the data inside, baidu

CodePudding user response:

Your action should be the excel data read into memory first, and then to find the data, by means of linq query

CodePudding user response:

With NPOI read into memory, first traverse to read again

CodePudding user response:

CloseXML NPOI.

CodePudding user response:

Will excel data table import database query again read

CodePudding user response:

Solved, thank you for you upstairs navel-gazing,
The first step, will excel to a DataTable,
The second step, the Select method to solve,
Is also simple and clear, Nice!

DataTable dt=Excel_ylz1. XlsxToDataTable (" library. XLSX ");
DataRow [] s=dt. Select (" id='203255515' ");
Console. WriteLine (s [0]. ItemArray [2]).

CodePudding user response:

Use oledb excel as a data source query, select Numbers from sheet1 where id='19910101'

CodePudding user response:


Refer to the two namespace
Using System. Collections. Generic;
Using System. The Data;


Installation: AccessDatabaseEngine this plugin


The code below
The static void Main (string [] args)
{
String filePath=@ "you put the file path";
String strCon="Provider=Microsoft. ACE. The OLEDB. 12.0; Data Source="+ filePath +"; Extended Properties='Excel Xml 12.0; HDR=YES; IMEX=1 '";
System. Data. The OleDb. OleDbConnection myConn=new System. The Data. The OleDb. OleDbConnection (strCon);
MyConn. The Open ();
The DataTable schemaTable=myConn. GetOleDbSchemaTable (System) Data. The OleDb. OleDbSchemaGuid. Tables, null);
String tableName=schemaTable. Rows [0] [2]. The ToString (). The Trim ();//tag name
String strCom="Select * from [Sheet1 $]".//query
System. Data. The OleDb. OleDbDataAdapter myCommand=new System. The Data. The OleDb. OleDbDataAdapter (strCom myConn);
//create a DataSet object
The DataSet myDataSet=new DataSet ();
//get yourself a DataSet object
MyCommand. The Fill (myDataSet);
//close this data link
MyConn. Close ();
The DataTable dtTask=myDataSet. Tables [0];

List List=new List (a);
For (int k=0; K & lt; DtTask. Rows. Count; K++)
{
The userInfo model=new the userInfo ();
Model. The id=the Convert. ToInt32 (dtTask Rows [k] [r]. "number" ToString ());
Model. The name=dtTask. Rows [k] [r]. "name" ToString ();
Model. The identity=dtTask. Rows [k] [r]. "id card" the ToString ();
List. The Add (model);
}
Console. WriteLine (" query article number "+ list. The Count ());
Console. WriteLine (" please enter the id number ");
String Identity=Console. ReadLine ();
Int id=(list. The Where (o=& gt; O.i dentity==Identity). FirstOrDefault ()?? New the userInfo ()). The id;
If (id & gt; 0)
{
Console. WriteLine (" the number you want to find is: "+ id);
}
The else
{
Console. WriteLine (" had not found you want to find the serial number of!" );
}
}


Public class the userInfo
{
///& lt; Summary>
///no.
///& lt;/summary>
Public int id {get; set; }
///& lt; Summary>
///name
///& lt;/summary>
Public string name {get; set; }
///& lt; Summary>
///id
///& lt;/summary>
Public string identity {get; set; }
}

CodePudding user response:

You can use LINQ

CodePudding user response:

Upstairs someone right, oledb is simple and fast
  •  Tags:  
  • C#
  • Related