Search code examples
npoi

How to set a number string into a real string formation?


I'm now using NPOI to cope with Excel export, and here's my codes (part in .NET):

 int rowIndex = 1;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow dataRow = sheet.CreateRow(rowIndex);

                for (int j = 0; j < cellCount; j++)
                {
                    cell = dataRow.CreateCell(j,CellType.String);
                    cell.SetCellValue(new HSSFRichTextString(dt.Rows[i][j].ToString()));
                }
                rowIndex++;
            }

What makes me feel surprised is there's a list whose number string is "20150525", and it will be analyzed as "2015……E+10" formation (scientific number formation). However I wanna keep it as a string value. So How?

Thanks!


Solution

  • In fact we have to set a CellStyle, snippet of sample codes is below:

    IRow row = book[0].CreateRow(rowIndex + 1); 
     ICell rowCell = null;
     rowCell = row.CreateCell(colIndex);
     rowCell.SetCellValue(realCellValue);
     ICellStyle cellStyle = book.CreateCellStyle();
     cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
     rowCell.CellStyle = cellStyle;