在浏览器中显示mvc导出excel而不是下载文件

时间:2014-07-04 06:30:58

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

我正在尝试以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 有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

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

  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

答案 1 :(得分:0)

尝试将Response.ContentType更改为

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