2
when I file in Excel using the library NPOI (https://github.com/tonyqus/npoi/tree/master/examples/xssf) I can change the style of the cell to moneyFormat with the following code, however, when the user opens Excel the cells are with the formatting of "GENERAL" and in order for it to be able to, for example, add up all transactions by selecting cells, it needs to tighten F2 (edit cell) and press Enter, only then the cell changes format. Does anyone know how to solve this? Follow the code:
HSSFFont boldFont = (HSSFFont)workBook.CreateFont();
HSSFCellStyle cellStyle = (HSSFCellStyle)workBook.CreateCellStyle();
ICellStyle moneyFormat = workBook.CreateCellStyle();
ICellStyle percentFormat = workBook.CreateCellStyle();
ICellStyle qtdFormat = workBook.CreateCellStyle();
//Define máscaras
IDataFormat format = workBook.CreateDataFormat();
moneyFormat.DataFormat = format.GetFormat("R$#,##0.00");
percentFormat.DataFormat = format.GetFormat("0.00");
qtdFormat.DataFormat = format.GetFormat("0");
//Linhas
foreach (var linha in mvtFinanceiroList)
{
row = (HSSFRow)sheet.CreateRow(index);
row.CreateCell(0).SetCellValue(linha.tb_ban_banco.ban_c_descricao); //Cria 1ª coluna da 1ª linha
int lastRow1 = row.LastCellNum;
row.CreateCell(lastRow1).SetCellValue(linha.mvt_c_descricao); //Cria 2ª coluna da 1ª linha
int lastRow2 = row.LastCellNum;
row.CreateCell(lastRow2).SetCellValue(string.Format("{0:C}", linha.mvt_n_valor)); //Cria 3ª coluna da 1ª linha
row.GetCell(lastRow2).CellStyle = moneyFormat; //Aplica máscara de Grana
int lastRow3 = row.LastCellNum;
row.CreateCell(lastRow3).SetCellValue(linha.mvt_d_dataMovimento.ToString("dd/MM/yyyy"));
row.GetCell(lastRow3).CellStyle = cellStyleCenter; //Posiciona no centro
index++;
}
would not be the
SetDataFormat()
in theCellStyle
?– Leandro Angelo
Leandro Angelo, it didn’t work. I tried several variations of Setdataformat(), but without effect.
– danteMesquita
The closest I came was seeing this tutorial, but it also doesn’t work properly for real value. link
– danteMesquita