C # edit the worksheet name in EXCEL

Source: Internet
Author: User

[Csharp]
</Pre> <pre name = "code" class = "plain">
Let's make a data capture software and store it in excel.
For good looks, of course, we need to change the sheet name. After studying it for a long time, we finally made a great effort and shared it with you.

Use C # To create an EXCEL file see http://www.bkjia.com/kf/201204/129635.html

First, we need to reference it in the C # project:

Right-click a project -- add reference -- COM -- Microsoft Excel 12.0 Object Library

Here, 12.0 is a 2007 database, that is, the excel document in xlsx format can be operated.

Enter two sentences in using:

[Csharp]
Using MSExcel = Microsoft. Office. Interop. Excel;
Using System. Reflection;
In fact, the above sentence is short for short, so that we can use the "class" of MSExcel below.
[Csharp]
MSExcel. Application excelApp; // Excel Application
MSExcel. Workbook excelDoc; // Excel document

The two documents are defined. If you want to see the details, create an excel file. Here we will explain how to change the Sheet Name:
[Csharp]
MSExcel. Worksheet ws = (MSExcel. Worksheet) excelApp. Worksheets. get_Item (1 );
Ws. Name = "Fox! ";
All right, we can see that the old "sheet1" is changed to "Fox! "This sheet
Let's give a complete function. Many of the functions I copied do not know what to use. please correct me if there are any mistakes!

[Csharp]
Public void CreateExcel (string path)
{
MSExcel. Application excelApp; // Excel Application
MSExcel. Workbook excelDoc; // Excel document
Path = @ "c: \ test.xlsx ";
ExcelApp = new MSExcel. ApplicationClass ();
If (File. Exists (path ))
{
File. Delete (path );
}
Object nothing = Missing. Value;
ExcelDoc = excelApp. Workbooks. Add (nothing );
MSExcel. Worksheet ws = (MSExcel. Worksheet) excelApp. Worksheets. get_Item (1 );
Ws. Name = "Fox! ";
Object format = MSExcel. XlFileFormat. xlWorkbookDefault;
ExcelDoc. SaveAs (path, nothing,
MSExcel. XlSaveAsAccessMode. xlExclusive, nothing, nothing );
ExcelDoc. Close (nothing, nothing, nothing );
ExcelApp. Quit ();
}

 

Roar, it seems that there is a lot of nonsense. Writing so much is to help beginners understand it, because they are suffering from a loss because the tutorials written by others are not clear. I hope you can discuss them together.

 

From icyfox_bupt

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.