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

推荐订阅源

Google DeepMind News
Google DeepMind News
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
雷峰网
雷峰网
Recent Announcements
Recent Announcements
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
博客园_首页
The Cloudflare Blog
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
S
SegmentFault 最新的问题
P
Proofpoint News Feed
Y
Y Combinator Blog
Jina AI
Jina AI
博客园 - 聂微东
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
F
Full Disclosure
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
T
Tailwind CSS Blog
J
Java Code Geeks
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 三生石上(FineUI控件)
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
宝玉的分享
宝玉的分享
IT之家
IT之家
Hacker News: Ask HN
Hacker News: Ask HN
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs

博客园 - 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();
        }
    }