在Button时间中
{
Excel.Application excel= new Excel.Application(); 
int rowIndex=1; 
int colIndex=0;  excel.Application.Workbooks.Add(true); 
     
DataTable table=GetData(); 
      
foreach(DataColumn col in table.Columns) 

colIndex++;  
excel.Cells[1,colIndex]=col.ColumnName;     

foreach(DataRow row in table.Rows) 

rowIndex++; 
colIndex=0; 
foreach(DataColumn col in table.Columns) 

colIndex++; 
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString(); 


excel.Visible=true; }
运行报错:找不到类型或命名空间 名称Excel

解决方案 »

  1.   

    晕~在项目的解决方案上面点开。来一个引用。点右件。添加引用。
    选择COM 
    到都面找 Microsoft Excel 11.0 Object Library
    确实
      

  2.   

    StreamWriter Sw =new System.IO.StreamWriter("导出的excel文件名")foreach(DataColumn dc in this.DataTable1.Columns)
    {
      Sw.Write(dc.ColumName+"\t");  //输出列名,你可以改成中文名称
    }
    Sw.WriteLine();foreach(DataRow dr in this.DataTable1.Rows)
    {
      for(int i=0;i<this.DataTable1.Columns.Count;i++)
      {
         Sw.Write(dr[0].ToString()+"\t")
      }
      Sw.WriteLine();  
    }