Recently in an order management software, consider reading Excel directly and then displaying the data on the DataGridView. , and then use Npoi to manipulate Excel.
1 stringStrcon ="Provider=microsoft.ace.oledb.12.0;data source="+ Path +"; Extended properties= ' Excel 12.0; Hdr=yes;imex=0 '";2 //string Strcon = "Provider=microsoft.ace.oledb.12.0;data source=d:/score table 2013.xlsx; Extended properties= ' Excel 12.0; Hdr=yes;imex=0 ' ";3OleDbConnection myconn =NewOleDbConnection (Strcon);4 stringSselect ="SELECT * FROM [sheet1$]";5 MyConn.Open ();6OleDbDataAdapter MyDataAdapter =NewOleDbDataAdapter (Sselect, myconn);7DataSet myDataSet =NewDataSet ();8Mydataadapter.fill (myDataSet);//, "[sheet1$]");9 myconn.close ();TenDatagridview_items. DataSource = mydataset.tables[0]. DefaultView;//Show to DataGridView
This method is very inefficient and shows that the data is brushed out one line at a time and is not available. Then choose to use the lightweight sqlite3:
Download the relevant dll:https://www.sqlite.org/download.html downloads on the website below
. NET tools:
Load SQLite
using System.Data.SQLite; using System.Data.SqlClient;
Use:
Pay attention to the use of try,catch, otherwise it will not show the data and do not hold the wrong. After try catch, you will find that you cannot load SQLite.Interop.dll. You need to place the DLL under the project path, and then set the property to copy to the output value: Always copy:
if(file.exists (path)) {Try{Sqliteconnection conn=NewSqliteconnection ("Data Source ="+path); Conn. Open (); Sqlitedataadapter DataAdapter=NewSqlitedataadapter ("Select * FROM Product", conn); DataSet DS=NewDataSet (); DataAdapter.Fill (DS); Datagridview_items. DataSource= ds. tables[0]; Conn. Close (); } Catch(System.Exception ex) {MessageBox.Show (ex). Message); } } Else{MessageBox.Show ("No related database found"); }
C # reads the data and displays the Dataggrideview. Use Excel first, then use Sqlite3.