Domanda

Sto cercando di creare celle doppie e formato numerico in Excel utilizzando biblioteca NPOI. Ho usato il codice come

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

i numeri in Excel si stanno allineando a destra, ma quando posso controllare formato che sta mostrando in "Generale"

alt text

poi ho cambiato il mio codice per come qui di seguito

 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

Poi, mentre l'apertura del file si sta dando l'errore e anche prendendo così tanto tempo per aprire. Ma formato Excel che mostra in "Numero"

errore di mostra è come qui di seguito.

alt text

Come risolvere questo problema?

È stato utile?

Soluzione

Date un'occhiata al questo , stai creando un oggetto CellStyle per ogni cella? Se è così non lo fanno. Prova a creare solo un paio di stili prima di creare le cellule e quindi applicare questi stili predefiniti per le cellule create.

Altri suggerimenti

Hare è un modo semplice per creare doppio formato nel documento Excel UTILIZZO 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.

Questa versione funziona, ho usato un sacco di progetti.

Molto Difficile C'è da inserire DateTime formato Excel, nessun buon esempio in Internet e penso che aiuta le persone a farlo modo giusto. Vi mostro esempio di codice:

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

var = eRow sheet.CreateRow (rowIndex); // crea nuova Row // rowIndex - è intero, come: 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

Per fissare la troppi stili differenti delle cellule dichiarano tutti gli stili di fuori di ogni ciclo si può essere in esecuzione.

Sono presumeing si 'j' sarebbe l'enumeratore quindi mi cadere quello che aveva in un formato corretto per voi.

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

Questo dovrebbe funzionare un po 'meglio, limitando il numero di stili di "nuovo".

Creare uno stile poi, ma questo stile per la colonna

 ICellStyle _TextCellStyle = wb1.CreateCellStyle();

 _TextCellStyle.DataFormat = wb1.CreateDataFormat().GetFormat("@");
 sheet.SetDefaultColumnStyle(2, _TextCellStyle);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top