datagrid可以导出到excel,但是存在两个问题:
1、每次提示保存excel文件,接着提示保存那个页面文件.aspx,
2、我的datagrid中有一个编号列,导入到excel中时编号前面的0自动给我去掉了。
不知道有没有人遇到过这样的问题,又怎么解决?
1、每次提示保存excel文件,接着提示保存那个页面文件.aspx,
2、我的datagrid中有一个编号列,导入到excel中时编号前面的0自动给我去掉了。
不知道有没有人遇到过这样的问题,又怎么解决?
{
if(TextBox1.Text=="")
{
Response.Write("<script language=javascript>");
Response.Write("window.alert(\"请输入文件名\")");
Response.Write("</script>");
}
else
{
Response.Clear();
Response.Buffer=true;
Response.Charset="gb2312";//设置了类型为中文防止乱码的出现
Response.AppendHeader("Content-Disposition","attachment;filename="+TextBox1.Text+".xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为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);
this.LogGrid.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
}
} private void LogGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[0].Attributes.Add("style","vnd.ms-excel.numberformat:@");
e.Item.Cells[3].Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");
}
}