C# 對Excel 單元格格式, 及行高、 列寬、 單元格邊框線、 凍結設置


 

一、對行高,列寬、單元格邊框等的設置

這篇簡短的文字對單元格的操作總結的比較全面,特此轉載過來。

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";

//打開已存在的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);

//讀取已打開的Excel Excel.Worksheet workSheet1 = (Excel.Worksheet)workBook.Sheets["SheetName1"]; Excel.Worksheet workSheet2 = (Excel.Worksheet)workBook.Sheets["SheetName2"]; //添加一個workSheet Worksheet workSheet = (Worksheet)workBook.Worksheets.Add(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); //RowHeight "1:1"表示第一行, "1:2"表示,第一行和第二行 ((Excel.Range)_workSheet.Rows["1:1", System.Type.Missing]).RowHeight = 100; //ColumnWidth "A:B"表示第一列和第二列, "A:A"表示第一列 ((Excel.Range)_workSheet.Columns["A:B", System.Type.Missing]).ColumnWidth = 10; // EXCEL操作(需要凍結的字段 按住ALT+W 再按F) Excel.Range excelRange = _workSheet .get_Range(_workSheet .Cells[10, 5], _workSheet .Cells[10, 5]); excelRange.Select(); excelApplication.ActiveWindow.FreezePanes = true; //Borders.LineStyle 單元格邊框線 Excel.Range excelRange = _workSheet.get_Range(_workSheet.Cells[2, 2], _workSheet.Cells[4, 6]); //單元格邊框線類型(線型,虛線型) excelRange.Borders.LineStyle = 1; excelRange.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous; //指定單元格下邊框線粗細,和色彩 excelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlMedium; excelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).ColorIndex =3; //設置字體大小 excelRange.Font.Size = 15; //設置字體是否有下划線 excelRange.Font.Underline = true; //設置字體在單元格內的對其方式 excelRange.HorizontalAlignment = XlHAlign.xlHAlignCenter; //設置單元格的寬度 excelRange.ColumnWidth = 15; //設置單元格的背景色 excelRange.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb(); // 給單元格加邊框 excelRange.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThick, XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
//自動調整列寬 excelRange.EntireColumn.AutoFit(); // 文本水平居中方式 excelRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; //文本自動換行 excelRange.WrapText = true; //填充顏色為淡紫色 excelRange.Interior.ColorIndex = 39; //合並單元格 excelRange.Merge(excelRange.MergeCells); _workSheet.get_Range("A15", "B15").Merge(_workSheet.get_Range("A15", "B15").MergeCells); /// <summary> /// 常用顏色定義,對就Excel中顏色名 /// </summary> public enum ColorIndex { 無色 = -4142, 自動 = -4105, 黑色 = 1, 褐色 = 53, 橄欖 = 52, 深綠 = 51, 深青 = 49, 深藍 = 11, 靛藍 = 55, 灰色80 = 56, 深紅 = 9, 橙色 = 46, 深黃 = 12, 綠色 = 10, 青色 = 14, 藍色 = 5, 藍灰 = 47, 灰色50 = 16, 紅色 = 3, 淺橙色 = 45, 酸橙色 = 43, 海綠 = 50, 水綠色 = 42, 淺藍 = 41, 紫羅蘭 = 13, 灰色40 = 48, 粉紅 = 7, 金色 = 44, 黃色 = 6, 鮮綠 = 4, 青綠 = 8, 天藍 = 33, 梅紅 = 54, 灰色25 = 15, 玫瑰紅 = 38, 茶色 = 40, 淺黃 = 36, 淺綠 = 35, 淺青綠 = 34, 淡藍 = 37, 淡紫 = 39, 白色 = 2 }

 

Code segment2

    range.NumberFormatLocal = "@";     //設置單元格格式為文本
    range = (Range)worksheet.get_Range("A1", "E1");     //獲取Excel多個單元格區域:本例做為Excel表頭
    range.Merge(0);     //單元格合並動作
    worksheet.Cells[1, 1] = "Excel單元格賦值";     //Excel單元格賦值
    range.Font.Size = 15;     //設置字體大小
    range.Font.Underline=true;     //設置字體是否有下划線
    range.Font.Name="黑體";       設置字體的種類   
    range.HorizontalAlignment=XlHAlign.xlHAlignCenter;     //設置字體在單元格內的對其方式
    range.ColumnWidth=15;     //設置單元格的寬度
    range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb();     //設置單元格的背景色
    range.Borders.LineStyle=1;     //設置單元格邊框的粗細
    range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb());     //給單元格加邊框
    range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //設置單元格上邊框為無邊框
        range.EntireColumn.AutoFit();     //自動調整列寬
    Range.HorizontalAlignment= xlCenter;     // 文本水平居中方式
    Range.VerticalAlignment= xlCenter     //文本垂直居中方式
    Range.WrapText=true;     //文本自動換行
    Range.Interior.ColorIndex=39;     //填充顏色為淡紫色
    Range.Font.Color=clBlue;     //字體顏色
    xlsApp.DisplayAlerts=false;     //保存Excel的時候,不彈出是否保存的窗口直接進行保存

 

Excel Interior.ColorIndex色彩列表

 

對用的Interior.ColorIndex色彩列表。

在C# 對Excel編程中的使用:

 for (int i = 5; i < countRow + 1; i++)
            {
                string strKB = null;
                Microsoft.Office.Interop.Excel.Range kbrng = (Microsoft.Office.Interop.Excel.Range)sheSource.Cells[i, 1];
                if (kbrng.Value2 != null)
                    strKB = kbrng.Value2.ToString();
                if (strTotal.Contains(strKB))
                    kbrng.Interior.ColorIndex = 3;
            }

 

二、常見的Excel單元格格式設置內容

2.1 數字(Range.NumberFormatlocal 屬性)

常規:

Range.NumberFormatlocal = "G/通用格式"

數值:

Range.NumberFormatlocal = "0.000_" --保留小 數位數為3  (此處“_”表示:留下一個與下一個字符同等寬度的空格)

Range.NumberFormatlocal = "0" --不要小數

Range.NumberFormatlo cal = "#,##0.000" --保留小數位數為3,並使用千位分隔符
貨幣:

Range.NumberFormatlocal = "$#,##0.000"
百分比:

Range.NumberFormatlocal = "0.000%"
分數:

Range.NumberFormatlocal = "# ?/?"
科學計數:

Range.NumberFormatlocal = "0.00E+00"

文本

Range.NumberFormatlocal = "@"

特殊:

Range.NumberFormatlocal = "000000"---郵政編碼
Range.NumberFormatlocal = "[DBNum1]G/通用格式"---中文小寫數字
Range.NumberFormatlocal = "[DBNum2]G/通用格式"---中文大寫數字
Range.NumberFormatlocal = "[DBNum2][$RMB]G/通用格式"---人民幣大寫

2.2 對齊

水平對齊:Range.HorizontalAlignment = etHAlignCenter  ---居中
垂 直對齊:Range.VerticalAlignment = etVAlignCenter---居中
是否自動換行:Range.WrapText = True
是否縮小字體填充:Range.ShrinkToFit = True
是否合並單元格:Range.MergeCells = False
文字豎排:Range.Orientation = etVertical
文字傾斜度數:Range.Orientation = 45 -----傾斜45度
字體(Font對象)
字體名稱:Font.Name = "華文行楷"
字形: Font.FontStyle = "常規"
字號:Font.Size = "10"
下划線:Font.Strikethrough = True; Font.Underline = etUnderlineStyleDouble ---雙下划線
上標:Font.Superscript = True
下 標:Font.SubScript = True
刪除線:Font.OutlineFont = True

2.3 邊框(Borders對象)

Borders.Item(etEdgeTop):上邊框
Borders.Item(etEdgeLeft):左邊框
Borders.Item (etEdgeRight):右邊框
Borders.Item(etEdgeBottom):下邊框
Borders.Item(etDiagonalDown) :左上--右下邊框
Borders.Item(etDiagonalUp):左下--右上邊框
Border.LineStyle = etContinuous 線條樣式

 

 

 

 

 

參考博文

1. Herbert, C# Excel 行高,列寬,合並單元格,單元格邊框線,凍結

2. zhpingyun008, C#設置EXCEL單元格格式.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM