【问题标题】:export excel in mvc displaying in the browser instead of downloading the file在浏览器中显示的mvc中导出excel而不是下载文件
【发布时间】:2014-08-25 10:08:35
【问题描述】:

我正在尝试以 excel 格式导出我的 webgrid 数据,但它在浏览器中写入内容而不是下载它。这是我的代码,我参考了许多示例,每个人都说同样的话,但没有一个有效。这是我的代码

var northwind = Database.Open("Northwind");
    var sql = "SELECT CustomerID, CompanyName, ContactName, Address, City, Country, Phone FROM Customers";
    var customers = northwind.Query(sql);

    var connString = string.Format(@"Provider=Microsoft.Jet.OleDb.4.0;
                                    Data Source={0}/{1};Extended Properties='Excel 8.0;HDR=Yes;'", 
                                    appData, newFileName);
    var provider = "System.Data.OleDb";

    using (var excel = Database.OpenConnectionString(connString, provider)){

        sql = @"INSERT INTO [Sheet1$] (CustomerID, CompanyName, ContactName, Address, City, Country, Phone) 
            VALUES (@0,@1,@2,@3,@4,@5,@6)";
        foreach(var customer in customers){
            excel.Execute(sql,  
                customer.CustomerID, 
                customer.CompanyName, 
                customer.ContactName, 
                customer.Address, 
                customer.City, 
                customer.Country, 
                customer.Phone);
        }
    }

    Response.AddHeader("Content-disposition", "attachment; filename=report.xls");
    Response.ContentType = "application/octet-stream";
    Response.TransmitFile(newFile);
    Response.Flush();
    File.Delete(newFile);
    Response.End(); 

我的看法是

    <script type="text/javascript">
        $(function () {
            $('#excel').appendTo($('tfoot tr td')).on('hover', function () {
                $(this).css('cursor', 'pointer');
            });
            $('#excel').on('click', function () {
                $('<iframe src="/GenerateExcel"></iframe>').appendTo('body').hide();
            });
        });
    </script>


@{
    Page.Title = "Export To Excel";
    var db = Database.Open("Northwind");
    var query = "SELECT CustomerID, CompanyName, ContactName, Address, City, Country, Phone FROM Customers";
    var data = db.Query(query);
    var grid = new WebGrid(data, ajaxUpdateContainerId: "grid");
}

    <h1>Export to Excel</h1>
    <div id="gridContainer">
        <div id="grid">
            @grid.GetHtml(    
                tableStyle : "table",
                alternatingRowStyle : "alternate",
                headerStyle : "header",
                columns: grid.Columns(
                    grid.Column("CustomerID", "ID"),
                    grid.Column("CompanyName", "Company Name"),
                    grid.Column("ContactName", "Contact Name"),
                    grid.Column("Address"),
                    grid.Column("City"),
                    grid.Column("Country"),
                    grid.Column("Phone")
                )
            )
            <img src="/images/excel-icon.png" id="excel" alt="Export to Excel" title="Export to Excel" />
        </div>
    </div>

上面的代码我在下面的站点中找到并尝试重现相同的内容,但在浏览器中写入内容而不是下载。这是来源

http://www.mikesdotnetting.com/Article/207/Exporting-The-Razor-WebGrid-To-Excel-Using-OleDb 有什么帮助吗?

【问题讨论】:

  • 您是否在配置或 IIS 中定义了名称为“application/octet-stream”的任何 MIME 类型,如果是,则删除。

标签: c# jquery asp.net-mvc asp.net-mvc-4


【解决方案1】:

我为此目的使用以下代码。

  Response.Clear();
  Response.AddHeader("content-disposition", "attachment;filename=download.xlsx");
  Response.Charset = "";
  Response.Cache.SetCacheability(HttpCacheability.NoCache);
  Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  content.CopyTo(context.Response.OutputStream); //content is the file stream
  Response.End();

所以不要使用 TransmitFile 方法,而是尝试使用 Response.OutputStream

【讨论】:

    【解决方案2】:

    尝试将您的Response.ContentType 更改为

    Response.ContentType = "application/ms-excel";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多