NPOI 设置单元格样式
欢迎使用 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
如何将单元格样式应用于上面循环中的这些行?
C# or VB.NET suggestion are welcome.
I have the following code to create Excel file with NPOI. It's working fine. I need to apply the cell style to those rows in the 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
How can I apply cell style to those rows in the loop above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在循环之外声明 Row 和 Cell,如下所示:
然后在循环内,分别为单元格分配值和样式,如下所示:
You need to declare Row and Cell outside of the loop sth like this:
Then inside the loop, you assign value and style to cell separately like this: