我是 Visual Basic 的新手,我正在尝试构建一个对 Excel 文件执行某些操作的简单应用程序。
我想编辑工作表的单元格边框属性,我需要编辑一些指定单元格的单独边框的权重和颜色(例如只有底部边框或顶部边框)。
我在网上找到了一些有趣的资源: http://www.functionx.com/vbaexcel/cells/Lesson4.htm 范围内每个单元格周围的边框 http://social.msdn.microsoft.com/Forums/en-US/ csharpgeneral/thread/93bb7ff7-0aed-4ce1-adca-aabde5fc3c2c
无论如何,我不可能遵循建议的示例。这是我的代码的摘录:
Public Class mytest
Dim oExcel As Object 'Oggetto per la gestione del file Excel
Dim oBook As Object 'Oggetto per la gestione del file Excel
Dim page As Integer = 1 'Indice per la gestione dei fogli Excel
....
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'Creazione nuovo workbook in Excel
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
'Add data to cells of the first worksheet in the new workbook
'Apertura file in lettura
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("input.csv")
MyReader.TextFieldType = FileIO.FieldType.Delimited
'Imposto il carattere di separazione tra i campi
MyReader.SetDelimiters(";")
'Creo stringa lettura righe
Dim currentRow As String()
'Leggo 1 volta per saltare
currentRow = MyReader.ReadFields()
'Fino alla fine del file
While Not MyReader.EndOfData
'Mostra riga nella label
lblShowElab.Text = page
Try
'Formatto i fogli
oBook.Worksheets(page).Range("A1:B1").Merge()
oBook.Worksheets(page).Range("A2:B2").Merge()
...
oBook.Worksheets(page).Range("B2").Borders(xlEdgeRight).LineStyle = xlContinuous
oBook.Worksheets(page).Range("B2").Borders(xlEdgeRight).Weight = xlThin
'Leggo riga per riga
currentRow = MyReader.ReadFields()
'Inserisco i campi di ogni riga nella cella voluta
oBook.Worksheets(page).Range("F2").Value = currentRow(14)
oBook.Worksheets(page).Range("A5").Value = currentRow(12)
...
'Incremento la pagina
page = page + 1
'Se la pagina e' maggiore di 3 la devo creare
If page > 3 Then
oBook.Worksheets.Add(After:=oBook.Worksheets(oBook.Worksheets.Count))
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
End Try
End While
lblShowElab.Text = "Elaborazione Terminata"
End Using
'Salva il Workbook ed esce da Excel
oBook.SaveAs("output.xlsx")
oExcel.Quit()
End Sub
End Class
命令 oBook.Worksheets(page).Range("B2").Borders(xlEdgeRight).LineStyle = xlContinuous oBook.Worksheets(page).Range("B2").Borders(xlEdgeRight).Weight = xlThin 不适用于我是因为 Visual Studio 无法识别并标记我 xlEdgeRight、xlContinuous、xlEdgeRight、xlThin 变量并假装我声明了这一点。
这个逗号在我在互联网上找到的每个示例中都很常见,我不明白为什么不适合我。我是否错过了一些要声明的库或命名空间?我需要的?
希望有人可以帮助我,问候,非常感谢。