如题,请给出代码,再次感谢

解决方案 »

  1.   

    System.Web.UI.WebControls.DataGrid dataGrid=new System.Web.UI.WebControls.DataGrid();
    dataGrid.DataSource=dt.DefaultView;
    dataGrid.DataBind();Response.ContentType ="application/vnd.ms-excel";
    Response.ContentEncoding=System.Text.Encoding.GetEncoding("BIG5");
    Response.AppendHeader("Content-Disposition","attachment;filename="+p_DecDocName);
    StringWriter  tw = new StringWriter() ;
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    dataGrid.RenderControl(hw);  
    string filePath = Page.Server.MapPath("..")+"\\MatCode\\FilePack\\" +p_DecDocName;
    StreamWriter sw = File.CreateText(filePath);
    sw.Write(tw.ToString());
    sw.Close();
      

  2.   

    if(this.dgData.Items.Count==0)
    {
    RegisterStartupScript("Error","<script>alert('对不起,你没有查询到任何记录,不能导出数据')</script>");
    }
    else
    {
    Response.Clear(); 
    Response.Buffer= true;
    Response.Charset="GB2312";
    Response.AppendHeader("Content-Disposition","attachment;filename=FileName.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.dgData.RenderControl(oHtmlTextWriter);
    Response.Write(oStringWriter.ToString());
    Response.End(); 
    }我觉得加标题楼主可以在导出后手动加一下,好像导入的时候不可以吧
      

  3.   

    如果本页的DataGrid有分页设置,导出的时候好像只有当前页的记录。这个怎么处理?如果再初始化一个DataGrid,能不能套用页面上的DataGrid的格式?让新定义的DataGrid不分页。不知道有没有套用的方法。如果只是传个页面上的DataGrid引用,设置成不分页,那么本页上的DataGrid也不能分页了。怎么办?