c#怎么操作excel,我搞N天到底怎么引用的。using哪些呢?
我是winfrom的用oledb的方式成功了,现在想用那种直接excel对象的,到底要引用哪些东西啊。除了这些呢?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;我不想用sql的方式,网上有这种方式的,这个excel怎么调出来的。
//创建Application对象 
Excel.Application xApp=new Excel.ApplicationClass(); 

解决方案 »

  1.   

    using Excel;
    using System.Reflection;
    使用前要在引用里添加Excel组件
      

  2.   

    是有这么种方式,以下是我们原来限制WORD文档另存后的权限代码,用水晶报表做的
    //限定导出的Word文档的修改权限
            public string CancelModify()
            {
                Object Nothing = System.Reflection.Missing.Value;
                object fileName = this.ExportPath+this.ch_ReportName+".doc";   //动态生成报表名称
                object LockComments = true;
                object savefileName;
                //Application crApplication = new Application();
                Application crApplication = new ApplicationClass();
                //打开刚生成的Word文档
                try
                {
                    Document crDocument = crApplication.Documents.Open(ref fileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                    savefileName = this.ExportPath + this.en_ReportName + "(只读).doc";
                    //将文档锁定后另存
                    crDocument.SaveAs(ref savefileName, ref Nothing, ref LockComments, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);                crDocument.Close(ref Nothing, ref Nothing, ref Nothing);                crApplication.Quit(ref Nothing, ref Nothing, ref Nothing);
                    return (string)savefileName;
                }
                catch (Exception eee)
                {
                    throw new Exception(eee.Message.ToString());
                }         
            }
    这样做需要引用
    using Microsoft.Office.Interop.Word;
    这样引用后如果不设置会报个错,发个邮箱来传个解决方法你
      

  3.   

    using Excel; 
    using System.Reflection; 
    使用前要在引用里添加Excel组件我说老大我直接加了
    using Excel; 
    using System.Reflection; 
    好像通不过啊,说:错误找不到类型或命名空间名称“Excel”(是否缺少 using 指令或程序集引用?) C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\mgExcel\mgExcel\Form1.cs 10 7 mgExcel您老说的 “使用前要在引用里添加Excel组件”到底是怎么引用啊,添加引用:.net有上百个,COM也有上百个哪个才是EXCEL的。
      

  4.   

    一楼正解~好像还有一个Excel的组建~
      

  5.   

    using Excel11;
    using System.Reflection;
    使用前要在引用里添加Excel11组件
      

  6.   

    先添加excel引用,如果是office2003的话版本就是excel11
    然后在引用命名空间
      

  7.   

    ExcelVBA编程, 相关资料google一下先引用Excel的Com组件
    在代码中, 引用命名空间, 如果是office2003的话, using Microsoft.Office.Interop.Excel;
    以下代码打开一个Excel文件:
    ApplicationClass objExcel = new ApplicationClass();
    objExcel.Visible = false;
    objExcel.DisplayAlerts = false;
    Workbooks objWorkbooks = objExcel.Workbooks;
    //打开指定excel文件
    //各参数的意义,请参考Excel VBA手册
    Workbook objWorkbook = objWorkbooks.Open(newFilePath, Type.Missing, Type.Missing, Type.Missing, 
    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    Range objRng = null;//编历工作簿(Workbook)中的所有工作表(sheet), 并获取有效区域内第一个单元格的内容
    //Excel VBA中,索引从1开始
    for (int sheetIndex = 1; sheetIndex <= objWorkbook.Sheets.Count; sheetIndex++)
    {
    Worksheet objWorksheet = (Worksheet)objWorkbook.Sheets[sheetIndex];
    string sheetName = objWorksheet.Name; //编历工作表(Worksheet)的有效范围内的所有单元格
    objRng = objWorksheet.UsedRange;
            int row = 1, col = 1;
    //row,col是相对于UsedRange,不是相对于整个工作表
    Range cellRange = (Range)objRng.Cells[row, col]; //单元格
    string text = cellRange.Text.ToString();
    string formula = cellRange.Formula.ToString();
    }//保存xls
    objWorkbook.Save();
      

  8.   

    在工程-添加引用-.net 然后找到miscrsoft office excel 11.0就行了,可以程序直接misggs....excel
      

  9.   

    先添加excel引用再引入相应的名字空间