Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPOI apply font to entire row of cells

Tags:

c#

excel

npoi

I'm using NPOI to export my data to excel. The problem is I found it really hard for any kind of graphical changes.

This is the method I'm using now to apply bold font to my cells.

//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

Is there a ,,cleaner" way to do this ? Now i have to manually add font for individual cells. I've tried many ways to do this on the internet and nothing seems to be working. Do you have a tested way to apply style to specific column or row ?

OffTopic: If not can you provide me with some good open source libraries with decent documentation and support that allow excel export (learning new dll is a pain but... :) what can you do)?

like image 729
cah1r Avatar asked Nov 27 '25 04:11

cah1r


1 Answers

I'm doing something similar and modified my take on it closer for your use:

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;
        }

This iterates over however many headers you want starting at the second cell (cellCount = 1) second row (sheet.CreateRow(1)).

like image 140
mitchellJ Avatar answered Nov 28 '25 19:11

mitchellJ