[Code] 导出excel2007

     private void ExportToExcel2007(DataGrid dtData)
        {
            
if (dtData != null)
            {
                
if (dtData.Items.Count == 0)
                {
                    Tools.Alert(
"当前无数据导出"this.Page);
                    
return;
                }
                dtData.AllowPaging 
= false;
                dtData.AutoGenerateColumns 
= false;

                System.Web.HttpResponse httpResponse 
= Page.Response;
                httpResponse.Clear();
                httpResponse.Buffer 
= true;
                httpResponse.Charset 
= "gb2312";
                
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssms"+ ".xls";                
                httpResponse.AppendHeader(
"Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                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);
                dtData.RenderControl(hw);
                
string directoryPath = Server.MapPath("~/"+ "TempExcel";
                
string filePath = Server.MapPath("~/"+ "TempExcel\\" + fileName;
                
if (!System.IO.Directory.Exists(directoryPath))
                {
                    System.IO.Directory.CreateDirectory(directoryPath);
                }                
                System.IO.StreamWriter sw 
= System.IO.File.CreateText(filePath);
                sw.Write(tw.ToString());
                sw.Close();
                DownFile(httpResponse, fileName, filePath);
                httpResponse.End();
            }
        }

        
private bool DownFile(System.Web.HttpResponse Response, string fileName, string fullPath)
        {
            System.IO.FileStream fs 
= System.IO.File.OpenRead(fullPath);
            
try
            {
                Response.ContentType 
= "application/octet-stream";

                Response.AppendHeader(
"Content-Disposition""attachment;filename=" +
                HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) 
+ ";charset=GB2312");                
                
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);//读入一个压缩块
                    if (readData.Length > 0)
                        Response.BinaryWrite(readData);
                    fPos 
+= size;
                }                          
                
return true;
            }
            
catch
            {
                
return false;
            }
            
finally
            {
                fs.Close();
                System.IO.File.Delete(fullPath);
            }
        }        
posted @ 2008-10-23 11:43  蛤蟆  阅读(205)  评论(0)    收藏  举报