VB.Net:Excel 个单元格周围的所有边框
VB.Net: All Borders Around Excel Cells
我试图在给定范围内的每个单元格周围放置边框。我一直无法通过研究找到一种方法来做到这一点。我唯一能够实现创建边框的是以下代码:
protoWorksheet.Range("A1:K1").BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
我想要的视觉效果:
此代码将在给定的连续范围内的每个单元格周围放置一个边框,在本例中,对于 Sheet1,范围是 C8:D8。
Option Strict On
Option Infer Off
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Excel
Public Module RangeBorderSample
Public Sub OpenExcelSimple()
Dim proceed As Boolean = False
Dim xlApp As Application = Nothing
Dim xlWorkBooks As Workbooks = Nothing
Dim xlWorkBook As Workbook = Nothing
Dim xlWorkSheet As Worksheet = Nothing
Dim xlWorkSheets As Sheets = Nothing
Dim xlCells As Range = Nothing
xlApp = New Application With {.DisplayAlerts = False, .Visible = True}
xlWorkBooks = xlApp.Workbooks
xlWorkBook = xlWorkBooks.Open(Path.Combine(
AppDomain.CurrentDomain.BaseDirectory, "Excel1.xlsx"))
xlWorkSheets = xlWorkBook.Sheets
For index As Integer = 1 To xlWorkSheets.Count
xlWorkSheet = CType(xlWorkSheets(index), Worksheet)
If xlWorkSheet.Name = "Sheet1" Then
proceed = True
Exit For
End If
Marshal.FinalReleaseComObject(xlWorkSheet)
xlWorkSheet = Nothing
Next
If proceed Then
xlCells = xlWorkSheet.Range("C6:D8")
PlaceBordersOnEachCell(xlCells)
End If
End Sub
Private Sub PlaceBordersOnEachCell(pRange As Range)
pRange.Cells.Borders.LineStyle = XlLineStyle.xlContinuous
End Sub
End Module
我试图在给定范围内的每个单元格周围放置边框。我一直无法通过研究找到一种方法来做到这一点。我唯一能够实现创建边框的是以下代码:
protoWorksheet.Range("A1:K1").BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
我想要的视觉效果:
此代码将在给定的连续范围内的每个单元格周围放置一个边框,在本例中,对于 Sheet1,范围是 C8:D8。
Option Strict On
Option Infer Off
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Excel
Public Module RangeBorderSample
Public Sub OpenExcelSimple()
Dim proceed As Boolean = False
Dim xlApp As Application = Nothing
Dim xlWorkBooks As Workbooks = Nothing
Dim xlWorkBook As Workbook = Nothing
Dim xlWorkSheet As Worksheet = Nothing
Dim xlWorkSheets As Sheets = Nothing
Dim xlCells As Range = Nothing
xlApp = New Application With {.DisplayAlerts = False, .Visible = True}
xlWorkBooks = xlApp.Workbooks
xlWorkBook = xlWorkBooks.Open(Path.Combine(
AppDomain.CurrentDomain.BaseDirectory, "Excel1.xlsx"))
xlWorkSheets = xlWorkBook.Sheets
For index As Integer = 1 To xlWorkSheets.Count
xlWorkSheet = CType(xlWorkSheets(index), Worksheet)
If xlWorkSheet.Name = "Sheet1" Then
proceed = True
Exit For
End If
Marshal.FinalReleaseComObject(xlWorkSheet)
xlWorkSheet = Nothing
Next
If proceed Then
xlCells = xlWorkSheet.Range("C6:D8")
PlaceBordersOnEachCell(xlCells)
End If
End Sub
Private Sub PlaceBordersOnEachCell(pRange As Range)
pRange.Cells.Borders.LineStyle = XlLineStyle.xlContinuous
End Sub
End Module