C # Excel row height, column width, merged cells, cell border lines, frozen

Source: Internet
Author: User
Tags dashed line

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
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 an open Excel
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, System.Type.Missing, System.Type.Missing);

RowHeight "1:1" for the first row, and "1:2" for the first and second rows
((Excel.Range) _worksheet.rows["1:1", System.Type.Missing]). RowHeight = 100;

ColumnWidth "A:b" represents the first column and the second column, "A:a" represents the first column
((Excel.Range) _worksheet.columns["A:b", System.Type.Missing]). ColumnWidth = 10;

Excel operations (fields that need to be frozen hold 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 (Linetype, dashed line)
ExcelRange.Borders.LineStyle = 1;
ExcelRange.Borders.get_Item (Xlbordersindex.xledgetop). LineStyle = Excel.XlLineStyle.xlContinuous;
Specify the thickness of the border line, and the color of the cell
ExcelRange.Borders.get_Item (Xlbordersindex.xledgebottom). Weight = Excel.XlBorderWeight.xlMedium;

ExcelRange.Borders.get_Item (Xlbordersindex.xledgebottom). ColorIndex = 3;

//Set font size
ExcelRange.Font.Size =;
Sets whether the font has an underscore
ExcelRange.Font.Underline = true;  

//Set how the font is within the cell
Excelrange.horizontalalignment = xlhalign.xlhaligncenter;
Set the width of the cell
Excelrange.columnwidth =;
Sets the background color of the cell
ExcelRange.Cells.Interior.Color = System.Drawing.Color.FromArgb (255, 204, 153). ToArgb ();
//Add border to cell
Excelrange.borderaround (xllinestyle.xlcontinuous, xlborderweight.xlthick, 
                                             xlcolorindex.xlcolorindexautomatic, System.Drawing.Color.Black.ToArgb ());
//AutoFit column width
excelRange.EntireColumn.AutoFit ();
Text horizontal centered mode
Excelrange.horizontalalignment = excel.xlhalign.xlhaligncenter;            &NBSP
//text wrap
Excelrange.wraptext = true;
Fill color is lavender
ExcelRange.Interior.ColorIndex =;

Merge cells
Excelrange.merge (Excelrange.mergecells);
_worksheet.get_range ("A15", "B15"). Merge (_worksheet.get_range ("A15", "B15"). MergeCells);

<summary>
Common color definitions, for color names in Excel
</summary>
public enum ColorIndex
{
Colorless =-4142, auto =-4105, black = 1, Brown = 53, Olive = 52, dark green = 51, Dark Turquoise = 49,
Dark blue = 11, Indigo = 55, Gray 80 = 56, crimson = 9, orange = 46, Dark yellow = 12, green = 10,
Cyan = 14, blue = 5, blue Gray = 47, Gray 50 = 16, red = 3, light orange = 45, sour orange = 43,
Sea green = 50, aqua green = 42, light blue = 41, violet = 13, Gray 40 = 48, Pink = 7,
Gold = 44, yellow = 6, bright green = 4, turquoise = 8, sky blue = 33, Plum = 54, Gray 25 = 15,
Rose = 38, tan = 40, pale yellow = 36, light green = 35, shallow turquoise = 34, pale blue = 37, light violet = 39,
White = 2
}

      1. Range. NumberFormatLocal =  "@";      // Format cells as text    
      2.   
      3. range = (range) Worksheet.get_range (// Get more than one range of cells in Excel: This example is an Excel header    
      4.   
      5. range. Merge (0)      //cell merge action    
      6.   
      7. worksheet. Cells[1, 1] = //excel cell assignment    
      8.   
      9. range. Font.Size = 15;    &

C # Excel row height, column width, merged cells, cell border lines, frozen

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.