首页 新闻 会员 周边 捐助

导出到excel的格式问题

0
悬赏园豆:30 [已解决问题] 解决于 2008-07-15 13:34
请问我在导出grid数据到excel,怎么把grid上面的01这种数据正常显示出来,现在显示的是1,还有怎么把隐藏的列不让它显示出来,<br>代码如下:<br>private void Export_Click(object sender, System.EventArgs e)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//清除客户端当前显示<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Response.Clear();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //作为附件输出,filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 件类型相符,可以为:.doc    .xls    .txt   .htm  <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Response.AddHeader("content-disposition", &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "attachment;filename=FileName.xls");&nbsp; //显示标头<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //设置显示的字和内容要存的形式<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Response.Charset = "UTF-8";<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Response.ContentType = "application/vnd.xls";<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.IO.StringWriter stringWrite = new System.IO.StringWriter();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.btnQuery_Click(this, e);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.uwgResult.UltraWebGridControl.RenderControl(htmlWrite);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Response.Write(stringWrite.ToString());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Response.End();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
Harry Lu的主页 Harry Lu | 初学一级 | 园豆:10
提问于:2008-07-14 17:15
< >
分享
最佳答案
0
Response.Buffer = true; Response.ClearContent(); ////指定http名称和值 Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode("StudentScores", System.Text.Encoding.UTF8) + ".xls"); //指定文件类型 Response.ContentType = "application/excel"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); StringWriter sw = new StringWriter(); ExcelGridView.AllowPaging = false; this.ExcelGridView.DataSource = dtStudentScore; ExcelGridView.DataBind(); //HtmlTextWriter 输出流 HtmlTextWriter htw = new HtmlTextWriter(sw); ExcelGridView.RenderControl(htw); string style = @"<style> .text { mso-number-format:\@; } </style> "; //单元格式为文本格式 //sw写入到http输出流 Response.Output.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); ExcelGridView.Visible = false; 加上<style> .text { mso-number-format:\@; } </style>这段CSS应该就可以正常显示 也可以通过RowDataBound来将指定的单元格设置成文本格式 protected void gv_RowDataBound(object sender, GridViewRowEventArgs e),增加导出Excel功能 { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[1].Attributes.Add("class", "text"); //设置第列为文本格式 e.Row.Cells[2].Attributes.Add("class", "text"); //设置第列为文本格式 e.Row.Cells[6].Attributes.Add("class", "text"); //设置第列为文本格式 e.Row.Cells[7].Attributes.Add("class", "text"); //设置第列为文本格式 } }
吴畏 | 菜鸟二级 |园豆:426 | 2008-07-14 17:23
其他回答(1)
0
格式的问题按楼上的方法  不显示列的问题可以将要隐藏的列.Visiable = false;我在gridview及AspxGridView下可以成功,不知道你的可不可以用(你是用的UltraWebGridControl控件?)
张荣华 | 园豆:2020 (老鸟四级) | 2008-07-14 17:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册