C # basic Excel operations

Source: Internet
Author: User

The main content of this article: C # implements the open, save, merge cell, and cell assignment operations on Excel.

Excelhelper code:

1 class excelhelper
2 {
3 publicstaticvoid savedata ()
4 {
5 string filepath = environment. currentdirectory + "\ data ";
6 if (! Directory. exists (filepath ))
7 {
8 directory. createdirectory (filepath );
9}
10 string filename = string. Format ("{0 }\{ 12.16.xls", filepath, datetime. Now. tow.datestring ());
11
12 excel. Application excelapp = new excel. applicationclass ();
13 // create a workbook
14 if (! File. exists (filename ))
15 {
16 excel. Workbook workbooknew = excelapp. workbooks. Add (true );
17 // create a worksheet
18 excel. worksheet worksheetnew = workbooknew. activesheet as Excel. worksheet;
19 workbooknew. saveas (filename, missing. value, missing. value, missing. value, missing. value, missing. value, Excel. xlsaveasaccessmode. xlnochange, missing. value, missing. value, true, false, missing. value );
20}
21. Excel. workbook workbook = excelapp. workbooks. open (filename, missing. value, missing. value, missing. value, missing. value, missing. value, false, missing. value, missing. value, true, false, missing. value, false );
22
23 // create a worksheet
24 excel. worksheet = Workbook. activesheet as Excel. worksheet;
25
26 excel. Range r = worksheet. get_range (
27 worksheet. cells [1, 1], Worksheet. cells [3, 3]); // select a cell
28 R. mergecells = true;
29
30 R. value2 = "I love China"; // set the text in the cell
31 R. Font. Name = ""; // set the font
32 R. Font. size = 20; // font size
33 r. Font. Bold = true; // bold display
34 R. horizontalalignment = excel. xlhalign. xlhaligncenter; // horizontally centered
35 R. verticalalignment = excel. xlvalign. xlvaligncenter; // center vertically
36
37 worksheet. cells [4, 1] = "singing to the motherland";
38 worksheet. cells [5, 2] = "reading for the rise of China";
39 excelapp. displayalerts = false;
40 excelapp. alertbeforeoverwriting = false;
41 Workbook. Save ();
42 excelapp. Quit ();
43 system. runtime. interopservices. Marshal. releasecomobject (excelapp );
44 excelapp = NULL;
45 system. gc. Collect ();
46}
47}
48

Program code:

1 class Program
2 {
3 staticvoid Main(string[] args)
4 {
5 Console.Title ="Excel Operation Example";
6 ExcelHelper.SaveData();
7 Environment.Exit(0);
8 }
9 }

Result:

I love China  
 
 
Singing to the motherland    
  Reading for the rise of China
       
       

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.