Excel action "1" in C #--a way to set the contents of an Excel cell to open an Excel file

Source: Internet
Author: User

Objective

As a member of the Project Management Brigade, the most contact in the company is the Excel file, so from the beginning, I want to start with Excel, learn a simple two times to develop, start their own programming road!

Program Interface

Function description

The Open File button allows the user to specify the Excel file to manipulate and displays the file path in a later textbox.

Set the cell button to set the contents of the Excel file according to the program.

Exit the program button to close the form.

Program source Code

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 usingSystem.Reflection;Ten usingMicrosoft.Office.Interop.Excel; One namespaceExcelreadandwrite A { -      Public Partial classForm1:form -     { the          PublicString filename =string. Empty; -          PublicForm1 () -         { - InitializeComponent (); +         } -         /// <summary> +         ///Open the Excel file, and set the value of the setting cell A         /// </summary> at         /// <param name= "Sender" ></param> -         /// <param name= "E" ></param> -         Private voidButton1_Click (Objectsender, EventArgs e) -         { -             if(Textbox_filename.text! ="") -             { in                 //This is a way to read an Excel file -Microsoft.Office.Interop.Excel.Application app =NewMicrosoft.Office.Interop.Excel.Application (); toWorkbook Wbook =app. Workbooks.Open (filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, + Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, type.missing); -  theWorksheet Worksheet = (Worksheet) wbook. worksheets[1]; *Worksheet. cells[1,1] ="ID"; $Worksheet. cells[1,2] ="Value";Panax Notoginseng                  for(inti =2; I < -; i++ ) -                 { theWorksheet. Cells[i,1] =i.tostring (); +Worksheet. Cells[i,2] = i *i; A                 } theMessageBox.Show ("has been set up successfully! "); +  -                 //Save and exit an Excel file $ Wbook. Save (); $Worksheet =NULL; -Wbook =NULL; - app. Quit (); theApp =NULL; -             }Wuyi             Else the             { -MessageBox.Show ("file not specified yet! "); Wu             } -         } About         /// <summary> $         ///Close the form -         /// </summary> -         /// <param name= "Sender" ></param> -         /// <param name= "E" ></param> A         Private voidButton_quit_click (Objectsender, EventArgs e) +         { the              This. Close (); -         } $         /// <summary> the         ///gets the filename of the specified file the         /// </summary> the         /// <param name= "Sender" ></param> the         /// <param name= "E" ></param> -         Private voidButton_openfile_click (Objectsender, EventArgs e) in         { theOpenFileDialog OFD =NewOpenFileDialog (); theOfd. InitialDirectory ="e:\\"; AboutOfd. Filter="Excel file |*.xlsx"; theOfd. Restoredirectory =true; theOfd. FilterIndex =1; the             if(OFD. ShowDialog () = =DialogResult.OK) +             { -filename =OFD. FileName; theTextbox_filename.text =filename;Bayi             } the         } the     } -}

Excel action "1" in C #--a way to set the contents of an Excel cell to open an Excel file

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.