asp.net - 错误 : The type or namespace name 'ExcelPackage' could not be found

标签 asp.net linq

我想在按下带有以下代码的按钮时生成 excel 文件,但我有 2 个错误。

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

        }
    }
}

最佳答案

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

using OfficeOpenXml;

// ...


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

query.CopyToDataTable(); 上的下一个异常是由CopyToDataTable引起的是 IEnumerable<DataRow> 的扩展方法但您的查询是 IEnumerable<anonymous type> .所以这根本行不通。你需要DataRows能够创建一个新的 DataTable从它。

如果你想将它与任何类型一起使用,你可以在我的其他答案中使用这里的方法:

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

关于asp.net - 错误 : The type or namespace name 'ExcelPackage' could not be found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13819962/

相关文章:

asp.net - 在列表中设置 asp 文本框的样式?

javascript - 用于阅读特定文本的书签

c# - 按数组元素排序 Linq 列表

c# - OrderBy 子集合特定类别

c# - LINQ - 为多个结果集构建 AND 与 OR 逻辑

c# - MEF CS0234 : The type or namespace name 'Composition' does not exist in the namespace 'System.ComponentModel' (are you missing an assembly reference? )

c# - 如何托管 Web 服务以便连接到同一网络的其他客户端可以访问

c# - C# 中的正则表达式

c# - Linq & String.ToLower() 奇怪的行为

c# - LINQ 查询中的左外连接