zoukankan      html  css  js  c++  java
  • asp.net 2.0 导出DataTable到Excel中

    除调用excel类,给每个单元格赋值外还可以使用另一种简单的方法

    protected void EduceExcel(DataTable dt, string FileName)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;//设置缓冲输出
            HttpContext.Current.Response.Charset = "GB2312";//设置输出流的HTTP字符集

            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.ContentType = "application/ms-";
            //_page.EnableViewState = false;//是否保持视图状态
            HttpContext.Current.Response.Write(HTML(dt));
            HttpContext.Current.Response.End();
        }

    private string HTML(DataTable dt)
        {

            StringBuilder strHtml = new StringBuilder();
            int I = dt.Columns.Count;
            strHtml.Append("<table>");
            strHtml.Append("<tr>");
            for (int j = 0; j < I; j++)
            {
                strHtml.Append("<td>" + dt.Columns[j].ColumnName + "</td>");
            }
            strHtml.Append("</tr>");
            //int ii = 1;
            foreach (DataRow dr in dt.Rows)
            {
                strHtml.Append("<tr>");
                //int I = dr.Table.Columns.Count;
                for (int i = 0; i < I; i++)
                {
                    strHtml.Append(" <td>" + dr[i].ToString() + "</td>");
                }
                strHtml.Append("</tr>");
            }
            strHtml.Append("</table>");

            return strHtml.ToString();
        }

  • 相关阅读:
    电脑网络连接正常,无法连接浏览器,无法上网
    幂等性
    jvm问题解决
    Mybatis设计模式
    单进程单线程的Redis如何能够高并发
    分布式锁(Zookeeper)
    MyBatis 的 DAO 接口跟 XML 文件里面的 SQL 是如何建立关系的
    ArrayList、LinkedList、Vector、HashSet、Treeset、HashMap、TreeMap的区别和适用场景
    时间函数-线程安全
    socket
  • 原文地址:https://www.cnblogs.com/hdjjun/p/1323035.html
Copyright © 2011-2022 走看看