I can see a full set of common operations on C # excel on the Internet, which is comprehensive and well written.

Source: Internet
Author: User

Address: http://hi.baidu.com/kjkj911/blog/item/0ecc3ec7855dd6d4d100600f.html

Private _ workbook = NULL; private worksheet _ worksheet = NULL; private excel. application _ excelapplicatin = NULL; _ excelapplicatin = new excel. application (); _ excelapplicatin. visible = true; _ excelapplicatin. displayalerts = true; _ workbook = _ excelapplicatin. workbooks. add (xlsheettype. xlworksheet); _ worksheet = (worksheet) _ workbook. activesheet; _ worksheet. name = "worksheetname"; // open an existing Excel String strexcelpathname = appdomain. currentdomain. basedirectory + "excelsheetname.xls"; excel. workbook workbook = application. workbooks. open (strexcelpathname, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing); // read opened Excel files. worksheet worksheet1 = (Excel. worksheet) workbook. sheets ["sheetname1"]; excel. worksheet worksheet2 = (Excel. worksheet) workbook. sheets ["sheetname2"]; // Add a worksheet = (worksheet) workbook. worksheets. add (system. type. missing, system. type. missing, system. type. missing, system. type. missing); // rowheight "" indicates the first line, "" indicates the first line and the second line (Excel. range) _ worksheet. rows ["1:1", system. type. missing]). rowheight = 100; // columnwidth "A: B" indicates the first and second columns, and "A: A" indicates the first column (Excel. range) _ worksheet. columns ["A: B", system. type. missing]). columnwidth = 10; // Excel operation (hold down Alt + W for frozen fields and then press F) excel. range excelrange = _ worksheet. get_range (_ worksheet. cells [10, 5], _ worksheet. cells [10, 5]); excelrange. select (); excelapplication. activewindow. freezepanes = true; // borders. linestyle cell border line excel. range excelrange = _ worksheet. get_range (_ worksheet. cells [2, 2], _ worksheet. cells [4, 6]); // cell border line type (line type, dotted line type) excelrange. borders. linestyle = 1; excelrange. borders. get_item (xlbordersindex. xledgetop ). linestyle = excel. xllinestyle. xlcontinuous; // specify the box width and color excelrange under the cell. borders. get_item (xlbordersindex. xledgebottom ). weight = excel. xlborderweight. xlmedium; excelrange. borders. get_item (xlbordersindex. xledgebottom ). colorindex = 3; // set the font size to excelrange. font. size = 15; // set whether the font contains the underline excelrange. font. underline = true; // sets the excelrange for the font in the cell. horizontalalignment = xlhalign. xlhaligncenter; // sets the cell width excelrange. columnwidth = 15; // set the cell background color excelrange. cells. interior. color = system. drawing. color. fromargb (255,204,153 ). toargb (); // Add a border excelrange to the cell. borderaround (xllinestyle. xlcontinuous, xlborderweight. xlthick, xlcolorindex. xlcolorindexautomatic, system. drawing. color. black. toargb (); // automatically adjusts the column width excelrange. entirecolumn. autofit (); // excelrange in horizontal text center mode. horizontalalignment = excel. xlhalign. xlhaligncenter; // Text wrap excelrange. wraptext = true; // The fill color is lavender excelrange. interior. colorindex = 39; // merge cells excelrange. merge (excelrange. mergecells); _ worksheet. get_range ("A15", "B15 "). merge (_ worksheet. get_range ("A15", "B15 "). mergecells); // <summary> // definition of common colors, for the color name in Excel /// </Summary> Public Enum colorindex {colorless =-4142, automatic =-4105, black = 1, brown = 53, Olive = 52, dark green = 51, dark blue = 49, dark blue = 11, indigo = 55, gray 80 = 56, dark red = 9, Orange = 46, dark yellow = 12, Green = 10, Blue = 14, Blue = 5, blue gray = 47, gray 50 = 16, Red = 3, light orange = 45, Acid Orange = 43, sea green = 50, water green = 42, light blue = 41, Violet = 13, gray 40 = 48, pink = 7, Gold = 44, yellow = 6, fresh green = 4, Green = 8, Blue = 33, merceon = 54, gray 25 = 15, Rose Red = 38, brown = 40, light yellow = 36, light Green = 35, light green = 34, light blue = 37, light purple = 39, white = 2}

 

  1. Range. numberformatlocal = "@"; // set the cell format to text.
  2. Range = (range) worksheet. get_range ("A1", "E1"); // obtain multiple cells in Excel. In this example, the table is used as the Excel header.
  3. Range. Merge (0); // cell merge action
  4. Worksheet. cells [1, 1] = "Excel cell assignment"; // Excel cell assignment
  5. Range. Font. size = 15; // set the font size.
  6. Range. Font. Underline =True; // Set whether the font contains underscores
  7. Range. Font. Name = ""; set the font type
  8. Range. horizontalalignment = xlhalign. xlhaligncenter; // you can specify the font type in a cell.
  9. Range. columnwidth = 15; // you can specify the cell width.
  10. Range. cells. Interior. Color = system. Drawing. color. fromargb (255,204,153). toargb (); // you can specify the background color of a cell.
  11. Range. Borders. linestyle = 1; // you can specify the Border width of a cell.
  12. Range. borderaround (xllinestyle. xlcontinuous, xlborderweight. xlthick, xlcolorindex. xlcolorindexautomatic, system. Drawing. color. Black. toargb (); // border the cell
  13. Range. borders. get_item (Microsoft. office. interOP. excel. xlbordersindex. xledgetop ). linestyle = Microsoft. office. interOP. excel. xllinestyle. xllinestylenone; // you can specify no border as the upper border of a cell.
  14. Range. entirecolumn. autofit (); // automatically adjusts the column width.
  15. Range. horizontalalignment = xlcenter; // horizontal text center Mode
  16. Range. verticalignment = xlcenter // text vertical center Mode
  17. Range. wraptext =True; // Text wrap
  18. Range. Interior. colorindex = 39; // fill color is lavender
  19. Range. Font. Color = clblue; // font color
  20. Xlsapp. displayalerts =False; // When saving the Excel file, save the file directly without the pop-up window.
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.