惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
罗磊的独立博客
B
Blog
博客园_首页
A
About on SuperTechFans
有赞技术团队
有赞技术团队
V
V2EX
U
Unit 42
I
InfoQ
IT之家
IT之家
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
H
Help Net Security

博客园 - gotolovo

导出csv 正则学习电话匹配 logmanager des 加密 解密 - gotolovo c# 发送邮件 工作流学习过程-事务 工作流学习过程-状态机 蛋疼的viewstate 工作流学习过程-持久化服务 工作流学习过程-使用关联 工作流学习过程-本地服务之事件处理 工作流学习过程-本地服务之调用方法 工作流学习过程-验证活动 工作流学习过程-自定义活动 工作流学习过程-开篇 基本的几个排序算法 关于sql中时间的格式转换 数据行变列 请编程遍历页面上所有TextBox控件并给它赋值为空
导出excel
gotolovo · 2012-04-13 · via 博客园 - gotolovo
public static class Excel
    {
        /// <summary>
        /// 将dataset里的数据导出为Excel.
        /// </summary>
        /// <param name="fileName">导出时保存的文件名.</param>
        /// <param name="page">页对象.</param>
        /// <param name="dataset">数据集.</param>
        public static void ExportExcel(string fileName, Page page, DataSet dataset)
        {
            try
            {
                page.Response.Clear();
                page.Response.Buffer = false;
                page.Response.Charset = "GB2312";
                page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
                page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
                page.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
                // 指定返回的是一个不能被客户端读取的流,必须被下载 
                page.Response.ContentType = "application/ms-excel";
                page.EnableViewState = false;
                page.Response.BufferOutput = false;
                page.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
                page.Response.Write(ExportTable(dataset));
                page.Response.Flush();
                page.Response.End();
            }
            catch (System.Exception)
            {
            }
        }
        /// <summary>
        /// 将DataSet信息输出为string
        /// </summary>
        /// <param name="dataset">DataSet信息</param>
        /// <returns>string格式的信息</returns>
        public static string ExportTable(DataSet dataset)
        {
            if (dataset == null)
                return string.Empty;
            StringBuilder strBuilder = new StringBuilder();
            foreach (DataTable table in dataset.Tables)
            {
                strBuilder.AppendLine("<table border=1 cellspacing=0 cellpadding=5 rules=all >");
                //输出表头.
                strBuilder.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
                foreach (DataColumn headColumn in table.Columns)
                {
                    strBuilder.AppendLine("<td>" + headColumn.ColumnName + "</td>");
                }
                strBuilder.AppendLine("</tr>");
                //输出数据.
                foreach (DataRow row in table.Rows)
                {
                    strBuilder.AppendLine("<tr>");
                    foreach (DataColumn dataColumn in table.Columns)
                    {
                        strBuilder.AppendLine("<td style=\"mso-number-format:'\\@';\">");
                        strBuilder.AppendLine(row[dataColumn].ToString());
                        strBuilder.AppendLine("</td>");
                    }
                    strBuilder.AppendLine("</tr>");
                }
                strBuilder.AppendLine("</table>");
            }
            return strBuilder.ToString();
        }



        /// <summary>
        /// 将dataset里的数据导出为Excel.
        /// </summary>
        /// <param name="fileName">导出时保存的文件名.</param>
        /// <param name="page">页对象.</param>
        /// <param name="dataset">数据集.</param>
        public static void ExportExcel(string fileName, Page page, DataTable datatable)
        {
            try
            {
                page.Response.Clear();
                page.Response.Buffer = false;
                page.Response.Charset = "GB2312";
                page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
                page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文;
                page.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
                // 指定返回的是一个不能被客户端读取的流,必须被下载 
                page.Response.ContentType = "application/ms-excel";
                page.EnableViewState = false;
                page.Response.BufferOutput = false;
                page.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
                page.Response.Write(ExportTable(datatable));
                page.Response.Flush();
                page.Response.End();
            }
            catch (System.Exception)
            {
            }
        }
        /// <summary>
        /// 将DataSet信息输出为string
        /// </summary>
        /// <param name="dataset">DataSet信息</param>
        /// <returns>string格式的信息</returns>
        public static string ExportTable(DataTable dataTable)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.AppendLine("<table border=1 cellspacing=0 cellpadding=5 rules=all >");
            //输出表头.
            strBuilder.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
            foreach (DataColumn headColumn in dataTable.Columns)
            {
                strBuilder.AppendLine("<td>" + headColumn.ColumnName + "</td>");
            }
            strBuilder.AppendLine("</tr>");
            //输出数据.
            foreach (DataRow row in dataTable.Rows)
            {
                strBuilder.AppendLine("<tr>");
                foreach (DataColumn dataColumn in dataTable.Columns)
                {
                    strBuilder.AppendLine("<td style=\"mso-number-format:'\\@';\">");
                    strBuilder.AppendLine(row[dataColumn].ToString());
                    strBuilder.AppendLine("</td>");
                }
                strBuilder.AppendLine("</tr>");
            }
            strBuilder.AppendLine("</table>");

            return strBuilder.ToString();
        }
    }