Pergunta

A sugestão C# ou vb.net é bem -vinda.

Eu tenho o seguinte código para criar o arquivo do Excel com o nPOI. Está funcionando bem. Preciso aplicar o estilo da célula a essas linhas nos loops.

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

Como posso aplicar o estilo de célula a essas linhas no loop acima?

Foi útil?

Solução

Você precisa declarar linha e célula fora do loop sth como esta:

Dim dataCell As HSSFCell
Dim dataRow As HSSFRow

Em seguida, dentro do loop, você atribui valor e estilo à célula separadamente assim:

    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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top