'Msoapplanguageid'在命名空间'microsoft.office.core'中含糊不清。 [英] 'Msoapplanguageid' is ambiguous in the namespace 'microsoft.office.core'.
问题描述
我正在使用visual studio 2010(VB.net技术)和Office 2010我正在尝试在vb.net中使用excel功能我遇到以下错误:
MsoAppLanguageID在名称空间Microsoft.Office.Core中不明确。
我添加两个以下参考:Microsoft Excel 14.0对象库Microsoft
Office 14.0对象库
并导入以下代码:< br $> b $ b
Imports System.Runtime.InteropServices
以下是以下功能代码:
公共 函数 ExportToExcel( ByVal a_sFilename As 字符串, ByVal a_sData 作为 DataTable, ByVal a_sFileTitle 作为 字符串, ByRef a_sErrorMessage As String ) As 布尔值
a_sErrorMessage = 字符串 .Empty
Dim bRetVal 作为 布尔 = False
Dim dsDataSet As DataTable = 没什么
尝试
dsDataSet = a_sData
Dim xlObject 作为 Microsoft.Office.Interop.Excel.Application = Nothi ng
Dim xlWB As Microsoft.Office.Interop.Excel.Workbook = Nothing
Dim xlSh As Microsoft.Office.Interop.Excel.Worksheet = Nothing
Dim rg As Microsoft.Office.Interop.Excel.Range = Nothing
' 到目前为止,问题必须是关于系统的文化信息,所以我定义了新的参数来处理文化信息。
Dim newCulture 作为 System.Globalization.CultureInfo ' 保存新的文化信息
Dim OldCulture As System.Globalization.CultureInfo ' 保存旧文化信息
OldCulture = System.Threading.Thread.CurrentThread.CurrentCulture
newCulture = 新 System.Globalization.CultureInfo(_
xlObject.LanguageSettings.LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI))
System.Threading.Thread.CurrentThread .CurrentCulture = newCulture
尝试
xlObject = 新 Microsoft.Office.Interop.Excel.Application()
' xlObject.AlertBeforeOverwriting = False
' xlObject.DisplayAlerts = False
' '这个添加一个新的woorkbook,你也可以从文件中打开工作簿
xlWB = xlObject.Workbooks.Add()
' 我自定义了这一行......
xlWB.SaveAs(a_sFilename,_
56 ,_
123456,_
654321,_
True ,_
True ,_
1 ,_
1 ,_
True ,_
True )
xlSh = DirectCast (xlObject.ActiveWorkbook.ActiveSheet,Microsoft.Office.Interop.Excel.Worksheet)
< span class =code-keyword>对于 j 作为 整数 = 0 dsDataSet.Columns.Count - 1
xlSh.Cells( 1 ,j + 1 )= _
dsDataSet.Columns(j ).ToString()
xlSh.Cells( 1 ,j + 1 )。字体。粗体= True
下一步
对于 i 作为 整数 = 1 dsDataSet.Rows.Count
对于 j As 整数 = 0 dsDataSet.Columns.Count - 1
xlSh.Cells(i + 1 ,j + 1 )= _
dsDataSet.Rows(i - 1 )(j).ToString()
下一步
下一步
xlSh.Columns.AutoFit()
如果 取值tring .IsNullOrEmpty(a_sFileTitle)然后
xlObject.Caption = 无标题
否则
xlObject.Caption = a_sFileTitle
结束 如果
xlWB.Save()
bRetVal = True
Catch ex As System.Runtime.InteropServices .COMException
如果 ex.ErrorCode = -2147221164 那么
a_sErrorMessage = 导出错误:请安装Microsoft Office(Excel)以使用导出到Excel功能。
' showmessage(a_sErrorMessage)
ElseIf ex.ErrorCode = -2146827284 然后
a_sErrorMessage = 导出时出错:Excel在工作表中只允许最多65,536行。
' showmessage(a_sErrorMessage)
其他
a_sErrorMessage =(( 导出错误:& ex.Message)+ Environment.NewLine& 错误:)+ ex.ErrorCode
' showmessage(a_sErrorMessage)
结束 如果
Catch ex As 例外
a_sErrorMessage = (( 导出错误:& ex.Message)+ Environment.NewLine& 错误:)
' showmessage(a_sErrorMessage)
最后
尝试
如果 xlWB IsNot Nothing 然后
xlWB.Close( Nothing , Nothing , Nothing )
结束 如果
xlObject.Workbooks.Close()
xlObject.Quit()
如果 rg IsNot 没什么 然后
Marshal.ReleaseComObject(rg)
结束 如果
如果 xlSh IsNot 没什么 然后
Marshal.ReleaseComObject(xlSh)
结束 如果
如果 xlWB IsNot 没什么 然后
Marshal.ReleaseComObject(xlWB)
结束 如果
如果 xlObject IsNot Nothing < span class =code-keyword>然后
Marshal.ReleaseComObject(xlObject)
结束 如果
Catch
结束 尝试
xlSh = 没什么
xlWB = 没什么
xlObject = 没什么
' 强制最终清理!
GC.Collect()
GC.WaitForPendingFinalizers()
结束 尝试
Catch ex As 例外
a_sErrorMessage = < span class =code-string> 导出错误:& ex.Message
' showmessage(a_sErrorMessage)
结束 尝试
返回 bRetVal
结束 功能
私有 Sub showmessage( ByVal a_sErrorMessage As String )
MyMessageBox.showMsg(MyMessageBox.typeMsag.ErrorMsag, ,a_sErrorMessage)
结束 Sub
我找不到办法解决问题,任何帮助或建议都会有所帮助,
谢谢,
我尝试了什么:
删除并添加引用,然后重新导入,但错误仍然存在。
问题在于添加对项目的额外引用,我删除了所有办公依赖项,只添加了excel引用并使用了interop.Excel,因此它得到了解决。 / BLOCKQUOTE>
I'm using visual studio 2010 (VB.net technology) and office 2010 I'm trying to use excel functionalities in vb.net I face the error below:
MsoAppLanguageID is ambiguous in the namespace Microsoft.Office.Core.
I add two following references: Microsoft Excel 14.0 Object Library MicrosoftOffice 14.0 Object Library
And import the following code:
Imports System.Runtime.InteropServices
Here is the following function code:
Public Function ExportToExcel(ByVal a_sFilename As String, ByVal a_sData As DataTable, ByVal a_sFileTitle As String, ByRef a_sErrorMessage As String) As Boolean a_sErrorMessage = String.Empty Dim bRetVal As Boolean = False Dim dsDataSet As DataTable = Nothing Try dsDataSet = a_sData Dim xlObject As Microsoft.Office.Interop.Excel.Application = Nothing Dim xlWB As Microsoft.Office.Interop.Excel.Workbook = Nothing Dim xlSh As Microsoft.Office.Interop.Excel.Worksheet = Nothing Dim rg As Microsoft.Office.Interop.Excel.Range = Nothing 'the problem so far must be about the culture info of the system so i define to new parameters to deal with culture info. Dim newCulture As System.Globalization.CultureInfo 'To save new Culture info Dim OldCulture As System.Globalization.CultureInfo 'To hold the old Culture info OldCulture = System.Threading.Thread.CurrentThread.CurrentCulture newCulture = New System.Globalization.CultureInfo( _ xlObject.LanguageSettings.LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI)) System.Threading.Thread.CurrentThread.CurrentCulture = newCulture Try xlObject = New Microsoft.Office.Interop.Excel.Application() 'xlObject.AlertBeforeOverwriting = False 'xlObject.DisplayAlerts = False ''This Adds a new woorkbook, you could open the workbook from file also xlWB = xlObject.Workbooks.Add() ' I customized this row... xlWB.SaveAs(a_sFilename, _ 56, _ "123456", _ "654321", _ True, _ True, _ 1, _ 1, _ True, _ True) xlSh = DirectCast(xlObject.ActiveWorkbook.ActiveSheet, Microsoft.Office.Interop.Excel.Worksheet) For j As Integer = 0 To dsDataSet.Columns.Count - 1 xlSh.Cells(1, j + 1) = _ dsDataSet.Columns(j).ToString() xlSh.Cells(1, j + 1).Font.Bold = True Next For i As Integer = 1 To dsDataSet.Rows.Count For j As Integer = 0 To dsDataSet.Columns.Count - 1 xlSh.Cells(i + 1, j + 1) = _ dsDataSet.Rows(i - 1)(j).ToString() Next Next xlSh.Columns.AutoFit() If String.IsNullOrEmpty(a_sFileTitle) Then xlObject.Caption = "untitled" Else xlObject.Caption = a_sFileTitle End If xlWB.Save() bRetVal = True Catch ex As System.Runtime.InteropServices.COMException If ex.ErrorCode = -2147221164 Then a_sErrorMessage = "Error in export: Please install Microsoft Office (Excel) to use the Export to Excel feature." 'showmessage(a_sErrorMessage) ElseIf ex.ErrorCode = -2146827284 Then a_sErrorMessage = "Error in export: Excel allows only 65,536 maximum rows in a sheet." 'showmessage(a_sErrorMessage) Else a_sErrorMessage = (("Error in export: " & ex.Message) + Environment.NewLine & " Error: ") + ex.ErrorCode 'showmessage(a_sErrorMessage) End If Catch ex As Exception a_sErrorMessage = (("Error in export: " & ex.Message) + Environment.NewLine & " Error: ") 'showmessage(a_sErrorMessage) Finally Try If xlWB IsNot Nothing Then xlWB.Close(Nothing, Nothing, Nothing) End If xlObject.Workbooks.Close() xlObject.Quit() If rg IsNot Nothing Then Marshal.ReleaseComObject(rg) End If If xlSh IsNot Nothing Then Marshal.ReleaseComObject(xlSh) End If If xlWB IsNot Nothing Then Marshal.ReleaseComObject(xlWB) End If If xlObject IsNot Nothing Then Marshal.ReleaseComObject(xlObject) End If Catch End Try xlSh = Nothing xlWB = Nothing xlObject = Nothing ' force final cleanup! GC.Collect() GC.WaitForPendingFinalizers() End Try Catch ex As Exception a_sErrorMessage = "Error in export: " & ex.Message 'showmessage(a_sErrorMessage) End Try Return bRetVal End Function Private Sub showmessage(ByVal a_sErrorMessage As String) MyMessageBox.showMsg(MyMessageBox.typeMsag.ErrorMsag, "", a_sErrorMessage) End Sub
I can't find a way to solve the problem, Any help or advise would be helpful,
Thanks,
What I have tried:
removing and adding the references, and re-importing, but the error still remains.解决方案The problem was with adding extra references to the project, i removed all office dependencies and only added the excel reference and used the interop.Excel so it got solved.
这篇关于'Msoapplanguageid'在命名空间'microsoft.office.core'中含糊不清。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!