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 }}