提问人:Valkyro 提问时间:11/1/2023 最后编辑:Valkyro 更新时间:11/2/2023 访问量:40
如何使用导入 DocumentFormat.OpenXml.Spreadsheet 设置标签字体/BackColor
How to set label fonts / BackColor with Imports DocumentFormat.OpenXml.Spreadsheet
问:
我继续开发我正在开发的程序,并能够成功地用我的 Excel 电子表格数据填充 DataGridView,所有这些都完美运行(目前)。
但是,我确实注意到的一件事是,由于添加任何更改我的标签字体或 BackColor 的代码行将不起作用,因此例如使用 BackColor: 或例如在 Font: 中指定错误。
现在从我能收集到的信息来看,我的只是用于电子表格的东西,但我并不完全知道,就像我说我的 DataGridView 运行良好一样。但是,我丢失了 BackColor 和 Font 功能。我试图实现的(并且之前已经添加过,但是我需要这一行才能使我的 DataGridView 正常工作)是当我的鼠标放在我的“添加数据”或“联系人”标签上时,例如,字体会略微增加,产生流行效果,并且背面颜色会变为青色,以便它脱颖而出。任何帮助将不胜感激。代码如下图所示:Imports DocumentFormat.OpenXml.Spreadsheet
'FromArgb' is not a member of 'Color'
Value of type 'FontFamily' cannot be converted to OpenXmlElement'
Imports DocumentFormat.OpenXml.Spreadsheet
Imports DocumentFormat.OpenXml.Spreadsheet
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
我注释掉了代码中无法阻止错误的部分,这样我仍然可以处理该项目。这些修复并不重要,但有:)会很好
答:
在进行了研究和大量测试之后,问题就出在了,但是由于与Xml的冲突,解决方法是使用而不是使用。同样可以与字体一起使用,而不是使用 .Imports DocumentFormat.OpenXml.Spreadsheet
Color.FromArgb
System.Drawing.Color.FromArgb
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
Private Sub lblAddData_MouseEnter(sender As Object, e As EventArgs) Handles lblAddData.MouseEnter
lblAddData.Font = New System.Drawing.Font(lblAddData.Font.FontFamily, lblAddData.Font.Size + 2)
End Sub
评论
FromRGB
System.Windows.Media.Color.FromRGB
FromRGB
Color.FromArgb
System.Drawing.Color.FromArgb
System.Drawing