导出到Excel时出错

时间:2017-07-24 12:52:25

标签: model-view-controller closedxml

我导出到excel控制器代码是

 [ValidateInput(false)]
    public ActionResult ExporttoExcel(Domain obj, int table)
    {
        DataSet ds = new DataSet();
        ds = Domain.GetAllTables();
        if (ds != null)
        {
            if (ds.Tables.Count > 0)
            {
                using (XLWorkbook wb = new XLWorkbook())
                {
                    var wsreport = wb.Worksheets.Add(ds.Tables[table], "Renewals");
                    wsreport.Tables.SingleOrDefault().ShowAutoFilter = false;

                    Response.ClearContent();
                    Response.Buffer = true;
                    Response.Charset = "";
                    Response.ContentType = "application/ms-excel";

                    if (table == 0)
                    {
                        Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "Renewals-Current Month"));
                    }
                    else if (table == 1)
                    {
                        Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "Renewals-Next Month"));
                    }

                    using (MemoryStream MyMemoryStream = new MemoryStream())
                    {
                        wb.SaveAs(MyMemoryStream);
                        MyMemoryStream.WriteTo(Response.OutputStream);
                        Response.Flush();                          
                    }
                    Response.End();
                }
            }
        }
        return View("ViewDomain");
    }

包含命名空间:

using System;
using System.Collections.Generic;
using System.Linq;
using dsmanager.Models;
using System.Web;
using System.Web.Mvc;
using dsmanager.DLL;
using System.Text;
using System.IO;
using System.Globalization;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Drawing;
using ClosedXML.Excel;

我收到以下错误enter image description here

如何解决此错误。缺少什么参考? 安装的所有块包都已更新,并且是最新版本。 如何加载程序集

1 个答案:

答案 0 :(得分:0)

您可能拥有DocumentFormat.OpenXml版本2.7或更高版本,这与ClosedXML v0.87.1不兼容。升级到ClosedXML v0.88或更高版本。