asp.net gridview数据导出到excel?
已经将gridview的表的格式(表头的字段)导入到了excel,但是表中的数据并没有导入到excel是怎么回事?望高手指教,多谢!代码:privatevoidToExc...
已经将gridview的表的格式(表头的字段)导入到了excel,但是表中的数据并没有导入到excel是怎么回事?望高手指教,多谢!
代码:
private void ToExcel(string FileName)
{
GridView1.Columns[0].Visible = false;
GridView1.Columns[17].Visible = false;
//Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType ="application /ms-excel";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//GridView1.DataSource = dt;
//GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(tw.ToString());
Response.Flush();
Response.End();
}
protected void Button2_Click1(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString();
ToExcel(filename+"设备信息表.xls");
} 展开
代码:
private void ToExcel(string FileName)
{
GridView1.Columns[0].Visible = false;
GridView1.Columns[17].Visible = false;
//Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType ="application /ms-excel";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//GridView1.DataSource = dt;
//GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(tw.ToString());
Response.Flush();
Response.End();
}
protected void Button2_Click1(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString();
ToExcel(filename+"设备信息表.xls");
} 展开
展开全部
参考一下我做的,可以直接复制
protected void Button1_Click(object sender, EventArgs e)
{
ExcelOut(this.GridView1);
}
public void ExcelOut(GridView gv)
{
if (gv.Rows.Count > 0)
{
Response.Clear();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("_yyyyMMdd_HHmmss") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
}
else
{
Response.Write("没有数据");
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
protected void Button1_Click(object sender, EventArgs e)
{
ExcelOut(this.GridView1);
}
public void ExcelOut(GridView gv)
{
if (gv.Rows.Count > 0)
{
Response.Clear();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("_yyyyMMdd_HHmmss") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
}
else
{
Response.Write("没有数据");
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
展开全部
/// <summary>
/// 在 Ajax 中导出GridView为Excel,先将 导出动作 按钮的的ID 加入到 UpdatePanel.Triggers属性中,并重写 页面的 VerifyRenderingInServerForm 方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnExport_Click(object sender, EventArgs e)
{
BtnQuery_Click(sender, e);//查询数据并绑定到GridView
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
GridView1.RenderControl(new HtmlTextWriter(sw));
Response.Clear();
string FileName = HttpUtility.UrlEncode("资金综合报表"+DateTime.Now.ToString("yyyy-MM-dd"), Encoding.UTF8) + ".xls";
Response.ContentEncoding = Encoding.UTF8;
Response.HeaderEncoding = Encoding.UTF8;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
Response.Write(sw.ToString());
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
我可以帮助你,你先设置我最佳答案后,我百度Hii教你。你的串号我已经记下,采纳后我会帮你制作
/// 在 Ajax 中导出GridView为Excel,先将 导出动作 按钮的的ID 加入到 UpdatePanel.Triggers属性中,并重写 页面的 VerifyRenderingInServerForm 方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnExport_Click(object sender, EventArgs e)
{
BtnQuery_Click(sender, e);//查询数据并绑定到GridView
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
GridView1.RenderControl(new HtmlTextWriter(sw));
Response.Clear();
string FileName = HttpUtility.UrlEncode("资金综合报表"+DateTime.Now.ToString("yyyy-MM-dd"), Encoding.UTF8) + ".xls";
Response.ContentEncoding = Encoding.UTF8;
Response.HeaderEncoding = Encoding.UTF8;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
Response.Write(sw.ToString());
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
我可以帮助你,你先设置我最佳答案后,我百度Hii教你。你的串号我已经记下,采纳后我会帮你制作
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |