C# NPOI将字体应用于整行单元格

C# NPOI将字体应用于整行单元格,c#,excel,npoi,C#,Excel,Npoi,我正在使用NPOI将数据导出到excel。问题是我发现任何形式的图形更改都非常困难 这是我现在用来将粗体字体应用于单元格的方法 //Create new Excel workbook var workbook = new HSSFWorkbook(); //Create new Excel sheet var sheet = workbook.CreateSheet(); //Create a header row

我正在使用NPOI将数据导出到excel。问题是我发现任何形式的图形更改都非常困难

这是我现在用来将粗体字体应用于单元格的方法

//Create new Excel workbook
        var workbook = new HSSFWorkbook();

        //Create new Excel sheet
        var sheet = workbook.CreateSheet();

        //Create a header row
        var headerRow = sheet.CreateRow(0);

        var boldFont = workbook.CreateFont();
        boldFont.FontHeightInPoints = 11;
        boldFont.FontName = "Calibri";
        boldFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;

        int cellCounter = 0;

        //day
        var cell = headerRow.CreateCell(cellCounter++);
        cell.SetCellValue("Day");
        cell.CellStyle = workbook.CreateCellStyle();
        cell.CellStyle.SetFont(boldFont);
        //month
        cell = headerRow.CreateCell(cellCounter++);
        cell.SetCellValue("Month");
        cell.CellStyle = workbook.CreateCellStyle();
        cell.CellStyle.SetFont(boldFont);
        //year
        cell = headerRow.CreateCell(cellCounter++);
        cell.SetCellValue("Year");
        cell.CellStyle = workbook.CreateCellStyle();
        cell.CellStyle.SetFont(boldFont);
        //machine name
        cell = headerRow.CreateCell(cellCounter++);
        cell.SetCellValue("Machine unique name");
        cell.CellStyle = workbook.CreateCellStyle();
        cell.CellStyle.SetFont(boldFont); //and so on
有没有一种、更干净的方法可以做到这一点?现在我必须手动为单个单元格添加字体。我在internet上尝试了许多方法,但似乎都没有效果。您有没有一种经过测试的方法可以将样式应用于特定的列或行


OffTopic:如果没有,你能为我提供一些好的开源库,并提供适当的文档和支持,以允许excel导出(学习新的dll是一件痛苦的事,但是…:)你能做什么?

我正在做类似的事情,并修改了我对它的理解,以供您使用:

private string[] columnHeaders =
{
    "Day",
    "Month",
    "Year",
    "Machine Unique Name"
}

    private void buildSheet(HSSFWorkbook wb, DataTable data, string sheetName)
    {
        var cHelp = wb.GetCreationHelper();
        var sheet = wb.CreateSheet(sheetName);

        HSSFFont hFont = (HSSFFont)wb.CreateFont();

        hFont.FontHeightInPoints = 11;
        hFont.FontName = "Calibri";
        hFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;

        HSSFCellStyle hStyle = (HSSFCellStyle)wb.CreateCellStyle();
        hStyle.SetFont(hFont);

        IRow headerRow = sheet.CreateRow(1);

        int cellCount = 1;
        foreach (string str in columnHeaders)
        {
            HSSFCell cell = (HSSFCell)headerRow.CreateCell(cellCount);
            cell.SetCellValue(cHelp.CreateRichTextString((str)));
            cell.CellStyle = hStyle;

            cellCount += 1;
        }
这将迭代从第二个单元格(cellCount=1)第二行(sheet.CreateRow(1))开始的任意数量的标题