亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C# 使用 NPOI 庫更改單元格的背景顏色

C# 使用 NPOI 庫更改單元格的背景顏色

C#
慕絲7291255 2021-10-24 14:05:43
我正在嘗試使用 c# 庫 NPOI 編寫一個 .xls 文件,我已經能夠創建該文件,但是當我嘗試更改工作表中某些單元格的背景顏色時,我不知道為什么所有單元格表改變顏色它讓我發瘋,你能幫幫我嗎。這是我正在使用的代碼:// Creation of XLS// I create a new excel workHSSFWorkbook workbook = new HSSFWorkbook();int rowNumber = 0;//I add to the excel work a sheet of workISheet sheet = workbook.CreateSheet("Sheet 1");// I create the Header of the sheetvar headerRow = sheet.CreateRow(0);headerRow.CreateCell(0).SetCellValue("Famiglia");headerRow.CreateCell(1).SetCellValue("Quantità Tagliata Fogli");headerRow.CreateCell(2).SetCellValue("Lotto Medio di Produzione Fogli");headerRow.CreateCell(3).SetCellValue("Quantità Scarto Medio(pezzi)");headerRow.CreateCell(4).SetCellValue("Valore Scarti Euro");headerRow.CreateCell(5).SetCellValue("% Scarto");headerRow.CreateCell(6).SetCellValue(" Lead Time Medio Produttivo");for (int c = 0; c < headerRow.Cells.Count; c++){                                   headerRow.Cells[0].CellStyle.FillForegroundColor= IndexedColors.LightBlue.Index;    headerRow.Cells[0].CellStyle.FillPattern = FillPattern.SolidForeground;}// Now what I have to do is to write the data in to the cells creating so a new recordrowNumber++;IRow row = sheet.CreateRow(rowNumber);                    row.CreateCell(0).SetCellValue(f.family);row.CreateCell(1).SetCellValue(f.QuantitàTagliataFogli);row.CreateCell(2).SetCellValue(f.LottoMedioProduzioneFogli);row.CreateCell(3).SetCellValue(f.QuantitàScartoMedioInPezzi);row.CreateCell(4).SetCellValue(f.ValoreScartoInEuro);row.CreateCell(5).SetCellValue(f.ScartoMedio);row.CreateCell(6).SetCellValue(f.LeadTimeMedioProduttivo);// Now I have to try to write the file XLSMemoryStream output = new MemoryStream();workbook.Write(output);SaveFileDialog SaveFileDialog = new SaveFileDialog();SaveFileDialog.Title = "Save As...";SaveFileDialog.Filter = "xls File (*.xls)|*.xls";SaveFileDialog.InitialDirectory = @"C:\";我本來希望只有第一行的單元格帶有 backColor lightblue 而不是我得到該顏色的工作表的所有單元格。為什么?!?
查看完整描述

2 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

請嘗試如下方法。我已經分離了字體、樣式,最后使用了你的單元格樣式的 for 循環分配也修復了 for 循環內部的錯誤,你每次只將樣式分配給 Cells[0]。


HSSFFont headerFont = (HSSFFont)workbook.CreateFont();

headerFont.FontHeightInPoints = (short)12;

headerFont.FontName = "Arial";

headerFont.Color = IndexedColors.White.Index;

headerFont.IsBold = true;

headerFont.IsItalic = false;

headerFont.Boldweight = 700;


HSSFCellStyle headerStyle = (HSSFCellStyle)workbook.CreateCellStyle();

headerStyle.WrapText = true;

headerStyle.FillForegroundColor = IndexedColors.LightBlue.Index;

headerStyle.FillPattern = FillPattern.SolidForeground;

headerStyle.Alignment = HorizontalAlignment.Center;

headerStyle.VerticalAlignment = VerticalAlignment.Center;

headerStyle.BorderBottom = BorderStyle.Thin;

headerStyle.BorderTop = BorderStyle.Thin;

headerStyle.BorderLeft = BorderStyle.Thin;

headerStyle.BorderRight = BorderStyle.Thin;

headerStyle.SetFont(headerFont);


for (int c = 0; c < headerRow.Cells.Count; c++)

{                               

    headerRow.Cells[c].CellStyle = headerStyle;

}


查看完整回答
反對 回復 2021-10-24
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

替換這些行:


for (int c = 0; c < headerRow.Cells.Count; c++)

{

    headerRow.Cells[0].CellStyle.FillForegroundColor= IndexedColors.LightBlue.Index;

    headerRow.Cells[0].CellStyle.FillPattern = FillPattern.SolidForeground;

}

和:


HSSFCellStyle cellStyleBlue = (HSSFCellStyle)workbook.CreateCellStyle();

cellStyleBlue.FillForegroundColor = IndexedColors.LightBlue.Index;

cellStyleBlue.FillPattern = FillPattern.SolidForeground;


for (int c = 0; c < headerRow.Cells.Count; c++)

{

    headerRow.Cells[c].CellStyle = cellStyleBlue;

}

然后,只有第一行的單元格才會應用單元格樣式。


查看完整回答
反對 回復 2021-10-24
  • 2 回答
  • 0 關注
  • 1290 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號