2012-12-11 206 views
3

我喜歡使用下面的代碼按下按鈕時生成excel文件,但我有2個錯誤。錯誤:無法找到類型或名稱空間名稱'ExcelPackage'

Error 1: The type or namespace name 'ExcelPackage' could not be found 

Error 2: var ds = query.CopyToDataTable(); 

的按鈕單擊事件代碼如下圖所示:

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Data.OleDb; 
using Microsoft.Office.Interop.Excel; 
using Microsoft.Office.Core; 
using System.Reflection; 
using System.ComponentModel; 
using System.Collections.Generic; 
using System.Data.Linq; 
using System.Data.SqlClient; 
using System.Drawing; 
using System.Text; 


public partial class Default2 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     var p = new ExcelPackage(); 
     var sheetName = "MyWorksheet"; 
     ExcelWorksheet ws = p.Workbook.Worksheets.Add(sheetName); 


     ws.Cells.Style.Font.Size = 11; //Default font size for whole sheet 
     ws.Cells.Style.Font.Name = "Calibri"; //Default Font name for whole sheet 

     LinqBooksDataSet dataSet = new LinqBooksDataSet(); 
     FillDataSetUsingLinqToSql2(dataSet); 

     // Query the DataTables 
     var query = 
     from publisher in dataSet.Publisher 
     join book in dataSet.Book 
     on publisher.ID equals book.Publisher 
     select new 
     { 
    Publisher = publisher.Name, 
    Book = book.Title 
}; 

     var ds = query.CopyToDataTable(); 

     if (ds.Tables.Count > 0 && ds.Rows.Count > 0) 
     { 
      ws.Cells["A1"].LoadFromDataTable(ds, true); 
      Response.BinaryWrite(p.GetAsByteArray()); 
      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
      Response.AddHeader("content-disposition", "attachment; filename=" + sheetName + ".xlsx"); 
     } 

    } 
    private static void FillDataSetUsingLinqToSql2(LinqBooksDataSet dataSet) 
    { 
     // Prepare the LINQ to SQL DataContext 
     var linqBooks = new LinqBooksDataContext(); 

     // Query the Publisher table 
     var publisherQuery = 
      from publisher in linqBooks.Publishers 
      select new { publisher.ID, publisher.Name }; 
     // Query the Book table 
     var bookQuery = 
      from book in linqBooks.Books 
      where book.PubDate.Value.Year > 1950 
      select new 
      { 
       book.ID, 
       book.Title, 
       book.Subject 


      }; 

     // Execute the queries and load the data into the DataSet 
     foreach (var publisher in publisherQuery) 
     { 
      dataSet.Publisher.AddPublisherRow(
      publisher.ID, publisher.Name, null, null); 
     } 
     foreach (var book in bookQuery) 
     { 
      dataSet.Book.AddBookRow(book.ID, book.Title, book.Subject); 

     } 
    } 
} 

回答

5

我假設你正在使用EPPLus。但是,您需要添加using OfficeOpenXml;

using OfficeOpenXml; 

// ... 


using (var pck = new OfficeOpenXml.ExcelPackage()) 
{ 
    using (var stream = File.OpenRead(path)) 
    { 
     pck.Load(stream); 
    } 
    // ... 
} 

query.CopyToDataTable();下異常的事實,CopyToDataTableIEnumerable<DataRow>一個擴展方法,但你的查詢是一個IEnumerable<anonymous type>引起的。所以這根本不適用於這種方式。您需要DataRows才能夠從中創建新的DataTable

如果你想與任何類型的使用它,你可以在我的其他的答案在這裏使用的方法:

Why I can't use .CopyToDataTable in LINQ query?

+0

感謝,是的,我使用EPPLus。什麼是var ds = query.CopyToDataTable()的問題; – masoud

+0

@masoud:編輯我的答案。 –

+0

錯誤4無法找到類型或命名空間名稱'OfficeOpenXml'...我需要做什麼? –

相關問題