C # use Excel to read data directly into the datagridview

Source: Internet
Author: User
Drag a datagridview control in winform and an openfiledialog control. The function of this example is to read an Excel file. I am still studying other functions. Code
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using Microsoft. office. core; using Excel = Microsoft. office. interOP. excel; using system. windows. forms; using system. reflection; namespace excelproject {public partial class excelform: FORM {private exceloperate EO; private string excelpath; private excel. application excel1; private excel. workbooks WBS = NULL; private excel. workbook WB = NULL; private excel. sheets WSS; private excel. worksheet Ws = NULL; private excel. range range1 = NULL; Public excelform () {initializecomponent (); this. excel1 = new excel. application (); If (excel1 = NULL) {MessageBox. show ("error"); system. windows. forms. application. exit () ;}excel1.visible = true ;}# open region Excel file close operation private void Open _ click (Object sender, eventargs e) {openfiledialog1 = new openfiledialog (); openfiledialog1.title = "open an Excel file"; openfiledialog1.filter = "excel03 file (*. XLS) | *. XLS | excel07 file (*. XLSX) | *. XLSX "; openfiledialog1.initialdirectory = @" C: \ Users \ Administrator \ Desktop "; openfiledialog1.restoredirectory = true; If (openfiledialog1.showdialog () = dialogresult. OK) {// select the file excelpath = openfiledialog1.filename; EO = new exceloperate (); readexcel (excelpath);} void readexcel (string path) {object Miss = system. reflection. missing. value; excel1.usercontrol = true; excel1.displayalerts = false; excel1.application. workbooks. open (excelpath, miss, miss); WBS = excel1.workbooks; WSS = WBS [1]. worksheets; Ws = (Excel. worksheet) WSS. get_item (1); int rownum = ws. usedrange. cells. rows. count; int colnum = ws. usedrange. cells. columns. count; string cellstr = NULL; char CH = 'a'; For (INT I = 0; I <colnum; I ++) {datagridview1.columns. add (I. tostring (), ch. tostring (); datagridview1.rows. add (rownum); For (Int J = 0; j <rownum; j ++) {cellstr = CH. tostring () + (J + 1 ). tostring (); datagridview1 [I, j]. value = ws. usedrange. cells. get_range (cellstr, Miss ). text. tostring () ;}ch ++ ;}# endregion }}

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.