最近在做一B/S的系统,其中有一部分要求客户端能够获取服务器数据库上的数据并保存在Excel文件中。具体就是读取数据库服务器上的表,然后导入Excel文件并能控制列宽~谁给个代码啊~~或者发个简单的可运行的demo给我~我的mail:[email protected],谢谢啦!!!

解决方案 »

  1.   

    我写过的一个例子,可能需要你修改一下protected void btn_excel_Click(object sender, EventArgs e)
        {
            Response.Clear();
            string excelname = "96447";
            Response.AddHeader("content-disposition", "attachment;filename="+excelname+".xls");
            
            Response.Charset = "gb2312";
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GV_Daily.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
            GV_Daily.AllowPaging = true;
            GV_BindData();        
        }
    下面这个自定义函数必须写:
        public override void VerifyRenderingInServerForm(Control control)
        {
            // Confirms that an HtmlForm control is rendered for
        }
      

  2.   

    在要保存的ASPX页面第一行加这一段试试 
    <% 
    Response.Buffer= true; 
    Response.Charset="GBK"; 
    Response.AppendHeader("Content-Disposition","attachment;filename=tablefile.xls"); 
    Response.ContentEncoding=System.Text.Encoding.GetEncoding("GBK"); 
    Response.ContentType = "application/ms-excel"; 
    this.EnableViewState = false; 
    %>和楼上的原理一样,只不过我这个是放在页面文件头的。
      

  3.   

    非常感谢楼上2位!!我是想把数据库里的表填充到dataset里,然后直接从dataset里导出到Excel文件中,不用gridview等控件~再次感谢~