我想用C#读取一个Excel模板,然后将从库中计算出的数据填写到模板的指定位置上。我从CSDN上看了一些关于这类问题的帖子,可是照那些方法都行不同,比如
Excel.Application xlApp = new Excel.Application();
xlApp.Visible=true;
等等很多的方法报错或提示没有这个方法。我想请教一下有哪位朋友真正操作成功了的,请帮个忙,先谢谢了!
    另外,好像不同的Excel版本要引用不同的控件,这个在程序里怎样做,怎样实现?谢谢!

解决方案 »

  1.   

    从"工程"菜单中选择"引用"栏;
    选择Microsoft Excel 9.0 Object Library(EXCEL2000),然后选择"确定"。表示在工程
    中要引用EXCEL类型库。
      

  2.   

    从"工程"菜单中选择"引用"栏;
    选择Microsoft Excel 11.0 Object Library(EXCEL2003),然后选择"确定"。表示在工程
    中要引用EXCEL类型库。
      

  3.   

    抱歉,我忘记提了,我安装的是Excel2000,工程中已经引入了Excel包,即【从"工程"菜单中选择"引用"栏;选择Microsoft Excel 9.0 Object Library(EXCEL2000),然后选择"确定"。】,但是很多方法还是无法实现!
      

  4.   

    to  aiqinbird(爱琴鸟) 
    你有成功例子吗?请发出来吧,谢谢!
      

  5.   

    我写过这样的程序哈,还要将excel的操作权限给.asp的用户哈
    我写的代码如下,用的也是excel2000,你看下哈
    string strMBfile = "",strNewfile = "",strFilePath="",strMBFilePath = "",strNewFilePath="",str_FileUrl="";
    strFilePath = Request.ServerVariables.Get("PATH_TRANSLATED");
    str_FileUrl = Request.Url.ToString();
    strMBFilePath = strFilePath.Substring(0,strFilePath.LastIndexOf("\\"));
    strMBfile = strMBFilePath + "\\excel模版.xls";
    if(!File.Exists(strMBfile))
    {Alert.Show("EXCEL模版不存在或路径不正确!");}
    Random nRandom = new Random(DateTime.Now.Millisecond);
    strNewFilePath = nRandom.NextDouble()+ ".xls";
    strNewfile = strMBFilePath + "\\temp\\" + strNewFilePath;
    if(File.Exists(strNewfile))
    { File.Delete(strNewfile); }
    File.Copy(strMBfile,strNewfile);

    #region//往模板写数据
    Excel.Application myApp;
    Excel.Workbook myBook;
    Excel.Worksheet mySheet;
    Excel.Sheets mySheets;
    myApp=null; myBook = null; mySheet = null;
    object oMissiong = System.Reflection.Missing.Value;
    myApp = new Excel.ApplicationClass();
    myApp.Visible=false;
    myApp.Workbooks.Open(strNewfile.Replace(".txt",".xls"),oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
    myBook = myApp.Workbooks[1];
    mySheets = myBook.Worksheets;
    mySheet = (Excel.Worksheet)mySheets[1];
    mySheet.Activate();
    //开始写如数据
    mySheet.Cells[1,1]= str_FileUrl.Substring(0,str_FileUrl.LastIndexOf("/")) + "/temp/" + strNewFilePath.Replace(".xls",".txt"); 

    myBook.Save();
    myBook.Close(true,strNewfile,true);
    这里可以打操作excel基本上所的功能,如列宽,行宽等,具体的调用excel的那些函数你最好看下excel的vba帮助。这个最大的问题在于如果写入的数据太大,就不行了哈,时间太长了!
      

  6.   

    我来回吧:
      
                               Excel.Application myApp=null;
    Workbook myBook=null;
    Worksheet mySheet=null;
             myApp= new ApplicationClass();
    myApp.Visible=false;
    object oMissiong=System.Reflection.Missing.Value;
    myBook=myApp.Workbooks.Add(true);
    mySheet=(Worksheet)myBook.ActiveSheet;
    mySheet.Cells.Select();
    mySheet.Cells.NumberFormatLocal = "@"; //写进表格中的数据
    //
    mySheet.Cells[1,1]="工单号";
    mySheet.Cells[1,2]="品牌";
                               .............
      

  7.   

    我也正在用读入Excel刚开始用,还不太会
    我引入的是:Microsoft Excel 11.0 Object Library(EXCEL2003),你申明的时候写出名字空间全路径就应该没有问题了:private Microsoft.Office.Interop.Excel.Application myExcel1;
    private Microsoft.Office.Interop.Excel.Workbook myWorkbook1;
    private Microsoft.Office.Interop.Excel.Worksheet mySheets1;this.myExcel1=new  Microsoft.Office.Interop.Excel.ApplicationClass();
    this.myWorkbook1=this.myExcel1.Workbooks.Add(1);
    this.mySheets1=(Microsoft.Office.Interop.Excel.Worksheet)this.myWorkbook1.Sheets[1];for(int i=0;i<tb1.Columns.Count;i++)
    {
    this.mySheets1.Cells[1,i+1]=tb1.Columns[i].ColumnName;
    }
    for(int i=0;i<tb1.Rows.Count;i++)
    {
    for(int j=0;j<tb1.Columns.Count;j++)
    {
    this.mySheets1.Cells[i+2,j+1]=tb1.Rows[i][j].ToString();
    }
    }
     this.myExcel1.Visible=true;
      

  8.   

    myApp= new ApplicationClass();
    程序运行到这一句的时候会报错:
    拒绝访问。 
    异常详细信息: System.UnauthorizedAccessException: 拒绝访问。 请问怎么解决?
      

  9.   

    Excel.Application objApp;
    Excel._Workbook objBook;
    Excel.Workbooks objBooks;
    Excel._Worksheet objSheet;objApp = new Excel.ApplicationClass();objBooks = objApp.Workbooks;
    objBook = objBooks.Add(System.Reflection.Missing.Value);objSheet = (Excel._Worksheet)objBook.Sheets.get_Item(1);
      

  10.   

    zhjchinese(学习学习在学习)
    我编译的时候,提示如下:
    d:\inetpub\wwwroot\ExcelDemo\outExcel.aspx.cs(30): “Excel.Application”并不包含对“Visible”的定义
    d:\inetpub\wwwroot\ExcelDemo\outExcel.aspx.cs(31): “Excel.Application”并不包含对“Workbooks”的定义
    d:\inetpub\wwwroot\ExcelDemo\outExcel.aspx.cs(32): “Excel.Application”并不包含对“Workbooks”的定义
    d:\inetpub\wwwroot\ExcelDemo\outExcel.aspx.cs(33): “Excel.Workbook”并不包含对“Worksheets”的定义