0

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!

xqMogvKW
  • 628
  • 7
  • 17
  • Can't confirm such a behaviour. If cell type is set to `CellType.String` - then content being recognized by Excel as string. At least - on my side. Try to update NPOI to the latest version. – Andrey Korneyev Jun 01 '15 at 15:46
  • OK, thanks. I've connected to the NPOI's author and they said they would fix it. Maybe a bug? Haha…… – xqMogvKW Jun 02 '15 at 02:59
  • @AndyKorneyev: In fact I've used another way of "customized defination". – xqMogvKW Jun 09 '15 at 05:34

1 Answers1

0

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;
xqMogvKW
  • 628
  • 7
  • 17