MVC下用C#实现Excel导出
Aspx頁面腳本:
function exportxls() {
??????????? window.open("/Common/HomeExport?startdate=" + $("#hidStartTime").val(), "exportxls", null);
??????? }
C#代碼如下:
?? public class CommonController
??? {
??????? public void HomeExport()
??????? {
??????????? StringBuilder sHtml = new StringBuilder(string.Empty);
??????????? //下面這句解決中文亂碼
??????????? sHtml.Append("<meta http-equiv=’content-type’ content=’application/ms-excel; charset=utf-8’/>");
??????????? ……
??????????? //打印表頭
??????????? sHtml.Append("<table border=’1’ width=’100%’>");
??????????? //打印列名
??????????? sHtml.Append("<tr height=’20’ align=’center’style='background-color:yellow'><td>No. </td><td>Project</td><td>Content</td>tr>");
??????????? //循環讀取List集合
??????????? ……
??????????? //打印表尾
??????????? sHtml.Append("</table>");
??????????? string filename = "Report";
??????????? //調用輸出Excel表的方法
??????????? ExportToExcel("application/ms-excel", filename + ".xls", sHtml.ToString());
??????? }
?
??????? public void ExportToExcel(string FileType, string FileName, string ExcelContent)
??????? {
??????????? System.Web.HttpContext.Current.Response.Charset = "UTF-8";
??????????? System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
??????????? System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
??????????? System.Web.HttpContext.Current.Response.ContentType = FileType;
??????????? System.IO.StringWriter tw = new System.IO.StringWriter();
??????????? System.Web.HttpContext.Current.Response.Output.Write(ExcelContent.ToString());
??????????? System.Web.HttpContext.Current.Response.Flush();
??????????? System.Web.HttpContext.Current.Response.End();
??????? }
??? }
?
?
轉載于:https://www.cnblogs.com/yumianhu/p/3707541.html
總結
以上是生活随笔為你收集整理的MVC下用C#实现Excel导出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 缅甸币兑换人民币 缅甸币能兑换多少人民币
- 下一篇: 童年回忆(1)