zoukankan      html  css  js  c++  java
  • asp.net导出excel

    public static void ExportExcel(System.Data.DataTable dt, string filetype, string Title)
    {
    if (filetype == "ms-excel" && dt.Rows.Count>0)
    {
    HttpContext.Current.Response.Clear();
    string title = Title;
    HttpContext.Current.Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\"><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
    // HttpContext.Current.Response.Write("<head>");
    HttpContext.Current.Response.Write("<!--[if gte mso 9]><xml>");
    HttpContext.Current.Response.Write("<x:ExcelWorkbook>");
    HttpContext.Current.Response.Write("<x:ExcelWorksheets>");
    HttpContext.Current.Response.Write("<x:ExcelWorksheet>");
    HttpContext.Current.Response.Write("<x:Name>sheet1</x:Name>");
    HttpContext.Current.Response.Write("<x:WorksheetOptions>");
    HttpContext.Current.Response.Write("<x:Print>");
    HttpContext.Current.Response.Write("<x:ValidPrinterInfo/>");
    HttpContext.Current.Response.Write("</x:Print>");
    HttpContext.Current.Response.Write("</x:WorksheetOptions>");
    HttpContext.Current.Response.Write("</x:ExcelWorksheet>");
    HttpContext.Current.Response.Write("</x:ExcelWorksheets>");
    HttpContext.Current.Response.Write("</x:ExcelWorkbook>");
    HttpContext.Current.Response.Write("</xml>");
    HttpContext.Current.Response.Write("<![endif]--> ");
    HttpContext.Current.Response.Write("</head>");
    HttpContext.Current.Response.Write("<body>");
    //导出表格网格线
    HttpContext.Current.Response.Write("<font style=\"color:black;font-weight:bold; font-size:12pt\">" + title + "</font>"); //设置标题
    HttpContext.Current.Response.Charset = "UTF-8";
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(title, System.Text.Encoding.UTF8) + ".xls");
    HttpContext.Current.Response.ContentType = "application/ms-excel";

    //page.EnableViewState = false;
    System.IO.StringWriter tw = new System.IO.StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    DataGrid dg1 = new DataGrid();
    dg1.HeaderStyle.Font.Bold = true;
    dg1.DataSource = dt;
    dg1.DataBind();
    dg1.RenderControl(hw);
    HttpContext.Current.Response.Write(tw.ToString());
    HttpContext.Current.Response.Write("</body>");
    HttpContext.Current.Response.Write("</html>");
    HttpContext.Current.Response.End();
    }
    }

  • 相关阅读:
    关于百度地图API的地图坐标转换问题
    SQL Server函数大全
    30条SQL查询优化原则
    .net升级到4.0之后,出现;System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798
    Java回调函数的理解
    .NET 开源GIS项目
    微信公众平台开发入门教程
    GIS理论(墨卡托投影、地理坐标系、地面分辨率、地图比例尺、Bing Maps Tile System)
    可视化之lattice、recharts、ggplot资源汇总
    回归小知识
  • 原文地址:https://www.cnblogs.com/guifang/p/2679916.html
Copyright © 2011-2022 走看看