asp.net – 将HTML表导出到Excel
发布时间:2020-12-16 06:56:55 所属栏目:asp.Net 来源:网络整理
导读:我在ASP.NET MVC View页面上有 HTML表.现在我必须将此表导出到Excel. (1)我使用局部视图(Inquiries.ascx)来显示数据库中的表数据(使用LINQ to Entity) (2)我也使用UITableFilter插件来过滤记录(例如:http://gregweber.info/projects/demo/flavorzoom.html)
我在ASP.NET MVC View页面上有
HTML表.现在我必须将此表导出到Excel.
(1)我使用局部视图(Inquiries.ascx)来显示数据库中的表数据(使用LINQ to Entity) (3)在任何时候,我都必须将可见记录过滤到Excel. 感谢您的回复. 谢谢 丽塔 这是我的观点: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Mvc.Master" Inherits="System.Web.Mvc.ViewPage" %> <asp:Content ID="Content2" ContentPlaceHolderID="cphHead" runat="server"> <script src="../../Scripts/jquery.tablesorter.js" type="text/javascript"></script> <script src="../../Scripts/jquery.uitablefilter.js" type="text/javascript"></script> <script type="text/javascript"> //Load Partial View $('#MyInquiries').load('/Home/Inquiries'); // To Apply Filter Expression using uiTableFilter plugin $("#searchName").keyup(function() { $.uiTableFilter($("#tblRefRequests"),this.value); $("#tblRefRequests").tablesorter({ widthFixed: true,widgets: ['zebra'] }); }); //Export the HTML table contents to Excel $('#export').click(function() { //Code goes here }); </script> </asp:Content> //Main Content <asp:Content ID="Content1" ContentPlaceHolderID="cphContent" runat="server"> <h2 class="pageName">View All Inquiries</h2> <input type="submit" value="Export to Excel" id="export" /> <div id='MyInquiries'></div> </asp:Content> 强类型部分视图用户控件(Inquiries.ascx)生成表: <table> <tr><td valign ="middle">Filter Expression: <%= Html.TextBox("searchName")%></td></tr> </table> <table id="tblRefRequests" > <thead> <tr> <th>Tx_ID</th> <th>TX Date</th> <th>Name</th> <th>Email Address </th> <th>Products</th> <th>Document Name</th> </tr> </thead> <tbody> <% foreach (var item in Model) { %> <tr> <td visible =false><%= item.RequestID %></td> <td><%= String.Format("{0:d}",item.RequestDate) %></td> <td><%= item.CustomerName %></td> <td><%= Html.Encode(item.Email) %></td> <td><%= item.ProductName %></td> <td><%= Html.Encode(item.DocDescription)%></td> </tr> <% } %> </tbody> </table> 这是我的控制器代码加载查询局部视图: [HttpGet] public PartialViewResult Inquiries() { var model = from i in myEntity.Inquiries where i.User_Id == 5 orderby i.TX_Id descending select new { RequestID = i.TX_Id,CustomerName = i.CustomerMaster.FirstName,RequestDate = i.RequestDate,Email = i.CustomerMaster.MS_Id,DocDescription = i.Document.Description,ProductName = i.Product.Name }; return PartialView(model); } 解决方法
尝试jQuery插件:
table2csv.使用参数,delivery:’value’,将csv作为字符串返回.
这是一个实现: >向页面添加常规html输入按钮和.NET HiddenField . // javascript function Export() { $('#yourHiddenFieldId').val() = $('#yourTable').table2CSV({delivery:'value'}); __doPostBack('#yourExportBtnId',''); } // c# if(Page.IsPostBack) { if(!String.IsNullOrEmpty(Request.Form[yourHiddenField.UniqueId])) { Response.Clear(); Response.ContentType = "text/csv"; Response.AddHeader("Content-Disposition","attachment; filename=TheReport.csv"); Response.Flush(); Response.Write(Request.Form[yourHiddenField.UniqueID]); Response.End(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 如何LoadControl一个使用VaryByControl OutputC
- asp.net – 自定义动态数据中的数据注释
- 动态数据 – ASP.NET动态数据向页面添加其他过滤条件
- 从ASP.NET连接到远程MongoDB实例
- asp.net – 如何防止低效的SQL查询减慢数据库服务器的速度
- asp.net-mvc – 在ASP.NET MVC应用程序中托管WCF服务?
- asp.net – 没有参数的WebResource.axd请求 – 这是一个无效
- asp.net-mvc-routing – ASP.NET Core 1.0中的属性路由
- ASP.NET Web API,Web服务发现和客户端创建
- 在ASP.Net MVC中禁用会话状态每请求
推荐文章
站长推荐
- asp.net核心 – ViewData是ASP.NET 5(核心)的Vie
- asp.net-mvc-5 – 在GenerateUserIdentityAsync方
- asp.net mvc 3区域和url路由配置
- asp.net-mvc-4 – 在常规MVC项目中使用breeze而不
- asp.net-mvc – 所选值不适用于SelectList
- ASP.NET会话状态InProc
- asp.net-mvc – 将查询字符串参数传递给.net mvc
- asp.net-mvc-4 – 页脚的Kendo Grid Aggregation
- com – asp经典中的server.createObject和create
- asp.net-mvc – 在部分视图中传递参数 – MVC3 /
热点阅读