When doing homework check a bit of information, with the VS2K5 read Excel -find it very simple to use ... Now the programming language has nothing to say! 1Project-Add Reference-com-microsoft Excel12.0Object Library&&-microsoft Office12.0Object Library2usingMicrosoft.Office.Core;usingMicrosoft.Office.Interop.Excel;usingSystem.IO;usingSystem.Reflection;3 stringOriginalfile = System.Windows.Forms.Application.StartupPath +@". \a.xlsx"; stringOutputFile; SaveFileDialog Save=NewSaveFileDialog (); Save. InitialDirectory="d:\\"; Save. Filter="Excel Files (*.xlsx) |*.xlsx| All Files (*. *) |*.*"; Save. FilterIndex=1; Save. Restoredirectory=true; if(Save. ShowDialog () = =DialogResult.OK) {Try{outputFile=save. FileName; System.IO.File.Copy (Originalfile, OutputFile,true); Excelrs=NewMicrosoft.Office.Interop.Excel.ApplicationClass (); //Open the destination file outputfileRsbook =ExcelRS.Workbooks.Open (OutputFile, missing, missing, missing, missing, missing, missing, Missi Ng, missing, missing, missing, missing, missing, missing, missing); //set the first job PuRssheet = (Microsoft.Office.Interop.Excel.Worksheet) RSbook.Sheets.get_Item (1); //activating current Work Purssheet.activate (); rssheet.cells[1,1] =DataGridView1.SelectedRows.Count; for(inti =0; i < DataGridView1.SelectedRows.Count; ++i) { for(intj =0; J < One; ++j) {Rssheet.cells[i+2, J +1] =Datagridview1.rows[datagridview1.selectedrows[i]. Index]. CELLS[J]. Value.tostring (). Trim (); //Rssheet.cellsRssheet.get_range (Rssheet.cells[i +2, J +1], Rssheet.cells[i +2, J +1]). Entirecolumn.columnwidth = +; //Rssheet.get_range (Rssheet.cells[i + 2, j + 1], missing). Auto}} rsbook.save (); Excelrs.displayalerts=true; Excelrs.visible=true; } Catch(System.Exception ex) {MessageBox.Show (ex). Message); } finally{excelrs.quit (); } }4OpenFileDialog Open=NewOpenFileDialog (); Open. InitialDirectory=@"D:\"; Open. Filter="Excel Files (*.xlsx) |*.xlsx| All Files (*. *) |*.*"; Open. FilterIndex=1; Open. Restoredirectory=true; if(Open. ShowDialog () = =DialogResult.OK) {Try{Excelrs=NewMicrosoft.Office.Interop.Excel.ApplicationClass (); Rsbook=ExcelRS.Workbooks.Open (Open. FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, Missing, missing, missing, missing); Rssheet= (Microsoft.Office.Interop.Excel.Worksheet) RSbook.Sheets.get_Item (1); Rssheet.activate (); Microsoft.Office.Interop.Excel.Range Range= Rssheet.get_range ("A"+I, Type.Missing); Counts=int. Parse (range. Text.tostring (). Trim ()); } Catch(System.Exception ex) {MessageBox.Show (ex). Message); } finally{excelrs.quit (); Conn. Close (); }} comment This forwarded to Weibo to be forwarded to Weibo read (675)| Reviews0) |use "sweep" to share the article in a circle of friends. Use easy letter "sweep" to share the article to the Circle of friends. Like to recommend 0 people| Reproduced
Visual Studio 2005 C # Read and write Excel files