protected bool IsExistExecl()
{
try
{
xlApp = new Excel.Application();
if (xlApp == null)
{
returnMessage = "无法创建Excel对象,可能您的计算机未安装Excel!";
return false;
}
}
catch (Exception ex)
{
returnMessage = "请正确安装Excel!";
//throw ex;
return false;
} return true;
}
{
try
{
xlApp = new Excel.Application();
if (xlApp == null)
{
returnMessage = "无法创建Excel对象,可能您的计算机未安装Excel!";
return false;
}
}
catch (Exception ex)
{
returnMessage = "请正确安装Excel!";
//throw ex;
return false;
} return true;
}
解决方案 »
- vs2008打了SP1补丁后智能提示变英文的问题!!
- 如果转化byte[]和常规变量类型?
- asp.net 多线程进度条的问题
- C# 怎么让RichTextBox的内容保存为纯文本格式TXT?
- Access 与Oracle 的数据类型转换,相互对应
- 请问如何将DataGridViewTextBoxColumn和DataGridViewButtonColumn组合成一个新列,即带编辑按钮的列?
- combobox已经绑定,如何遍历出它的text和value呢?
- ImageList共用問題
- 请问,C# 如何实现异步非阻塞网络编程?谁有代码,给我发一份,谢谢
- 未将对象引用设置到对象的实例,会是什么原因?
- 求助。vs2012编译的c#程序在win2008R2SP1运行不了
- 求助:一个外部程序调用的窗体,如果关闭外部程序再打开,会重复获取输出
光有赋值,声明呢
当然应该是这样用,xlApp要赋值成Excel.Application的实例,它自己必须是个Excel.Application类型的对象
你不能先把他声明成COM对象啊
好好查查using的用法,不能乱用啊
要么写在类外面,最前面
using system.IO;要么写成
using()
{
}
的形式
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Diagnostics;
using System.Data.OleDb;
using System.Windows.Forms;namespace QRCode
{
public class ExcelIO : IDisposable
{
#region Constructors
private ExcelIO()
{
status = IsExistExecl() ? 0 : -1;
} public static ExcelIO GetInstance()
{
//if(instance == null)
//{
// lock (syncRoot)
// {
// if(instance == null)
// {
// instance = new ExcelIO();
// }
// }
//}
//return instance;
return new ExcelIO();
}
#endregion #region Fields
private static ExcelIO instance;
private static readonly object syncRoot = new object();
private string returnMessage;
private Excel.Application xlApp;
private Excel.Workbooks workbooks = null;
private Excel.Workbook workbook = null;
private Excel.Worksheet worksheet = null;
private Excel.Range range = null;
private int status = -1;
private bool disposed = false;//是否已经释放资源的标记
#endregion #region Properties
/// <summary>
/// 返回信息
/// </summary>
public string ReturnMessage
{
get { return returnMessage; }
} /// <summary>
/// 状态:0-正常,-1-失败 1-成功
/// </summary>
public int Status
{
get { return status; }
}
#endregion #region Methods
/// <summary>
/// 判断是否安装Excel
/// </summary>
/// <returns></returns>
protected bool IsExistExecl()
{
try
{
xlApp = new Excel.Application();
if (xlApp == null)
{
returnMessage = "无法创建Excel对象,可能您的计算机未安装Excel!";
return false;
}
}
catch (Exception ex)
{
returnMessage = "请正确安装Excel!";
//throw ex;
return false;
} return true;
}
没见过这种写法,不报错?
改成
using Microsoft.Office.Interop.Excel;
如果确认已安装了excel,看看它的版本和你在自己机器上引用的Excel组件版本一致
其实你可以在catch块里多一道动态创建的操作
try
{
xlApp = new Excel.Application();
if (xlApp == null)
{
returnMessage = "无法创建Excel对象,可能您的计算机未安装Excel!";
return false;
}
}
catch
{
try
{
var typeCLSID = Guid.Parse("000208D5-0000-0000-C000-000000000046");
type = System.Type.GetTypeFromCLSID(typeCLSID);
object excelComObj = Activator.CreateInstance(type);
}
catch (Exception ex)
{
returnMessage = "请正确安装Excel!";
//throw ex;
return false;
}
}
只给你思路,上面的GUID和progID可以通过 控制面板 > 管理工具 > 组件服务 -> DCOM 中找到(找不到则说明没有安装成功)
object excelComObj = Activator.CreateInstance(type);
//或者你也可以通过ProID,也就是COM组件名的方式创建
object excelComObj = Marshal.GetActiveObject("Microsoft Excel Application"); //VB中也可以用GetObject()
object也可以用Net4.0之后的动态类型
否则你会由于C#编译时的类型检查,使用组件里的函数时非常麻烦(不停地反射),当然用VB.Net也会很方便
先看看office安装时excel的.net编程支持支持是否安装,
如果是精简版或者绿色版的office往往出问题;
其次,查下程序引用的版本和本地的版本是否一致,
不一致,重新添加引用。