c# asp.net中将datagrid中的数据导入excel问题?我按下面的代码,导出的文件不是.xls格式,而是.aspx文件
privatevoidbtout_Click(objectsender,System.EventArgse){if(dgxd.Items.Count==0){Respon...
private void btout_Click(object sender, System.EventArgs e)
{
if(dgxd.Items.Count==0)
{
Response.Write("<script>alert('对不起,你没有查询到任何记录,不能导出数据')</script>");
}
else
{
Response.Clear();
Response.Buffer=true;
Response.Charset="GB2312";
Response.AppendHeader("content-disponsition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType="application/excel";
this.EnableViewState=false;
System.Globalization.CultureInfo myCItrad=new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter=new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter=new System.Web.UI.HtmlTextWriter(oStringWriter);
dgxd.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
}
我点击导出按钮后,导出的文件不是FileName.xls格式,而是FileName.aspx格式的,这是为什么?还有就是我想把datagrid中的编辑列在导出后不显示,代码应该怎样写?编辑列在datagrid的第一列。
请各位大侠帮帮忙,谢谢了 展开
{
if(dgxd.Items.Count==0)
{
Response.Write("<script>alert('对不起,你没有查询到任何记录,不能导出数据')</script>");
}
else
{
Response.Clear();
Response.Buffer=true;
Response.Charset="GB2312";
Response.AppendHeader("content-disponsition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType="application/excel";
this.EnableViewState=false;
System.Globalization.CultureInfo myCItrad=new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter=new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter=new System.Web.UI.HtmlTextWriter(oStringWriter);
dgxd.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
}
我点击导出按钮后,导出的文件不是FileName.xls格式,而是FileName.aspx格式的,这是为什么?还有就是我想把datagrid中的编辑列在导出后不显示,代码应该怎样写?编辑列在datagrid的第一列。
请各位大侠帮帮忙,谢谢了 展开
1个回答
展开全部
没有输出文件名称:Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8) + ".xls\"");
追问
还是不行呢。点击导出按钮后,出现的对话框是这样的:
追答
#region ====打印====
//打印必须的重载方法
public override void VerifyRenderingInServerForm(Control control) { }
//打印方法(也就是保存为Excel的方法)
private void GridViewPrint(GridView GV)
{
Response.Buffer = true;
//Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8) + ".xls\"");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.grdProjectInfo.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
///
/// 打印
///
///
///
protected void btnPrint_Click(object sender, EventArgs e)
{
CDataBase objDB = new CDataBase();
//取消分页并填充
grdProjectInfo.AllowPaging = false;
FillDataSource(objDB);
grdProjectInfo.DataBind();
SetXH(objDB);
GridViewPrint(grdProjectInfo);
//重新分页再填充
grdProjectInfo.AllowPaging = true;
FillDataSource(objDB);
grdProjectInfo.DataBind();
SetXH(objDB);
if (objDB != null)
{
objDB.Close();
}
}
#endregion
这是导出gridview的,你可以参照下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询