ASP.NET导出excel代码

public ActionResult Export()
        {
            string table = "<table border='1px solid black'><tr><td>编号</td><td>真实姓名</td><td>昵称</td><td>电话</td><td>数量</td><td>参与时间</td><td>状态</td><td>备注</td></tr>";
            table += "<tr><td>1</td><td>2</td><td>3</td><td>4<td>5</td><td>6</td><td>6</td><td>6</td></tr>";
            table += "</table>";
           string fileName = "游戏活动模板" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
            Response.ContentType = "application/force-download";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
            Response.Write("<head>");
            Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
            //Response.ContentType = "application/excel";
            //Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.Write("<!--[if gte mso 9]><xml>");
            Response.Write("<x:ExcelWorkbook>");
            Response.Write("<x:ExcelWorksheets>");
            Response.Write("<x:ExcelWorksheet>");
            Response.Write("<x:Name>Report Data</x:Name>");
            Response.Write("<x:WorksheetOptions>");
            Response.Write("<x:Print>");
            Response.Write("<x:ValidPrinterInfo/>");
            Response.Write("</x:Print>");
            Response.Write("</x:WorksheetOptions>");
            Response.Write("</x:ExcelWorksheet>");
            Response.Write("</x:ExcelWorksheets>");
            Response.Write("</x:ExcelWorkbook>");
            Response.Write("</xml>");
            Response.Write("<![endif]--> ");
            Response.Write(table);
            Response.End();
        }

猜你喜欢

转载自www.cnblogs.com/wenghan/p/12514457.html