欢迎使用C#或VB.NET建议。
我有以下代码用NPOI创建Excel文件。它工作正常。我需要将单元格样式应用于循环中的那些行。
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
如何将单元格样式应用于上面循环中的那些行?
答案 0 :(得分:2)
你需要在循环之外声明Row和Cell,如下所示:
Dim dataCell As HSSFCell
Dim dataRow As HSSFRow
然后在循环内部,您将单独的值和样式分配给单元格,如下所示:
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