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; } }
宇宙无敌导出到Excel的最简单,最快速,最不易出问题的方法: 如果 Jet OLE DB 提供程序能够使用另外的数据源,请将该数据源中的数据导出到 Excel 工作簿中。可通过 Jet OLE DB 提供程序以这种方式使用的数据源包括:文本文件、Microsoft Access 数据库,当然也包括其他 Excel 工作簿。使用单个 INSERT INTO 命令,您可以将其他表/查询中的数据导出到您的工作簿中: INSERT INTO [Sheet1$] IN 'C:\Book1.xls' 'Excel 8.0;' SELECT * FROM MyTable"INSERT INTO 要求目标表(或工作表)已存在,并且数据已附加到目标表中。您还可以使用 SELECT..INTO 将您的表/查询导出到工作簿中: SELECT * INTO [Excel 8.0;Database=C:\Book1.xls].[Sheet1] FROM [MyTable]当您使用 SELECT..INTO 时,如果目标表或工作簿不存在,将为您创建。如果在发出 SELECT..INTO 命令之前表已存在,您将收到错误信息。
设置好EXCEL模板,赋值到单元格
/// <summary>
/// 将DataTable中的数据导出到指定的Excel文件中
/// </summary>
/// <param name="page">Web页面对象</param>
/// <param name="tab">包含被导出数据的DataTable对象</param>
/// <param name="FileName">Excel文件的名称</param>
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.ForeColor = Color.White;
dataGrid.HeaderStyle.BackColor = Color.FromName("#aaaadd");
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);
string filePath = page.Server.MapPath(" .")+"\\Files\\" +FileName;
if( !Directory.Exists(Path.GetDirectoryName(filePath)))
Directory.CreateDirectory(Path.GetDirectoryName(filePath)) ; System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close(); DownFile(httpResponse,FileName,filePath);
httpResponse.End();
}
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;
}
}
如果 Jet OLE DB 提供程序能够使用另外的数据源,请将该数据源中的数据导出到 Excel 工作簿中。可通过 Jet OLE DB 提供程序以这种方式使用的数据源包括:文本文件、Microsoft Access 数据库,当然也包括其他 Excel 工作簿。使用单个 INSERT INTO 命令,您可以将其他表/查询中的数据导出到您的工作簿中:
INSERT INTO [Sheet1$] IN 'C:\Book1.xls' 'Excel 8.0;' SELECT * FROM MyTable"INSERT INTO 要求目标表(或工作表)已存在,并且数据已附加到目标表中。您还可以使用 SELECT..INTO 将您的表/查询导出到工作簿中:
SELECT * INTO [Excel 8.0;Database=C:\Book1.xls].[Sheet1] FROM [MyTable]当您使用 SELECT..INTO 时,如果目标表或工作簿不存在,将为您创建。如果在发出 SELECT..INTO 命令之前表已存在,您将收到错误信息。