vb.net开发中的office版本问题
我自己电脑上安装的是office2003,所以在vb.net的项目添加的引用是“Microsoft office 11.0 object library”。但到了别人那里,他用的是office 2002,这样程序就会运行出错。提示“System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.”请问,在开发中,如何解决这个office版本不同的问题?谢谢! --------------------编程问答-------------------- 把Microsoft.Office.Interop.Excel.dll複製過去,放到安裝目錄下。 --------------------编程问答-------------------- 我也遇到过,我的解决方法是:将你使用到的DLL放到安装包中,安装时复制到安装目录下 --------------------编程问答-------------------- 答案是用后期绑定:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ExcelOpt
{
/// <summary>
/// Excel后期绑定的操作类
/// </summary>
public class Application
{
object m_ExcelApp;
public Application()
{
Type objExcelType = Type.GetTypeFromProgID("Excel.Application");
if (objExcelType == null)
{
throw new Exception("未发现Excel程序");
}
m_ExcelApp = Activator.CreateInstance(objExcelType);
if (m_ExcelApp == null)
{
throw new Exception("启用Excel程序失败!");
}
}
public void Open(string ExcelFileName)
{
//检查文件是否存在
if (!System.IO.File.Exists(ExcelFileName))
throw new Exception("要打开的文件" + ExcelFileName + "不存在!");
//打开文件
if (m_ExcelApp != null)
{
object[] parameters = new object[1] { ExcelFileName };
}
}
public void SaveAs(string ExcelFileName)
{
}
public bool Visible
{
get
{
object objVisible =
m_ExcelApp.GetType().InvokeMember("Visible", BindingFlags.GetProperty, null, m_ExcelApp, null);
if (objVisible is Boolean)
return (bool)objVisible;
else
throw new Exception("调用方法失败!");
}
set
{
object[] Parameters = new object[1] { value };
m_ExcelApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, m_ExcelApp, Parameters);
}
}
public Workbooks Workbooks
{
get
{
object workbooks = m_ExcelApp.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, m_ExcelApp, null);
if (workbooks == null)
throw new Exception("查询工作簿时失败!");
else
return new Workbooks(workbooks);
}
}
}
}
namespace ExcelOpt
{
public class Workbooks
{
private object m_Workbooks;
private string m_ExcelFileName;
public Workbooks(object workbooks)
{
m_Workbooks = workbooks;
}
public void Open(string ExcelFileName)
{
try
{
m_ExcelFileName = ExcelFileName;
object[] Parameters = new object[1] { ExcelFileName };
m_Workbooks.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, m_Workbooks, Parameters);
}
catch (Exception err)
{
throw err;
}
}
public Workbook this[int index]
{
get
{
object[] Parameters = new object[1]{index };
object workbook = m_Workbooks.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, m_Workbooks, Parameters);
if (workbook == null)
throw new Exception("获取工作薄时出现错误!");
else
return new Workbook(workbook);
}
}
}
}
namespace ExcelOpt
{
public class Workbook
{
private object m_Workbook;
public Workbook(object workbook)
{
m_Workbook = workbook;
}
public Worksheets Worksheets
{
get
{
object worksheets = m_Workbook.GetType().InvokeMember("Worksheets", System.Reflection.BindingFlags.GetProperty, null, m_Workbook, null);
if (worksheets == null)
throw new Exception("获取工作表集合时失败!");
else
return new Worksheets(worksheets);
}
}
public void SaveAs(string FileName)
{
object[] Parameters = new object[1] { FileName };
m_Workbook.GetType().InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, m_Workbook, Parameters);
}
}
}
namespace ExcelOpt
{
public class Worksheets
{
private object m_Worksheets;
public Worksheets ( object worksheets )
{
m_Worksheets = worksheets;
}
public Worksheet this[int index]
{
get
{
object[] Parameters = new object[1] { index };
object worksheet = m_Worksheets.GetType().InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, m_Worksheets, Parameters);
if (worksheet == null)
throw new Exception("获取工作表时出现错误!");
else
return new Worksheet(worksheet);
}
}
}
}
namespace ExcelOpt
{
public class Worksheet
{
private object m_Worksheet;
public Worksheet ( object worksheet )
{
m_Worksheet = worksheet ;
}
public Range this[ int row , int col ]
{
get
{
object[] Parameters = new Object[2] { row , col };
object cells = m_Worksheet.GetType().InvokeMember("Cells", System.Reflection.BindingFlags.GetProperty, null, m_Worksheet, Parameters);
if (cells == null)
throw new Exception("获取单元格失败!");
else
return new Range(cells);
}
}
public Range this[ int row , string col ]
{
get
{
object[] Parameters = new Object[2] { row, col };
object cells = m_Worksheet.GetType().InvokeMember("Cells", System.Reflection.BindingFlags.GetProperty, null, m_Worksheet, Parameters);
if (cells == null)
throw new Exception("获取单元格失败!");
else
return new Range(cells);
}
}
}
}
--------------------编程问答-------------------- 漏了一个:)
namespace ExcelOpt
{
public class Range
{
private object m_Range;
public Range(object Range)
{
m_Range = Range;
}
public object Value
{
get
{
object result = m_Range.GetType().InvokeMember("Value", System.Reflection.BindingFlags.GetProperty, null, m_Range, null);
return result;
}
set
{
object[] Parameters = new Object[1] { value };
m_Range.GetType().InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, null, m_Range, Parameters);
}
}
}
}
--------------------编程问答-------------------- 在参照上的copy选项上选Copy Always,自动拷贝DLL过去就行了。 --------------------编程问答-------------------- 请问limeng0513,这个“参照”在哪可以找到?谢谢! --------------------编程问答-------------------- 点击显示所有文件按钮,可以看到参照设置,是文件夹形式,点击参照的Excel,属性里的拷贝项目选总是拷贝。 --------------------编程问答-------------------- --------------------编程问答-------------------- 在解决方案资源管理器中的引用项的属性中,把“复制本地”属性设置成“TRUE”,然后再制作安装包的时候把BIN下的所有都包括进去。
补充:.NET技术 , VB.NET