错误:找不到类型或命名空间名称"ExcelPackage"

mas*_*oud 3 linq asp.net

当我按下以下代码的按钮时,我喜欢制作excel文件,但我有2个错误.

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

Error 2: var ds = query.CopyToDataTable();
Run Code Online (Sandbox Code Playgroud)

按钮点击事件的代码如下:

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);

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*ter 6

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

using OfficeOpenXml;

// ...


using (var pck = new OfficeOpenXml.ExcelPackage())
{
    using (var stream = File.OpenRead(path))
    {
        pck.Load(stream);
    }
    // ...
}
Run Code Online (Sandbox Code Playgroud)

下一个例外query.CopyToDataTable();是由于这CopyToDataTable是一个扩展方法,IEnumerable<DataRow>但您的查询是一个IEnumerable<anonymous type>.所以这不会以这种方式工作.您需要DataRows能够从中创建新的DataTable.

如果你想在任何类型中使用它,你可以在我的另一个答案中使用这里的方法:

为什么我不能在LINQ查询中使用.CopyToDataTable?

  • @ chinna_82:下载[**EPPlus**](https://epplus.codeplex.com/releases/view/79802)并在项目中添加对它的引用.然后添加`using`-directive,如我在答案中提到的:`使用OfficeOpenXml;` (3认同)