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

推荐订阅源

A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
N
News and Events Feed by Topic
C
Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
Scott Helme
Scott Helme
P
Palo Alto Networks Blog
S
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tor Project blog
量子位
G
Google Developers Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
NISL@THU
NISL@THU
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
AWS News Blog
AWS News Blog
爱范儿
爱范儿
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
Security Archives - TechRepublic
Security Archives - TechRepublic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
Cloudbric
Cloudbric
aimingoo的专栏
aimingoo的专栏
L
Lohrmann on Cybersecurity
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hacker News: Ask HN
Hacker News: Ask HN
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
S
Security @ Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
P
Proofpoint News Feed
V
V2EX
Martin Fowler
Martin Fowler
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Cloudflare Blog
SecWiki News
SecWiki News
罗磊的独立博客
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
The Last Watchdog
The Last Watchdog

博客园 - 范燕军

UML学习之用例图 jquery分页控件 对象,XML序列化 (转)C#中调用Windows API时的数据类型对应关系 asp.net页生命周期 SVN服务端的配置 [ 转] .NET相关的最好东西--全球最新评价 asp.net执行.sql文件 [转] 用ASP.NET对IIS中的虚拟目录进行操作 [转] 使用JavaScript检测浏览器的相关特性 [转] 路径,文件,目录,I/O常见操作汇总(三) [转] 路径,文件,目录,I/O常见操作汇总(二) [转] 路径,文件,目录,I/O常见操作汇总(一) [转]NET用户控件文件上传,并给图片文件加水印(增加文字水印文字设置) [转] Javascript访问Cookie的四个常用方法 [转] Javascript访问Cookie的四个常用方法 [转]常用的JavaScript验证正则表达式 [转]ajax加载内容示例 [转]14 个经典的javascript代码
[转]C#实现MS SQL数据导出到Excel
范燕军 · 2008-03-31 · via 博客园 - 范燕军

将MS SQL数据导出到Excel:

 public static void Export(System.Web.UI.Page page, System.Data.DataTable tab, string FileName)
    
{
        System.Web.HttpResponse httpResponse 
= page.Response;
        System.Web.UI.WebControls.DataGrid dataGrid 
= new System.Web.UI.WebControls.DataGrid();
        dataGrid.DataSource 
= tab.DefaultView;
        dataGrid.AllowPaging 
= false;
        dataGrid.HeaderStyle.BackColor 
= System.Drawing.Color.Green;
        dataGrid.HeaderStyle.HorizontalAlign 
= HorizontalAlign.Center;
        dataGrid.HeaderStyle.Font.Bold 
= true;
        dataGrid.DataBind();
        httpResponse.AppendHeader(
"Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)); //filename=*.xls;
        httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        httpResponse.ContentType 
= "application / ms - excel";
        System.IO.StringWriter tw 
= new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw 
= new System.Web.UI.HtmlTextWriter(tw);
        dataGrid.RenderControl(hw);
        dataGrid.Visible 
= false;
        
string filePath = page.Server.MapPath(".."+ "\\\\Files\\\\" + FileName;
        System.IO.StreamWriter sw 
= System.IO.File.CreateText(filePath);
        sw.Write(tw.ToString());
        sw.Close();
        DownFile(httpResponse, FileName, filePath);
        httpResponse.End();
    }

    
/// <summary>
    
/// 
    
/// </summary>
    
/// <param name="Response"></param>
    
/// <param name="fileName"></param>
    
/// <param name="fullPath"></param>
    
/// <returns></returns>

    private static bool DownFile(System.Web.HttpResponse Response, string fileName, string fullPath)
    
{
        
try
        
{
            Response.ContentType 
= "application / octet - stream";
            Response.AppendHeader(
"Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=GB2312");
            System.IO.FileStream fs 
= System.IO.File.OpenRead(fullPath);
            
long fLen = fs.Length;
            
int size = 102400;//每100K同时下载数据 
            byte[] readData = new byte[size];//指定缓冲区的大小 
            if (size > fLen) size = Convert.ToInt32(fLen);
            
long fPos = 0;
            
bool isEnd = false;
            
while (!isEnd)
            
{
                
if ((fPos + size) > fLen)
                
{
                    size 
= Convert.ToInt32(fLen - fPos);
                    readData 
= new byte[size];
                    isEnd 
= true;
                }

                fs.Read(readData, 
0, size);//读入一个压缩块 
                Response.BinaryWrite(readData);
                fPos 
+= size;
            }

            fs.Close();
            System.IO.File.Delete(fullPath);
            
return true;
        }

        
catch
        
{
            
return false;
        }

    }