程序中调用Excel控件,同时引用“Microsoft.Office.Interop.Excel”,编译出现"错误 无法从“Microsoft.Office.Interop.Excel._Workbook”转换为“Interop.Excel._Workbook”",请问该如何解决?

解决方案 »

  1.   

      Microsoft.Office.Interop.Excel.WorkbookClass 
      

  2.   

    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;Excel.Application   app   =   new   Excel.ApplicationClass(); 
    Excel.Workbook   xbook=   app.Workbooks.Open(Server.MapPath( "A.xls "),   Missing.Value,   Missing.Value,   Missing.Value,   Missing.Value,   Missing.Value,   Missing.Value,   Missing.Value,   Missing.Value,   Missing.Value,   Missing.Value,   Missing.Value,   Missing.Value,   Missing.Value,   Missing.Value); 
    Excel.Worksheet sheet = app.Sheets[1] as Excel.Worksheet;

    using Excel = Microsoft.Office.Interop.Excel;
    Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
    Excel.Workbook book = null;
    Excel.Worksheet sheet = null;
      

  3.   

    参考
       1. using System;  
       2. using System.Collections.Generic;  
       3. using System.Text;  
       4. using System.Data;  
       5. using System.Data.OleDb;  
       6. using System.Data.SqlClient;  
       7. using System.IO;  
       8. using Microsoft.Office.Interop.Excel;  
       9. namespace TestAccess  
      10. {  
      11.     class Program  
      12.     {  
      13.         static void Main(string[] args)  
      14.         {  
      15.   
      16.             string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";  
      17.             strConnection += @"Data Source=C:\Documents and Settings\v-changl\My Documents\couse.xlsx;";  
      18.             strConnection += "Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";  
      19.             OleDbConnection objConnection = new OleDbConnection(strConnection);  
      20.             objConnection.Open();  
      21.             OleDbDataAdapter myCommandd = new OleDbDataAdapter("select * from [Sheet1$]", objConnection);  
      22.             DataSet ds = new DataSet();  
      23.             myCommandd.Fill(ds, "[Sheet1$]");  
      24.             System.Data.DataTable dt = ds.Tables["[Sheet1$]"];  
      25.             Console.WriteLine(dt.Columns[0].ToString());  
      26.             Console.WriteLine(dt.Columns[1].ToString());  
      27.             DataRow drDisplay = dt.Rows[0];  
      28.             int[] num = new int[dt.Columns.Count];  
      29.             for (int j = 0; ; )  
      30.             {  
      31.                 for (int i = 0; i < dt.Columns.Count; i++)  
      32.                 {  
      33.   
      34.                     if (drDisplay[i] is DBNull) ;  
      35.                     else  
      36.                         num[i] += Convert.ToInt32(drDisplay[i]);  
      37.   
      38.                 }  
      39.                 if (++j >= dt.Rows.Count) break;  
      40.                 drDisplay = dt.Rows[j];  
      41.             }  
      42.             objConnection.Close();  
      43.             object MissingValue = Type.Missing;  
      44.             Microsoft.Office.Interop.Excel.Application app = new Application();  
      45.             Microsoft.Office.Interop.Excel.Workbook wbook = app.Workbooks.Open(@"C:\Documents and Settings\v-changl\My Documents\couse.xlsx", MissingValue,  
      46. MissingValue, MissingValue, MissingValue,  
      47. MissingValue, MissingValue, MissingValue,  
      48. MissingValue, MissingValue, MissingValue,  
      49. MissingValue, MissingValue, MissingValue,  
      50. MissingValue);  
      51.             Microsoft.Office.Interop.Excel.Worksheet wsheet = wbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;  
      52.             for (int i = 0; i < dt.Columns.Count; i++)  
      53.             {  
      54.                 //注意下面是i+1,,excel小标默认从1开始  
      55.                 wsheet.Cells[dt.Rows.Count + 2, i + 1] = num[i].ToString();  
      56.             }  
      57.   
      58.             wbook.Save();  
      59.             wbook.Close(true, null, null);  
      60.             System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet);  
      61.             System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook);  
      62.             System.Runtime.InteropServices.Marshal.ReleaseComObject(app);  
      63.             wsheet = null;  
      64.             wbook = null;  
      65.             app = null;  
      66.             GC.Collect();  
      67.         }  
      68.     }  
      69. }