Read from Excel file under C #

Source: Internet
Author: User

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 #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.