System.TypeInitializationException FORM 访问 Class
System.TypeInitializationException FORM access to Class
我正在做一个项目,我需要调用一个 class,我在其中创建和管理一个 excel 文件。但由于未定义的原因,我无法从我的表单中调用此函数。它 return 是一个例外:System.TypeInitializationException。我已经检查了我的 class,但我看不出有什么问题。
我想调用我的表格 Excel_Management class :
Imports System.Diagnostics
Imports System.Windows.Forms
Public Class Proc_Form
Private excel_Manage = New Excel_Management
Public Shared ok As Boolean = New Boolean
If My.Settings.check_Directory = False Then
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
My.Settings.path = FolderBrowserDialog1.SelectedPath
My.Settings.check_Directory = True
'My.Settings.Save()
MsgBox(My.Settings.path)
excel_Manage.check_Excel()
End If
Excel_Management class :
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Imports System.String
Public Class Excel_Management
Public Shared appExcel As Excel.Application = New Excel.Application
Public Shared wsExcel As Excel.Worksheet = New Excel.Worksheet
Public Shared wbExcel As Excel.Workbook = New Excel.Workbook
Public Shared file_name As String = "Name" & DateTime.Now.ToString("yyyy") & ".xls"
Public Shared sheet_name As String = "S " & DatePart(DateInterval.WeekOfYear, Now())
Public Shared check_test As Boolean = False
Public Shared excel_check As New Proc_Form
Public Shared misValue As Object = System.Reflection.Missing.Value
Public Shared message, title, defaultValue As String
Public Shared myValue As Object = New Object
Public Shared i As Integer = 1
Public Shared match As Match = Regex.Match(myValue, "[^a-z0-9]")
Shared Sub check_Excel()
If appExcel Is Nothing Then
MsgBox("No Excel Detected !")
Return
Else
create_Excel()
End If
End Sub
你能从我的代码中看到任何不允许我访问我的 excel class 的问题吗?
提前致谢。
我认为问题在于您无法按照您的方式创建 New Excel.Workbook
,因为它是一个界面。
试试看:
Public Shared wbExcel As Excel.Workbook = appExcel.Workbooks.Add(System.Reflection.Missing.Value)
来自此处:Strange error on Excel Workbook input,或该问题的其他解决方案之一。
此外,您的 Regex.Match
无法匹配对象。我不知道 myValue
是什么,但一个选项可能是:
Regex.Match(myValue.ToString, "[^a-z0-9]")
最后,如果您让 Visual Studio 中的调试器中断这些错误,您将很容易发现这些错误。转到 "DEBUG" 菜单 > "Exceptions..." 并在 "Thrown" 列中的 "Common Language Runtime Exceptions" 旁边打勾。这应该可以帮助您调试任何其他错误。
我正在做一个项目,我需要调用一个 class,我在其中创建和管理一个 excel 文件。但由于未定义的原因,我无法从我的表单中调用此函数。它 return 是一个例外:System.TypeInitializationException。我已经检查了我的 class,但我看不出有什么问题。
我想调用我的表格 Excel_Management class :
Imports System.Diagnostics
Imports System.Windows.Forms
Public Class Proc_Form
Private excel_Manage = New Excel_Management
Public Shared ok As Boolean = New Boolean
If My.Settings.check_Directory = False Then
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
My.Settings.path = FolderBrowserDialog1.SelectedPath
My.Settings.check_Directory = True
'My.Settings.Save()
MsgBox(My.Settings.path)
excel_Manage.check_Excel()
End If
Excel_Management class :
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Imports System.String
Public Class Excel_Management
Public Shared appExcel As Excel.Application = New Excel.Application
Public Shared wsExcel As Excel.Worksheet = New Excel.Worksheet
Public Shared wbExcel As Excel.Workbook = New Excel.Workbook
Public Shared file_name As String = "Name" & DateTime.Now.ToString("yyyy") & ".xls"
Public Shared sheet_name As String = "S " & DatePart(DateInterval.WeekOfYear, Now())
Public Shared check_test As Boolean = False
Public Shared excel_check As New Proc_Form
Public Shared misValue As Object = System.Reflection.Missing.Value
Public Shared message, title, defaultValue As String
Public Shared myValue As Object = New Object
Public Shared i As Integer = 1
Public Shared match As Match = Regex.Match(myValue, "[^a-z0-9]")
Shared Sub check_Excel()
If appExcel Is Nothing Then
MsgBox("No Excel Detected !")
Return
Else
create_Excel()
End If
End Sub
你能从我的代码中看到任何不允许我访问我的 excel class 的问题吗?
提前致谢。
我认为问题在于您无法按照您的方式创建 New Excel.Workbook
,因为它是一个界面。
试试看:
Public Shared wbExcel As Excel.Workbook = appExcel.Workbooks.Add(System.Reflection.Missing.Value)
来自此处:Strange error on Excel Workbook input,或该问题的其他解决方案之一。
此外,您的 Regex.Match
无法匹配对象。我不知道 myValue
是什么,但一个选项可能是:
Regex.Match(myValue.ToString, "[^a-z0-9]")
最后,如果您让 Visual Studio 中的调试器中断这些错误,您将很容易发现这些错误。转到 "DEBUG" 菜单 > "Exceptions..." 并在 "Thrown" 列中的 "Common Language Runtime Exceptions" 旁边打勾。这应该可以帮助您调试任何其他错误。