zoukankan      html  css  js  c++  java
  • 类型“Microsoft.Office.Interop.Excel.ApplicationClass”未定义构造函数

    前两天写了一个导出excel的方法:

    1. protected void AddExcel(DataSet ds,string strName)   
    2.         {   
    3.             DataTable dt = ds.Tables[0];   
    4.   
    5.             string fileName = strName + ".xls";   
    6.   
    7.             Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();   
    8.   
    9.             int rowIndex = 1;   
    10.             int colIndex = 0;   
    11.   
    12.             excel.Application.Workbooks.Add(true);   
    13.   
    14.             foreach (DataColumn col in dt.Columns)   
    15.             {   
    16.                 colIndex++;   
    17.                 excel.Cells[1, colIndex] = col.ColumnName;   
    18.             }   
    19.   
    20.             foreach (DataRow row in dt.Rows)   
    21.             {   
    22.                 rowIndex++;   
    23.                 colIndex = 0;   
    24.                 for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)   
    25.                 {   
    26.                     excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();   
    27.                 }   
    28.             }   
    29.   
    30.             excel.Visible = false;   
    31.             excel.ActiveWorkbook.SaveAs(fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);   
    32.             //excel.Save(fileName);    
    33.   
    34.             excel.Quit();   
    35.             excel = null;   
    36.   
    37.             GC.Collect();//垃圾回收    
    38.         }  

    protected void AddExcel(DataSet ds,string strName)       

     {           

     DataTable dt = ds.Tables[0];            string fileName = strName + ".xls";            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();            int rowIndex = 1;            int colIndex = 0;            excel.Application.Workbooks.Add(true);            foreach (DataColumn col in dt.Columns)            {                colIndex++;                excel.Cells[1, colIndex] = col.ColumnName;            }            foreach (DataRow row in dt.Rows)            {                rowIndex++;                colIndex = 0;                for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)                {                    excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();                }            }            excel.Visible = false;            excel.ActiveWorkbook.SaveAs(fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);            //excel.Save(fileName);             excel.Quit();            excel = null;            GC.Collect();//垃圾回收         } 

    ,之前没用过这种方式,结果出现以下错误:
    类型“Microsoft.Office.Interop.Excel.ApplicationClass”未定义构造函数    
    无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口。



    解决方法:将引用的DLL:Microsoft.Office.Interop.Excel;的嵌入互操作类型改为false,就可以了。

  • 相关阅读:
    C#--事件驱动在上位机中的应用【一】(搭建仿真PLC环境)
    C#--事件驱动在上位机中的应用【三】(自定义控件)
    C#--事件驱动在上位机中的应用【二】(自定义控件)
    C#--属性--propfull和prop使用场所
    C#--通过Modbus TCP与西门子1200PLC通讯
    C#--简单调用WebService
    C#-- 简单新建WebService服务
    C#--发布WebService和部署IIS到本地服务器
    P1909 买铅笔
    P1089 津津的储蓄计划
  • 原文地址:https://www.cnblogs.com/zhoukuan0905/p/2109126.html
Copyright © 2011-2022 走看看