• .NET页面导出Excel


     public static void CreateExcel(DataSet ds)
            {
                string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
                string colHeaders = "", ls_item = "";

                //定义表对象与行对象,同时用DataSet对其值进行初始化
                DataTable dt = ds.Tables[0];
                DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
                int i = 0;
                int cl = dt.Columns.Count;


                //取得数据表各列标题,各标题之间以 分割,最后一个列标题后加回车符
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))//最后一列,加
                    {
                        colHeaders += dt.Columns[i].Caption.ToString() + " ";
                    }
                    else
                    {
                        colHeaders += dt.Columns[i].Caption.ToString() + " ";
                    }

                }
                HttpContext.Current.Response.Write(colHeaders);
                //向HTTP输出流中写入取得的数据信息

                //逐行处理数据 
                foreach (DataRow row in myRow)
                {
                    //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据   
                    for (i = 0; i < cl; i++)
                    {
                        if (i == (cl - 1))//最后一列,加
                        {
                            ls_item += row[i].ToString() + " ";
                        }
                        else
                        {
                            ls_item += row[i].ToString() + " ";
                        }

                    }
                    HttpContext.Current.Response.Write(ls_item);
                    ls_item = "";

                }
                HttpContext.Current.Response.End();
            }

  • 相关阅读:
    HDOJ 1000 A + B Problem C++解法
    HDU之旅
    C++primer plus第六版课后编程题答案8.8
    C++primer plus第六版课后编程题答案8.6
    C++primer plus第六版课后编程题答案8.5
    C++primer plus第六版课后编程题答案8.3(正解)
    C语言练手自己编写学生成绩管理系统
    C_数据结构
    C---数组名作函数参数
    C---通过指针访问数组
  • 原文地址:https://www.cnblogs.com/liusuqi/p/3146387.html
Copyright © 2020-2023  润新知