NPOI 设置单元格样式“HSSFFont.BOLDWEIGHT_BOLD”不工作

发布于 11-09 06:54 字数 1256 浏览 5 评论 0原文

我正在使用 NPOI 从 Asp.Net 输出 excel。我想为我的单元格设置粗体和正常样式,但它适用于少数单元格,而不适用于剩余单元格。请查看以下示例:

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

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

    Dim font As HSSFFont = _hssfworkbook.CreateFont()
    font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
    cellStyle.SetFont(font)

    For i = 0 To 9 Step 1
        'I want to add cell style to these cells
        If i Mod 2 = 0 Then 
                Sheet1.CreateRow(i).CreateCell(1).SetCellValue(i)
                font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
                cellStyle.SetFont(font)
                Sheet1.GetRow(i).GetCell(1).CellStyle = cellStyle
        Else
                Sheet1.CreateRow(i).CreateCell(1).SetCellValue(i)
                font.Boldweight = HSSFFont.BOLDWEIGHT_NORMAL
                cellStyle.SetFont(font)
                Sheet1.GetRow(i).GetCell(1).CellStyle = cellStyle
        End If
   Next

实际上代码工作正常,但我不知道具体情况从哪里以及为什么它停止为剩余的几行工作。它不适用于所有单元格,并且从该特定单元格开始,粗体和正常属性停止在整个工作表(如sheet2和sheet3)上工作。

I'm using NPOI to output excel from Asp.Net. I want to set bold and normal style to my cell but it is working for few cell and not for remaining cell.Please have look on following example:

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

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

    Dim font As HSSFFont = _hssfworkbook.CreateFont()
    font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
    cellStyle.SetFont(font)

    For i = 0 To 9 Step 1
        'I want to add cell style to these cells
        If i Mod 2 = 0 Then 
                Sheet1.CreateRow(i).CreateCell(1).SetCellValue(i)
                font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
                cellStyle.SetFont(font)
                Sheet1.GetRow(i).GetCell(1).CellStyle = cellStyle
        Else
                Sheet1.CreateRow(i).CreateCell(1).SetCellValue(i)
                font.Boldweight = HSSFFont.BOLDWEIGHT_NORMAL
                cellStyle.SetFont(font)
                Sheet1.GetRow(i).GetCell(1).CellStyle = cellStyle
        End If
   Next

Actually code is working fine but i don't know the particular situation from where and why its stops working for remaining few rows. Its not working properly for all cells and from that particular cell the bold and normal property stops working on whole sheet like sheet2 and sheet3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮光沉寂2024-11-16 06:54:37

您必须使用多种样式,因为对样式的更改会影响工作表中引用该样式的所有位置。

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

Dim BoldFont As HSSFFont = hssfworkbook.CreateFont()
    BoldFont.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
Dim NormalFont As HSSFFont = hssfworkbook.CreateFont()
    NormalFont.Boldweight = HSSFFont.BOLDWEIGHT_NORMAL
Dim cellStyleBold As HSSFCellStyle = hssfworkbook.CreateCellStyle()
With cellStyleBold
    .setFont(BoldFont)
    .Alignment = HSSFCellStyle.ALIGN_CENTER
End With
Dim cellStyleNormal As HSSFCellStyle = hssfworkbook.CreateCellStyle()
With cellStyleNormal
    .setFont(NormalFont)
    .Alignment = HSSFCellStyle.ALIGN_CENTER
End With

For i - 0 To 9 Step 1
    If i Mod 2 = 0 Then 
        With Sheet1.CreateRow(i).CreateCell(1)
             .SetCellValue(i)
             .cellStyle = cellStyleBold
        End With
    Else
        With Sheet1.CreateRow(i).CreateCell(1)
            .SetCellValue(i)
            .cellStyle = cellStyleNormal
        End With
    End If
Next

You have to use multiple styles because a change to a style affects everywhere that style is referenced in the sheet.

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

Dim BoldFont As HSSFFont = hssfworkbook.CreateFont()
    BoldFont.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
Dim NormalFont As HSSFFont = hssfworkbook.CreateFont()
    NormalFont.Boldweight = HSSFFont.BOLDWEIGHT_NORMAL
Dim cellStyleBold As HSSFCellStyle = hssfworkbook.CreateCellStyle()
With cellStyleBold
    .setFont(BoldFont)
    .Alignment = HSSFCellStyle.ALIGN_CENTER
End With
Dim cellStyleNormal As HSSFCellStyle = hssfworkbook.CreateCellStyle()
With cellStyleNormal
    .setFont(NormalFont)
    .Alignment = HSSFCellStyle.ALIGN_CENTER
End With

For i - 0 To 9 Step 1
    If i Mod 2 = 0 Then 
        With Sheet1.CreateRow(i).CreateCell(1)
             .SetCellValue(i)
             .cellStyle = cellStyleBold
        End With
    Else
        With Sheet1.CreateRow(i).CreateCell(1)
            .SetCellValue(i)
            .cellStyle = cellStyleNormal
        End With
    End If
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文