C# Excel設置單元格邊框 之 NPOI


很多表格中都要使用邊框,本節將為你重點講解NPOI中邊框的設置和使用。

邊框和其他單元格設置一樣也是調用ICellStyle接口,ICellStyle有2種和邊框相關的屬性,分別是:

邊框相關屬性 說明 范例
Border+方向 邊框類型 BorderTop, BorderBottom,BorderLeft, BorderRight
方向+BorderColor 邊框顏色 TopBorderColor,BottomBorderColor, LeftBorderColor, RightBorderColor

其中邊框類型分為以下幾種:

邊框范例圖 對應的靜態值
image CellBorderType.DOTTED
image CellBorderType.HAIR
image CellBorderType.DASH_DOT_DOT
image CellBorderType.DASH_DOT
image CellBorderType.DASHED
image CellBorderType.THIN
image CellBorderType.MEDIUM_DASH_DOT_DOT
image CellBorderType.SLANTED_DASH_DOT
image CellBorderType.MEDIUM_DASH_DOT
image CellBorderType.MEDIUM_DASHED
image CellBorderType.MEDIUM
image CellBorderType.THICK
image CellBorderType.DOUBLE

 

至於顏色那就很多了,全部在HSSFColor下面,如HSSFColor.GREEN, HSSFColor.RED,都是靜態實例,可以直接引用。

下面我們假設我們要把一個單元格的四周邊框都設置上,可以用下面的代碼:

    //創建單元格樣式
    ICellStyle cellStyle = workbook.CreateCellStyle();
   //設置為文本格式,也可以為 text,即 dataFormat.GetFormat("text");
    cellStyle.DataFormat = dataFormat.GetFormat("@");
    cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;    //下邊框線
    cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;      //左邊框線
    cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;     //右邊框線
    cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;       //上邊框線

 


免責聲明!

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



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