C # Modifies the value of an Excel-specified cell

Source: Internet
Author: User

<summary>

Writes the specified string to the specified cell
</summary>
<param name= "Data" > DataSource to write </param>
<param name= "SheetName" > Sheet name (full path +excel table name) </param>
<param name= "Row" > The first few lines </param>
<param name= "column" > Columns </param>
public void WriteData (string data,string sheetname, int row, int column)
{
Try
{

Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application ();//Instantiate an Excel object
Object missing = system.reflection.missing.value;//Gets the missing object type value
Open the specified Excel file
Excel. Application.DisplayAlerts = false;//does not display a prompt dialog box


Microsoft.Office.Interop.Excel.Workbook Workbook = Excel. Workbooks.Open (SheetName,
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);//Open Excel
Microsoft.Office.Interop.Excel.Sheets Sheets = workbook. worksheets;//instance Table
Microsoft.Office.Interop.Excel.Worksheet Worksheet = (Microsoft.Office.Interop.Excel.Worksheet) sheets[1];//first table

Worksheet. Cells[row,column].value = data;
Workbook. Save ();//Saving worksheet
Workbook. Close (false, missing, missing);//Close sheet
}
catch (Exception e)
{
MessageBox.Show (E.message.tostring ());
}

}

C # Modifies the value of an Excel-specified cell

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.