[已解决问题] 导出到excel的格式问题
提问时间: 2008-07-14 17:15
悬赏分:30 浏览:212 次
请问我在导出grid数据到excel,怎么把grid上面的01这种数据正常显示出来,现在显示的是1,还有怎么把隐藏的列不让它显示出来,
代码如下:
private void Export_Click(object sender, System.EventArgs e)
        {
             //清除客户端当前显示
            Response.Clear();
            //作为附件输出,filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文            件类型相符,可以为:.doc    .xls    .txt   .htm  
            Response.AddHeader("content-disposition",                     "attachment;filename=FileName.xls");  //显示标头
            //设置显示的字和内容要存的形式
            Response.Charset = "UTF-8";
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            this.btnQuery_Click(this, e);
           
            this.uwgResult.UltraWebGridControl.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
           
           
        }

提问者:Harry Lu - 初学一级
最佳答案
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"); //设置第列为文本格式
            }
        }
2008/7/14 17:23:40 回答者:吴畏


提问者对于答案的评价:我试过gridview的格式用gv_RowDataBound方法可以,不过用了style我这边还是不能正常显示;由于我这边使用的是ultrawebgrid,研究了下,不过还是不知道在什么事件里增加e.Row.Cells[1].Attributes.Add("class", "text"),纯粹没事研究下,所以算了,不过还是谢谢你们的回答。
其它回答(1)
格式的问题按楼上的方法 
不显示列的问题可以将要隐藏的列.Visiable = false;我在gridview及AspxGridView下可以成功,不知道你的可不可以用(你是用的UltraWebGridControl控件?)
1个月前   回答者:张荣华 - 小虾三级
评论
1个月前   张荣华 :
格式的问题按楼上的方法 
不显示列的问题可以将要隐藏的列.Visiable = false;我在gridview及AspxGridView下可以成功,不知道你的可不可以用(你是用的UltraWebGridControl控件?)
   您需要登录以后才能回答!
我的问题    我要提问


快到期问题

> 问题排行榜

有不合适内容,建议去除