Search code examples
c#npoi

NPOI doesn't apply format to .xls


C#, NPOI

Good evening, i'm trying to fill in an empty template with values(numbers, but in string variable), that needs to be in cells.

So, when it came to formatting, i've stuck with styling. That means, when i wrote the value inside(in pre-formatted cell) i get just the string value, that was in array.

But, when i open file, click "edit cell", then apply without any changes, then cell is formatted, with the format, which was in a template

I tried to apply previous style in cell(basically just copy style in var, before setting a value, then re-apply style), but, it didnt help.

Here is the video of my doings

var cr = new CellReference(cellAndValue[i, 0]);
var row = sheet?.GetRow(cr.Row);
var cell = row?.GetCell(cr.Col);
var prevtype = cell.CellType;
var prevstyle = cell.CellStyle;
var dataformat = cell.CellStyle.DataFormat;
CellType type = cell.CellType;
cell.SetCellValue(cellAndValue[i, 0]);

How to resolve that?)


Solution

  • It was required to convert incoming data to int, before inserting it XD

    int n;
    if (Int32.TryParse(cellAndValue[i, 1], out n))
    {
        cell.SetCellValue(n);
    }
    else
    {
        cell.SetCellValue(cellAndValue[i, 1]);
    }