我正在尝试使用 c# 库 NPOI 编写一个 .xls 文件,我已经能够创建该文件,但是当我尝试更改工作表中某些单元格的背景颜色时,我不知道为什么所有单元格表改变颜色它让我发疯,你能帮帮我吗。
这是我正在使用的代码:
// Creation of XLS
// I create a new excel work
HSSFWorkbook workbook = new HSSFWorkbook();
int rowNumber = 0;
//I add to the excel work a sheet of work
ISheet sheet = workbook.CreateSheet("Sheet 1");
// I create the Header of the sheet
var 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 record
rowNumber++;
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 XLS
MemoryStream 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 而不是我得到该颜色的工作表的所有单元格。
为什么?!?
慕沐林林
拉莫斯之舞
相关分类