Pregunta

Estoy intentando crear células dobles y de formato de número en Excel usando la biblioteca NPOI. Solía ??código como

Dim cell As HSSFCell = row.CreateCell(j)
cell.SetCellValue(Double.Parse(dr(col).ToString))

números en Excel están alineando bien, pero cuando compruebo el formato que se muestra en "General"

text alt

luego cambié de código para, como a continuación

 Dim cell As HSSFCell = row.CreateCell(j)
 cell.SetCellValue(Double.Parse(dr(col).ToString))
 Dim cellStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
 cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,#0.0")
 cell.CellStyle = cellStyle

A continuación, al abrir el archivo Mientras se está dando error y también tomar mucho tiempo para abrir. Pero el formato de Excel que muestra en "Número"

Mostrando error es como a continuación.

text alt

¿Cómo solucionar este problema?

¿Fue útil?

Solución

Tome un vistazo a este , estás creando un objeto CellStyle para cada celda? Si es así no lo hacen. Trate de crear sólo un par de estilos antes de crear sus células y luego aplicar estos estilos predefinidos para las células que se crean.

Otros consejos

Hare es una manera simple de crear formato doble en el Documento de Excel USO NPOI .

//make NUMERIC Format in Excel Document // Author: Akavrelishvili
  var eRow = sheet.CreateRow(rowIndex); //create new Row , rowIndex - it's integer, like : 1,2,3
  eRow.CreateCell(0).SetCellValue(row["ProvidName"].ToString()); //create cell and set string value

  double Amount = Convert.ToDouble(row["Amount"].ToString()); //convert string to double
  eRow.CreateCell(1).SetCellValue(Amount); // create cell and set double value.

Esta versión está trabajando, lo he utilizado una gran cantidad de proyectos.

Muy duro No es insertar DateTime formato en Excel, ningún buen ejemplo en Internet y creo que ayuda a las personas que lo hacen manera correcta. Te muestro ejemplo de código:

     //make Date Time Format in Excel Document // Author: Akavrelishvili

var eRow = sheet.CreateRow (rowIndex); // crear nueva fila // rowIndex - es número entero, como: 1,2,3

 ICellStyle cellDateStyle = workBook.CreateCellStyle(); //create custom style
 cellDateStyle.DataFormat = workBook.CreateDataFormat().GetFormat("dd/mm/yyyy"); //set day time Format

 eRow.CreateCell(3).SetCellValue(Convert.ToDateTime(row["Date"])); //set DateTime value to cell
                    eRow.GetCell(6).CellStyle = cellDateStyle; // Restyle cell using "cellDateStyle"


I hope it helps

Para solucionar los muchos diferentes estilos de celda declaran todos los estilos fuera de cualquier bucle puede que esté ejecutando.

Estoy presumeing que 'j' sería el empadronador así que voy a dejar lo que tenía en un formato corregido para usted.

Dim cellStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,#0.0")

For col = 0 To ColoumCounter
  For j = 0 To Counter
    Dim cell As HSSFCell = row.CreateCell(j)
    cell.SetCellValue(Double.Parse(dr(col).ToString))
    cell.CellStyle = cellStyle
  Next
Next

Esto debería funcionar un poco mejor, limitando el número de estilos "nuevas".

Crear un estilo de entonces, pero este estilo para la columna

 ICellStyle _TextCellStyle = wb1.CreateCellStyle();

 _TextCellStyle.DataFormat = wb1.CreateDataFormat().GetFormat("@");
 sheet.SetDefaultColumnStyle(2, _TextCellStyle);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top