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

推荐订阅源

S
Secure Thoughts
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Heimdal Security Blog
SecWiki News
SecWiki News
H
Hacker News: Front Page
N
News | PayPal Newsroom
L
LINUX DO - 最新话题
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
AI
AI
C
Cybersecurity and Infrastructure Security Agency CISA
Scott Helme
Scott Helme
PCI Perspectives
PCI Perspectives
S
Securelist
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Cyberwarzone
Cyberwarzone
A
Arctic Wolf
Forbes - Security
Forbes - Security
T
Tor Project blog
Spread Privacy
Spread Privacy
WordPress大学
WordPress大学
I
Intezer
Martin Fowler
Martin Fowler
Help Net Security
Help Net Security
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
博客园 - 司徒正美
W
WeLiveSecurity
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
P
Palo Alto Networks Blog
Google DeepMind News
Google DeepMind News
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
V
Vulnerabilities – Threatpost
Jina AI
Jina AI
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
Simon Willison's Weblog
Simon Willison's Weblog
Project Zero
Project Zero
T
Threatpost
P
Privacy International News Feed
人人都是产品经理
人人都是产品经理
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research

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