解决方案 »
- 控件事件激发有先后顺序吗?
- Diagnostics.ProcessStartInfo 调用EXE 怪事~~ 请达人解答~~~ 诡异呀
- gridview 分页
- 我的存储过程+游标的一段小程序结果有异常,我看不出来哪里的问题,帮帮忙高手、大侠们!~~~~
- 发生类型为System.StackOverflowException 的异常,产生原因是什么?怎么解决?
- 怎样才叫应用程序结束?
- GGJJ: 例外詳細資訊: System.Data.SqlClient.SqlException: 程序 'look6' 預期使用未提供的參數 '@radio'
- 163.com 的邮箱到了1。5G,大家觉得它需要用多大的硬盘才能够容纳这么多用户???
- 到对话框值?结贴率100%
- 哪儿有MSDN2003三张盘下载?
- 关于用模板导出word失败
- 为什么调试代码老是提示Server too Busy
1.服务器生成提供下载,写后端代码
2.客户端允许ActiveX下载,写JS代码
try it:
http://www.cnblogs.com/insus/archive/2009/02/28/1400266.html
public override bool EnableEventValidation {
get { return false; }
set { base.EnableEventValidation = false; }
}public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}
public static void ExportCommon(string XlsTittle, System.Web.UI.WebControls.GridView gridView, int[] hideColumns, string fileExt)
{
HttpContext context = HttpContext.Current;
System.Web.UI.WebControls.Label TextTittle = new System.Web.UI.WebControls.Label();
System.Web.UI.WebControls.Label TextTimeFrame = new System.Web.UI.WebControls.Label();
System.Web.UI.WebControls.Label TextNowTime = new System.Web.UI.WebControls.Label(); TextTittle.Text = XlsTittle + "<br style='mso-data-placement:same-cell;'/>";
TextNowTime.Text = "导出时间:" + DateAndTime.Now + "<br style='mso-data-placement:same-cell;'/>";
TextTittle.ControlStyle.Font.Size = 15;
TextTittle.ControlStyle.Font.Name = "微软雅黑";
TextTittle.ControlStyle.ForeColor = Color.Black;
context.Response.ClearContent();
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
context.Response.AddHeader("content-disposition", string.Format("attachment; filename=Page{0}.{1}", gridView.PageIndex + 1, fileExt));
switch (fileExt.ToLower()) {
case "xls":
context.Response.ContentType = "application/excel";
break; // TODO: might not be correct. Was : Exit Select break;
case "doc":
context.Response.ContentType = "application/word";
break; // TODO: might not be correct. Was : Exit Select break;
} System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw); Page page = new Page();
HtmlForm form = new HtmlForm();
gridView.EnableViewState = false; page.EnableEventValidation = false; page.DesignerInitialize(); // 隐藏指定的列
if (hideColumns != null) {
foreach (int columnIndex in hideColumns) {
if (columnIndex < gridView.Columns.Count) {
gridView.Columns[columnIndex].Visible = false;
}
}
} // 如果HeaderRow里的控件是button的话,则把它替换成文本
foreach (TableCell tc in gridView.HeaderRow.Cells) {
// TableCell里的每个Control
foreach (Control c in tc.Controls) {
// 如果控件继承自接口IButtonControl
if (c.GetType().GetInterface("IButtonControl") != null && c.GetType().GetInterface("IButtonControl").Equals(typeof(IButtonControl))) {
// 如果该控件不是“导出Excel”按钮则把button转换成文本
tc.Controls.Clear();
tc.Text = ((IButtonControl)c).Text;
}
}
}
// 将服务器控件的内容输出到所提供的 System.Web.UI.HtmlTextWriter 对象中
// gridView.RenderControl(htw)
page.Controls.Add(TextTittle);
page.Controls.Add(TextNowTime);
page.Controls.Add(form);
//把form放到page内 form.Controls.Add(gridView);
//再把gridview放到form中 page.RenderControl(htw);
//输出page
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
context.Response.Write(sw.ToString());
context.Response.Flush();
context.Response.End();
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}