Visual Studio 2005 C # Read and write Excel files

Source: Internet
Author: User

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

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.