从Web页面把SQL导出Excel,如何既保存在服务器端,又能自动导出到客户端。
2011-03-09 07:44
489 查看
1、数据表Temp 中关键要建立Id字段并设置为标识主键。
2、引用为
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = Connect.BindTable(" SELECT * FROM Temp Order by Id Desc");
Export(this, dt, this.tbValidTime.Text + '-' + this.tbEnd.Text + "在线统计报表.xls");
}
3、定义
public static void Export(System.Web.UI.Page page, System.Data.DataTable tab, string FileName) //Book1.xls
{
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);
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();
}
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;
}
}
http://blog.csdn.net/anya/archive/2009/03/28/4032571.aspx
2、引用为
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = Connect.BindTable(" SELECT * FROM Temp Order by Id Desc");
Export(this, dt, this.tbValidTime.Text + '-' + this.tbEnd.Text + "在线统计报表.xls");
}
3、定义
public static void Export(System.Web.UI.Page page, System.Data.DataTable tab, string FileName) //Book1.xls
{
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);
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();
}
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;
}
}
http://blog.csdn.net/anya/archive/2009/03/28/4032571.aspx
相关文章推荐
- 从Web页面把SQL导出Excel,如何既保存在服务器端,又能自动导出到客户端。
- 如何将服务器端文件 自动下载并保存到客户端IE临时文件夹 或者指定文件夹
- 向Excel中导出长位数字时Excel会自动将其转化为类似于1.34E+13的格式,如何将其保存为文本?(如身份证)
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- 导出到excel,如果不保存,会自动关闭页面的问题
- DevExpress asp.net 导出Excel 自动开启迅雷问题,默认保存为aspx页面
- 导出datatable到excel然后让web客户端下载到本地
- JXL导出EXCEL:如何自动调整列宽
- delphi客户端调服务器端的java webservice如何在参数中传对象? 转
- java使用poi或者jxl实现excel导出之后如何弹出保存提示框
- WEB页面导出为EXCEL文档的方法(ZT)
- 如何将WEB页面中的数据保存为一个word文件
- javascript 将页面上的Table导出保存为Excel (无格式)
- ASP.NET如何导出excel和txt并可以个性化修改表头添加统计行等,并传回客户端下载
- 关于webview如何自动登录保存登录信息
- 如何在Web页面中绑定服务器端变量
- 关于常用的sql语句及EAS客户端如何导出insert语句
- web页面导出excel数据格式问题
- JavaScript将Web页面内容导出到Word及Excel的方法
- Web页面的数据导出excel时的格式问题(转)