我继续开发我正在开发的程序,并能够成功地使用我的 Excel 电子表格数据填充 DataGridView,所有这些都运行得很好(目前)。 然而,我确实注意到了这一点,因为添加
Imports DocumentFormat.OpenXml.Spreadsheet
任何更改标签字体或背景颜色的代码都将不起作用,请指定错误,例如使用 BackColor: 'FromArgb' is not a member of 'Color'
或例如字体: Value of type 'FontFamily' cannot be converted to OpenXmlElement'
。
现在,据我所知,我的 Imports DocumentFormat.OpenXml.Spreadsheet
只是用于电子表格的东西,但我并不完全知道,就像我说的那样,我的 DataGridView 工作正常。但是我失去了背景颜色和字体功能。我试图实现的目标(并且在添加 Imports DocumentFormat.OpenXml.Spreadsheet
之前已经完成了,但是我需要这条线才能让我的 DataGridView 正常工作)是当我的鼠标移到“添加数据”或“联系人”标签上时,例如,字体会稍微增加,产生流行效果,并且背景颜色会变为青色,以便脱颖而出。任何帮助将不胜感激。代码如下所示:
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports DocumentFormat.OpenXml.EMMA
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet
Public Class frmDDEF
Private Sub LoadExcelDataToDataGridView()
If Not bckLoader.IsBusy Then
bckLoader.RunWorkerAsync()
End If
End Sub
Private Sub bckLoader_DoWork(sender As Object, e As DoWorkEventArgs) Handles bckLoader.DoWork
Dim filePath As String = "*spreadsheet path*"
' Define the row where your data starts in the Excel file
Dim startingRow As Integer = 1 ' Start from Row 1
Try
Using spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Open(filePath, False)
Dim workbookPart As WorkbookPart = spreadsheetDocument.WorkbookPart
Dim worksheetPart As WorksheetPart = workbookPart.WorksheetParts.First()
Dim worksheet As SheetData = worksheetPart.Worksheet.Elements(Of SheetData)().First()
' Clear existing data in the DataGridView
DataGridView1.Invoke(Sub() DataGridView1.Rows.Clear())
Dim rows = worksheet.Elements(Of Row)().Skip(startingRow - 1)
For Each row In rows
Dim values = row.Elements(Of Cell)().Take(5).Select(Function(cell, colIndex)
Dim cellValue = GetCellValue(workbookPart, cell)
If colIndex = 1 Then ' Assuming Time column is at index 1
Dim timeValue As Double
If Double.TryParse(cellValue, timeValue) Then
Dim totalSeconds As Integer = CInt(timeValue * 24 * 60 * 60)
Dim hours As Integer = totalSeconds \ 3600
Dim minutes As Integer = (totalSeconds \ 60) Mod 60
Dim seconds As Integer = totalSeconds Mod 60
cellValue = $"{hours:D2}:{minutes:D2}:{seconds:D2}"
End If
End If
Return cellValue
End Function).ToList()
Dim hasData = values.Any(Function(value) Not String.IsNullOrEmpty(value))
If hasData Then
DataGridView1.Invoke(Sub() DataGridView1.Rows.Add(values.ToArray()))
End If
Next
End Using
Catch ex As Exception
MsgBox("An error occurred: " & ex.Message)
End Try
End Sub
Private Function GetCellValue(workbookPart As WorkbookPart, cell As Cell) As String
Dim cellValue As String = cell.InnerText
If cell.DataType IsNot Nothing AndAlso cell.DataType.Value = CellValues.SharedString Then
Dim sharedStringTablePart As SharedStringTablePart = workbookPart.GetPartsOfType(Of SharedStringTablePart).First()
If sharedStringTablePart.SharedStringTable.Elements().Count > 0 Then
Dim sharedStringItem As SharedStringItem = sharedStringTablePart.SharedStringTable.Elements().ElementAt(Integer.Parse(cellValue))
cellValue = sharedStringItem.Text.Text
End If
ElseIf cell.DataType IsNot Nothing AndAlso cell.DataType.Value = CellValues.Date Then
' Handle Time format "hh:mm:ss"
Dim excelTimeValue As Double
If Double.TryParse(cellValue, excelTimeValue) Then
Dim totalSeconds As Integer = CInt(excelTimeValue * 24 * 60 * 60)
Dim hours As Integer = totalSeconds \ 3600
Dim minutes As Integer = (totalSeconds \ 60) Mod 60
Dim seconds As Integer = totalSeconds Mod 60
cellValue = $"{hours:D2}:{minutes:D2}:{seconds:D2}"
End If
End If
Return cellValue
End Function
Private Function ConvertToExcelColumnName(columnIndex As Integer) As String
Dim dividend As Integer = columnIndex + 1
Dim columnName As String = String.Empty
While dividend > 0
Dim modulo As Integer = (dividend - 1) Mod 26
columnName = Convert.ToChar(65 + modulo) & columnName
dividend = CInt((dividend - modulo) / 26)
End While
Return columnName
End Function
Private Sub tmrIconLoad_Tick(sender As System.Object, e As System.EventArgs) Handles tmrIconLoad.Tick
Me.ShowIcon = False ' Disables icon once loaded
tmrIconLoad.Enabled = False ' Disables timer
End Sub
Private Sub frmDDEF_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadExcelDataToDataGridView()
tmrIconLoad.Enabled = True ' Triggers Timer to disable Icon once loaded
grpPANELMENU.BackColor = Color.FromArgb(2, 7, 26) ' Sets default colour of the Menu Panel
grpPANELTOP.BackColor = Color.FromArgb(2, 7, 26) ' Sets default colour of the Top Panel (has image on it anyway)
End Sub
' CLICK CODE
Private Sub lblAddData_Click(sender As Object, e As EventArgs) Handles lblAddData.Click
MessageBox.Show("This will allow you to Add Data")
End Sub
Private Sub lblPhoneLog_Click(sender As Object, e As EventArgs) Handles lblPhoneLog.Click
MessageBox.Show("This will allow you to view the Phone Logs")
End Sub
Private Sub lblContact_Click(sender As Object, e As EventArgs) Handles lblContact.Click
frmContact.LoadForm("loading")
End Sub
' HIGHLIGHT CODE
Private Sub lblAddData_MouseMove(sender As Object, e As MouseEventArgs) Handles lblAddData.MouseMove
lblAddData.BackColor = Color.FromArgb(3, 182, 252) ' Changes the Add Data Label to Cyan Highlight on the Menu Panel
End Sub
Private Sub lblPhoneLog_MouseMove(sender As Object, e As MouseEventArgs) Handles lblPhoneLog.MouseMove
' lblPhoneLog.BackColor = Color.FromArgb(3, 182, 252) ' Changes the Phone Log Label to Cyan Highlight on the Menu Panel
End Sub
Private Sub lblContact_MouseMove(sender As Object, e As MouseEventArgs) Handles lblContact.MouseMove
' lblContact.BackColor = Color.FromArgb(3, 182, 252) ' Changes the Contact Label to Cyan Highlight on the Menu Panel
End Sub
Private Sub frmDDEF_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
' lblAddData.BackColor = Color.FromArgb(2, 7, 26) ' Removes the Cyan Highlight from the Add Data Label when the mouse goes over the Form
'lblPhoneLog.BackColor = Color.FromArgb(2, 7, 26) ' Removes the Cyan Highlight from the Phone Log Label when the mouse goes over the Form
' lblContact.BackColor = Color.FromArgb(2, 7, 26) ' Removes the Cyan Highlight from the Contact Label when the mouse goes over the Form
End Sub
Private Sub grpPANELMENU_MouseMove(sender As Object, e As MouseEventArgs) Handles grpPANELMENU.MouseMove
' lblAddData.BackColor = Color.FromArgb(2, 7, 26) ' Removes the Cyan Highlight from the Add Data Label when the mouse goes over the Panel Menu
' lblPhoneLog.BackColor = Color.FromArgb(2, 7, 26) ' Removes the Cyan Highlight from the Phone Log Label when the mouse goes over the Panel Menu
' lblContact.BackColor = Color.FromArgb(2, 7, 26) ' Removes the Cyan Highlight from the Contact Label when the mouse goes over the Panel Menu
End Sub
' SELECTED LABEL EFFECT
Private Sub lblAddData_MouseEnter(sender As Object, e As EventArgs) Handles lblAddData.MouseEnter
lblAddData.Font = New Font(lblAddData.Font.FontFamily, lblAddData.Font.Size + 2)
End Sub
Private Sub lblAddData_MouseLeave(sender As Object, e As EventArgs) Handles lblAddData.MouseLeave
' lblAddData.Font = New Font(lblAddData.Font.FontFamily, lblAddData.Font.Size - 2)
End Sub
Private Sub lblPhoneLog_MouseEnter(sender As Object, e As EventArgs) Handles lblPhoneLog.MouseEnter
' lblPhoneLog.Font = New Font(lblPhoneLog.Font.FontFamily, lblPhoneLog.Font.Size + 2)
End Sub
Private Sub lblPhoneLog_MouseLeave(sender As Object, e As EventArgs) Handles lblPhoneLog.MouseLeave
' lblPhoneLog.Font = New Font(lblPhoneLog.Font.FontFamily, lblPhoneLog.Font.Size - 2)
End Sub
Private Sub lblContact_MouseEnter(sender As Object, e As EventArgs) Handles lblContact.MouseEnter
' lblContact.Font = New Font(lblContact.Font.FontFamily, lblContact.Font.Size + 2)
End Sub
Private Sub lblContact_MouseLeave(sender As Object, e As EventArgs) Handles lblContact.MouseLeave
' lblContact.Font = New Font(lblContact.Font.FontFamily, lblContact.Font.Size - 2)
End Sub
End Class
我注释掉了无法阻止错误的代码部分,以便我仍然可以继续该项目。这些修复并不重要,但如果有的话那就太好了:)
经过研究和大量测试后,
Imports DocumentFormat.OpenXml.Spreadsheet
是问题所在,但解决方法是不要使用Color.FromArgb
,而是使用System.Drawing.Color.FromArgb
,因为与 Xml 发生冲突。同样可以与字体一起使用,而不是使用 New Font
。示例如下:
System.Drawing.Font
Private Sub lblAddData_MouseMove(sender As Object, e As MouseEventArgs) Handles lblAddData.MouseMove
lblAddData.BackColor = System.Drawing.Color.FromArgb(3, 182, 252) ' Changes the Add Data Label to Cyan Highlight on the Menu Panel
End Sub