VB2008数据输出到Excel2007时报错,无法将COM 对象强制转换为接口类型,求指教!
在XP里,VB2008+Excel2007测试通过,在Win764位系统里+VB2010+Excel2007总是错误。程序部分代码:Importsexcel=Micros...
在XP里,VB2008+Excel2007测试通过,在Win7 64位系统里+VB2010+Excel2007总是错误。
程序部分代码:
Imports excel = Microsoft.Office.Interop.Excel
Friend Class outE
Inherits System.Windows.Forms.Form
Public Sub bctBatExcel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles bctBatExcel.Click
Dim xlsapp As New Microsoft.Office.Interop.Excel.Application
Dim Cbook As Microsoft.Office.Interop.Excel.Workbook
Dim Csheet As Microsoft.Office.Interop.Excel.Worksheet
xlsapp = New Microsoft.Office.Interop.Excel.ApplicationClass
Cbook = scxls.Workbooks.Add(True) ‘错误提示行
Csheet = scbook.Worksheets(1)
错误提示:
无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel._Application”。此操作失败的原因是对 IID 为“{000208D5-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 加载类型库/DLL 时出错。 (异常来自 HRESULT:0x80029C4A (TYPE_E_CANTLOADLIBRARY))。
.
代码部分修正一下:
Dim xlsapp As New Microsoft.Office.Interop.Excel.Application
Dim Cbook As Microsoft.Office.Interop.Excel.Workbook
''''Dim Cbook As Microsoft.Office.Interop.Excel.WorkbookClass '无效
Dim Csheet As Microsoft.Office.Interop.Excel.Worksheet
xlsapp = New Microsoft.Office.Interop.Excel.ApplicationClass
''''xlsapp = New Microsoft.Office.Interop.Excel.Application ‘无效
''''xlsapp = CreateObject("Excel.Application") '无效
Cbook = xlsapp.Workbooks.Add(True) '错误提示行
Csheet = Cbook.Worksheets(1) 展开
程序部分代码:
Imports excel = Microsoft.Office.Interop.Excel
Friend Class outE
Inherits System.Windows.Forms.Form
Public Sub bctBatExcel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles bctBatExcel.Click
Dim xlsapp As New Microsoft.Office.Interop.Excel.Application
Dim Cbook As Microsoft.Office.Interop.Excel.Workbook
Dim Csheet As Microsoft.Office.Interop.Excel.Worksheet
xlsapp = New Microsoft.Office.Interop.Excel.ApplicationClass
Cbook = scxls.Workbooks.Add(True) ‘错误提示行
Csheet = scbook.Worksheets(1)
错误提示:
无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel._Application”。此操作失败的原因是对 IID 为“{000208D5-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 加载类型库/DLL 时出错。 (异常来自 HRESULT:0x80029C4A (TYPE_E_CANTLOADLIBRARY))。
.
代码部分修正一下:
Dim xlsapp As New Microsoft.Office.Interop.Excel.Application
Dim Cbook As Microsoft.Office.Interop.Excel.Workbook
''''Dim Cbook As Microsoft.Office.Interop.Excel.WorkbookClass '无效
Dim Csheet As Microsoft.Office.Interop.Excel.Worksheet
xlsapp = New Microsoft.Office.Interop.Excel.ApplicationClass
''''xlsapp = New Microsoft.Office.Interop.Excel.Application ‘无效
''''xlsapp = CreateObject("Excel.Application") '无效
Cbook = xlsapp.Workbooks.Add(True) '错误提示行
Csheet = Cbook.Worksheets(1) 展开
1个回答
展开全部
把这句 xlsapp = New Microsoft.Office.Interop.Excel.ApplicationClass
改成这样行不行 xlsapp = New Microsoft.Office.Interop.Excel.Application
改成这样行不行 xlsapp = New Microsoft.Office.Interop.Excel.Application
更多追问追答
追问
已试过,不好用,错误提示相同
追答
从"工程"菜单中选择"引用"栏;选择Microsoft Excel 11.0 Object Library(EXCEL2003),或者其他版本的,然后选择"确定"。表示在工程中要引用EXCEL类型库。
参考以下程序片段吧
Dim xlApp As Excel.Application
Dim xlBook As Excel.WorkBook
Dim xlSheet As Excel.Worksheet
Dim FileName, SheetName As String
FileName = "e:\data.xls" '要访问的工作簿路径和名称
SheetName = "sheet1" '指定的要访问的工作表名称
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open(FileName) '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlSheet = xlBook.Worksheets(SheetName) '设置活动工作表
xlSheet.Cells(5, 2) = Text1.Text '给单元格B5赋值
xlSheet.Cells(2, 3) = Text2.Text '给单元格C2赋值
xlBook.Close (True) '关闭工作簿
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询