使用vb.net的excel报告中的某些列中缺少顶部边框

时间:2017-05-03 06:55:24

标签: excel vb.net

我正在使用vb.net生成excel报告。报告中缺少一些边界。缺少E32 F32,G32 H32的顶部边框,缺少H32的右边。下面是我的代码。甚至缺少B32的右上角。enter image description here

      With oSheet.Range("E" & count)
            .Merge()
            .HorizontalAlignment = XlHAlign.xlHAlignCenter
            .VerticalAlignment = XlHAlign.xlHAlignCenter
            .Borders.Weight = XlBorderWeight.xlThin
            .Value = "For the month"
            .Font.Bold = True
            .ColumnWidth = 20

        End With
        oSheet.Range("E36:F36").Merge()
        With oSheet.Range("E" & count + 1)
            .HorizontalAlignment = XlHAlign.xlHAlignCenter
            .VerticalAlignment = XlHAlign.xlHAlignCenter
            .Borders.Weight = XlBorderWeight.xlThin
            .Font.Bold = True
            .ColumnWidth = 20

        End With

        With oSheet.Range("G" & count)
            .Merge()
            .HorizontalAlignment = XlHAlign.xlHAlignCenter
            .VerticalAlignment = XlHAlign.xlHAlignCenter
            .Borders.Weight = XlBorderWeight.xlThin
            .Value = "Since Inspection"
            .Font.Bold = True
            .ColumnWidth = 20

        End With
        oSheet.Range("G36:H36").Merge()
        count += 1
        With oSheet.Range("E" & count)
            .Merge()
            .HorizontalAlignment = XlHAlign.xlHAlignCenter
            .VerticalAlignment = XlHAlign.xlHAlignCenter
            .Borders.Weight = XlBorderWeight.xlThin
            .Value = "No. of Loans"
            .Font.Bold = True
            .ColumnWidth = 20
            .WrapText = True
        End With
        With oSheet.Range("F" & count)
            .Merge()
            .HorizontalAlignment = XlHAlign.xlHAlignCenter
            .VerticalAlignment = XlHAlign.xlHAlignCenter
            .Borders.Weight = XlBorderWeight.xlThin
            .Value = "Amount"
            .Font.Bold = True
            .ColumnWidth = 20
            .WrapText = True
        End With
        With oSheet.Range("G" & count)
            .Merge()
            .HorizontalAlignment = XlHAlign.xlHAlignCenter
            .VerticalAlignment = XlHAlign.xlHAlignCenter
            .Borders.Weight = XlBorderWeight.xlThin
            .Value = "No. of Loans"
            .Font.Bold = True
            .ColumnWidth = 20
            .WrapText = True
        End With
        With oSheet.Range("H" & count)
            .Merge()
            .HorizontalAlignment = XlHAlign.xlHAlignCenter
            .VerticalAlignment = XlHAlign.xlHAlignCenter
            .Borders.Weight = XlBorderWeight.xlThin
            .Value = "Amount"
            .Font.Bold = True
            .ColumnWidth = 20
            .WrapText = True
        End With
        count += 1
        oExcel.Visible = True

1 个答案:

答案 0 :(得分:0)

您是否使用Microsoft.Office.Interop.Excel执行此操作?

如果是这样,我通常使用如下代码创建一个有边界的单元格;

Dim sPathFileExcel As String = "C:\test.xlsx"
Dim oAppExcel As New Microsoft.Office.Interop.Excel.Application
Dim WB As Microsoft.Office.Interop.Excel.Workbook
WB = oAppExcel.Workbooks.Open(sPathFileExcel)
Dim WS As Microsoft.Office.Interop.Excel.Worksheet = WB.Sheets(1)

Dim styBorder As Microsoft.Office.Interop.Excel.Style = WS.Application.ActiveWorkbook.Styles.Add("styBorder")

            With styBorder

                .Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
                .Borders.Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalUp).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone
                .Borders.Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalDown).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone
                .Borders.Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone
                .Borders.Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone

            End With

Dim rngActual As Microsoft.Office.Interop.Excel.Range = WS.Cells(2, 3)

rngActual.Style = "styBorder"