The specific source code is as follows, display opens the file browser selects the Excel file, reads the file, finally displays reads the partial content.
Public partial class Mainwindow:window {public MainWindow () {InitializeComponent (); }//Add two functions, one is to read the Excel file function, and the other is to write data into the Excel file function. public static DataTable Loaddatafromexcel (String filepath,string sheetname) {DataSet GetData = new Data Set (); String strconn = "provider=microsoft.jet.oledb.4.0;" + "Data source=" + FilePath + "; Extended properties= ' Excel 8.0; Hdr=no;imex=1 '; "; String strexcel = String. Format ("select * from [{0}$]", sheetname); Connect data source OleDbConnection conn = new OleDbConnection (strconn); Conn. Open (); Fit to Data source OleDbDataAdapter adapter = new OleDbDataAdapter (Strexcel, strconn); Adapter. Fill (GetData, sheetname); Conn. Close (); Return GetData. Tables[sheetname]; } public static void Writedatatoexcel (DataSet DS, String filePath) {}//Open file Browser, select the desired file private void Btnopen_click (object sender, RoutedEventArgs e) {OpenFileDialog FileDialog = new Openfil Edialog (); Filedialog.multiselect = false; Filedialog.title = "Please select File"; Filedialog.filter = "Excel file|*.xlsx"; if (filedialog.showdialog () = = System.Windows.Forms.DialogResult.OK) {string file = FileDialog. FileName; System.Windows.Forms.MessageBox.Show ("Selected file:" + file); Pathtxt.text = file; }} private void Btntransfer_click (object sender, RoutedEventArgs e) {DataTable furtable = Loaddatafromexcel (Pathtxt.text, "Nov FUR"); DataRow dtrow = furtable.rows[0]; object[] Rowitem = Dtrow.itemarray; Pathtxt.text = ""; foreach (object item in Rowitem) {if (item. ToString () = null) {Pathtxt.text + = Item. ToString (); Pathtxt.text + = ""; }}//system.windows.forms.messagebox.show (Dtrow[0]. ToString ()); } }
Read from Excel file under C #