C # Excel Row Height, column width, merge cells, cell border lines, freeze-common operations on C # excle

Source: Internet
Author: User

 

 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 File              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 the opened Excel File Excel. worksheet worksheet1 = (Excel. worksheet) Workbook. Sheets [ "Sheetname1" ]; Excel. worksheet worksheet2 = (Excel. worksheet) Workbook. Sheets [ "Sheetname2" ]; // Add a worksheet Worksheet worksheet = (worksheet) Workbook. worksheets. Add (system. type. Missing, system. type. Missing ); // Rowheight "" indicates the first line, and "" 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 the frozen field Alt + W 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 width and color of the box below the cell Excelrange. Borders. get_item (xlbordersindex. xledgebottom). Weight = excel. xlborderweight. xlmedium; excelrange. Borders. get_item (xlbordersindex. xledgebottom). colorindex = 3; // Set the font size Excelrange. Font. size = 15; // Set whether the font contains underscores. Excelrange. Font. Underline = True ; // Set the font type in the cell. Excelrange. horizontalalignment = xlhalign. xlhaligncenter;// Set 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 to the cell bar Excelrange. borderaround (xllinestyle. xlcontinuous, xlborderweight. xlthick, xlcolorindex. xlcolorindexautomatic, system. Drawing. color. Black. toargb ()); // Automatically adjust the column width Excelrange. entirecolumn. autofit (); // Horizontal text center Mode Excelrange. 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.  /// </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, Sky Blue = 33, merceon = 54, gray 25 = 15, Rose Red = 38, brown = 40, light yellow = 36, light green = 35, light blue = 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. This example is used as the Excel header.
  3. Range. Merge (0 );// Cell Merging
  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;// Set the font type in the cell.
  9. Range. columnwidth = 15; // you can specify the cell width.
  10. Range. cells. Interior. Color = system. Drawing. color. fromargb (255,204,153). toargb ();// Set the cell background color
  11. Range. Borders. linestyle = 1;// Set the cell Border Width
  12. Range. borderaround (xllinestyle. xlcontinuous, xlborderweight. xlthick, xlcolorindex. xlcolorindexautomatic, system. Drawing. color. Black. toargb ());// Add a border to the cell bar
  13. Range. Borders. get_item (Microsoft. Office. InterOP. Excel. xlbordersindex. xledgetop). linestyle = Microsoft. Office. InterOP. Excel. xllinestyle. xllinestylenone;// Set the cell top border to borderless
  14. Range. entirecolumn. autofit ();// Automatically adjust the column width
  15. Range. horizontalalignment = xlcenter;// Horizontal text center Mode
  16. Range. verticalalignment = xlcenter// Vertical text Center
  17. Range. wraptext =True;// Text wrap
  18. Range. Interior. colorindex = 39;// The fill color is lavender.
  19. Range. Font. Color = clblue;// Font color
  20. Xlsapp. displayalerts =False;// When saving the Excel file, the Save or not window is not displayed and saved directly.

 

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.