NPOI邊框設(shè)置

設(shè)置文檔默認(rèn)為無邊框

ISheet sheet = book.CreateSheet(sheetName[i]);

sheet.DisplayGridlines = false;//設(shè)置默認(rèn)為無邊框

var head = sheet.CreateRow(0);

for (int a = 0; a < header.Count(); a++)

{

ICell cell = head.CreateCell(a);

XSSFCellStyle fCellStyle = (XSSFCellStyle)book.CreateCellStyle();

XSSFFont ffont = (XSSFFont)book.CreateFont();

ffont.FontHeight = 20 * 20;

ffont.FontHeightInPoints = (short)9.75;//字號(hào)

ffont.FontName = "Times New Roman";//字體

ffont.Color = HSSFColor.White.Index;//字色

fCellStyle.SetFont(ffont);

fCellStyle.FillPattern = FillPattern.SolidForeground;//添加背景色必須加這句

fCellStyle.FillForegroundColor = HSSFColor.Grey50Percent.Index;//設(shè)置背景填充色50%的灰色

fCellStyle.VerticalAlignment = VerticalAlignment.Center;//垂直對(duì)齊

fCellStyle.Alignment = HorizontalAlignment.Center;//水平對(duì)齊

fCellStyle.BorderBottom = BorderStyle.Thin;//下邊框?yàn)榧?xì)線邊框

fCellStyle.BorderLeft = BorderStyle.Thin;//左邊框

fCellStyle.BorderRight = BorderStyle.Thin;//上邊框

fCellStyle.BorderTop = BorderStyle.Thin;//右邊框

fCellStyle.BottomBorderColor = HSSFColor.Grey25Percent.Index;//下邊框?yàn)榧?xì)線邊框

fCellStyle.LeftBorderColor = HSSFColor.Grey25Percent.Index;//左邊框

fCellStyle.RightBorderColor = HSSFColor.Grey25Percent.Index;//上邊框

fCellStyle.TopBorderColor = HSSFColor.Grey25Percent.Index;//右邊框

cell.CellStyle = fCellStyle;

cell.SetCellValue(header[a]);

}

for (int j = 0; j < datas[i].Count(); j++)

{

var row = sheet.CreateRow(j + 1);

if (row == null)

{

row = sheet.CreateRow(j + 1);

}

row.CreateCells(datas[i][j], styleBody);//styleBody

}

for (int columnNum = 0; columnNum < header.Count; columnNum++)

{

int columnWidth = sheet.GetColumnWidth(columnNum) / 256;//獲取當(dāng)前列寬度

for (int rowNum = 0; rowNum < sheet.LastRowNum; rowNum++)//在這一列上循環(huán)行

{

IRow currentRow = sheet.GetRow(rowNum);

ICell currentCell = currentRow.GetCell(columnNum);

int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;//獲取當(dāng)前單元格的內(nèi)容寬度

if (columnWidth < length)

{

columnWidth = length;

}//若當(dāng)前單元格內(nèi)容寬度大于列寬,則調(diào)整列寬為當(dāng)前單元格寬度

}

sheet.SetColumnWidth(columnNum, columnWidth * 256);

}



//行擴(kuò)展方法填充數(shù)據(jù)設(shè)置并單元格樣式(數(shù)據(jù)類型不可為空,否則會(huì)報(bào)空指針)

public static void CreateCells(this IRow row, object data, ICellStyle style = null)

{

Type t = data.GetType();

int i = 0;

foreach (var Propertie in t.GetProperties())

{

var cell = row.CreateCell(i++);

if (style != null)

{

cell.CellStyle = style;

}

cell.SetCellValue(Convert.ChangeType(Propertie.GetValue(data), Propertie.PropertyType).ToString());

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 使用首先需要了解他的工作原理 1.POI結(jié)構(gòu)與常用類 (1)創(chuàng)建Workbook和Sheet (2)創(chuàng)建單元格 (...
    長城ol閱讀 8,748評(píng)論 2 25
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,929評(píng)論 0 33
  • Apache POI 是用Java編寫的免費(fèi)開源的跨平臺(tái)的 Java API,Apache POI提供API給Ja...
    玩味Orz閱讀 2,760評(píng)論 0 0
  • 姓名:榮珊 20171130周檢視 今天我發(fā)誓:從此做美樂愛覺的天使,帶給人快樂幸福,給人力量,給人希望,給人夢...
    榮珊聚焦成長閱讀 222評(píng)論 0 1
  • 法國作家圣??诵跖謇镌谕挕缎⊥踝印分袑懙?,主人公小王子先后到達(dá)六顆星球漫游,最后還來到了地球。后來,小王子因?yàn)樗?..
    左手夢圓閱讀 384評(píng)論 8 6

友情鏈接更多精彩內(nèi)容