C# 使用C设置HSSF工作表中特定单元格的背景色#

C# 使用C设置HSSF工作表中特定单元格的背景色#,c#,excel,hssfworkbook,C#,Excel,Hssfworkbook,我正在尝试使用HSSFWorkbook生成excel工作表。我想设置单元格的背景色。我正在使用HSSFWorkbook,但不幸的是无法获得背景色 我需要将一些单元格设置为相同的颜色,而将一些其他单元格设置为多种颜色 到目前为止,我的代码是: HSSFWorkbook工作簿=新的HSSFWorkbook(); MemoryStream MemoryStream=新的MemoryStream(); 数据集repds=exceldetils.ToDataSet(“批处理”); HSSFSheet sh

我正在尝试使用
HSSFWorkbook
生成excel工作表。我想设置单元格的背景色。我正在使用
HSSFWorkbook
,但不幸的是无法获得背景色

我需要将一些单元格设置为相同的颜色,而将一些其他单元格设置为多种颜色

到目前为止,我的代码是:
HSSFWorkbook工作簿=新的HSSFWorkbook();
MemoryStream MemoryStream=新的MemoryStream();
数据集repds=exceldetils.ToDataSet(“批处理”);
HSSFSheet sheets=(NPOI.HSSF.UserModel.HSSFSheet)workbook.CreateSheet(“批处理”);
HSSFRow headerRow=(NPOI.HSSF.UserModel.HSSFRow)sheets.CreateRow(0);
//
List columnnames=新列表();
foreach(repds.Tables[0].Columns中的DataColumn列)
{
//column.ColumnName=hssfont.FONT\u ARIAL;
//columnnames.Add(column.ColumnName);
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
}
var cHelp=workbook.GetCreationHelper();
hssfont hFont=(hssfont)工作簿。CreateFont();
hFont.Boldweight=(短)fontboldwweight.Bold;
hFont.Color=HSSFColor.Black.Index;
hFont.FontHeightInPoints=10;
HSSFCellStyle hs=工作簿.CreateCellStyle();
HSSFCellStyle hStyle=(HSSFCellStyle)工作簿。CreateCellStyle();
hStyle.SetFont(hFont);
hStyle.BorderBottom=BorderStyle.Medium;
hStyle.FillBackgroundColor=HSSFColor.Black.Index;
hStyle.FillPattern=FillPattern.SolidForeground;
int-cellCount=0;
foreach(列名称中的字符串str)
{
HSSFCell cell=(HSSFCell)headerRow.CreateCell(cellCount);
cell.SetCellValue(cHelp.CreateRichTextString((str));
cell.CellStyle=hStyle;
细胞计数+=1;
}
int rowIndex=1;
foreach(repds.Tables[0].Rows中的数据行第1行)
{
HSSFRow数据行=(NPOI.HSSF.UserModel.HSSFRow)sheets.CreateRow(行索引);
foreach(repds.Tables[0].Columns中的DataColumn列)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row1[column].ToString());
}
rowIndex++;
}
工作簿。编写(memoryStream);
//memoryStream.Flush();
//memoryStream.GetBuffer();
HSSFWorkbook workbook = new HSSFWorkbook();
MemoryStream memoryStream = new MemoryStream();
DataSet repds = exceldetils.ToDataSet("Batch");
HSSFSheet sheets = (NPOI.HSSF.UserModel.HSSFSheet)workbook.CreateSheet("Batch");
HSSFRow headerRow = (NPOI.HSSF.UserModel.HSSFRow)sheets.CreateRow(0);
//
List<string> columnnames = new List<string>();
foreach (DataColumn column in repds.Tables[0].Columns)
{
  //column.ColumnName = HSSFFont.FONT_ARIAL;
  // columnnames.Add(column.ColumnName);
  headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
}
var cHelp = workbook.GetCreationHelper();
HSSFFont hFont = (HSSFFont)workbook.CreateFont();

hFont.Boldweight = (short)FontBoldWeight.Bold;
hFont.Color = HSSFColor.Black.Index;
hFont.FontHeightInPoints = 10   ;
HSSFCellStyle hs = workbook.CreateCellStyle();
HSSFCellStyle hStyle =(HSSFCellStyle) workbook.CreateCellStyle(); 

hStyle.SetFont(hFont);
hStyle.BorderBottom = BorderStyle.Medium;
hStyle.FillBackgroundColor = HSSFColor.Black.Index;

hStyle.FillPattern = FillPattern.SolidForeground;
int cellCount = 0;
foreach (string str in columnnames)
{
  HSSFCell cell = (HSSFCell)headerRow.CreateCell(cellCount);
  cell.SetCellValue(cHelp.CreateRichTextString((str)));
  cell.CellStyle = hStyle;
  cellCount += 1;
}

int rowIndex = 1;
foreach (DataRow row1 in repds.Tables[0].Rows)
{
  HSSFRow dataRow = (NPOI.HSSF.UserModel.HSSFRow)sheets.CreateRow(rowIndex);
  foreach (DataColumn column in repds.Tables[0].Columns)
  {
    dataRow.CreateCell(column.Ordinal).SetCellValue(row1[column].ToString());
  }
  rowIndex++;
}
workbook.Write(memoryStream);
//memoryStream.Flush();
//memoryStream.GetBuffer();