Domanda

Il suggerimento C# o VB.NET è il benvenuto.

Ho il seguente codice per creare un file Excel con NPOI. Funziona bene. Devo applicare lo stile cellulare a quelle file nei loop.

Dim hssfworkbook As New HSSFWorkbook()

    Dim sheetOne As HSSFSheet = hssfworkbook.CreateSheet("Sheet1")
    hssfworkbook.CreateSheet("Sheet2")
    hssfworkbook.CreateSheet("Sheet3")
    hssfworkbook.CreateSheet("Sheet4")

        Dim cellStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
    cellStyle.Alignment = HSSFCellStyle.ALIGN_CENTER

      For i = 0 To 9 Step 1
        'I want to add cell style to these cells
        sheetOne.CreateRow(i).CreateCell(1).SetCellValue(i)
        sheetOne.CreateRow(i).CreateCell(2).SetCellValue(i)
  Next

Come posso applicare lo stile cellulare a quelle righe nel loop sopra?

È stato utile?

Soluzione

Devi dichiarare riga e cella al di fuori del loop sth in questo modo:

Dim dataCell As HSSFCell
Dim dataRow As HSSFRow

Quindi all'interno del ciclo, assegni valore e stile a cell separatamente in questo modo:

    dataRow = sheetOne.CreateRow(i)
    dataCell = dataRow.CreateCell(1)
    dataCell.SetCellValue(i)
    dataCell.CellStyle = cellStyle

    dataRow = sheetOne.CreateRow(i)
    dataCell = dataRow.CreateCell(2)
    dataCell.SetCellValue(i)
    dataCell.CellStyle = cellStyle
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top