C # Excel Row Height, column width, merge cells, cell border lines, and freeze

Source: Internet
Author: User

Private _ Workbook _ 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 );
// 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, tea color = 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. 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 ;&

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.