[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