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

推荐订阅源

IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
雷峰网
雷峰网
罗磊的独立博客
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog
人人都是产品经理
人人都是产品经理
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Help Net Security
P
Proofpoint News Feed
The Cloudflare Blog
D
Docker
大猫的无限游戏
大猫的无限游戏

博客园 - 范燕军

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;
        }

    }