彩票走势图

巧用DevExpress GridView导入导出Excel

原创|其它|编辑:郝浩|2010-02-26 10:25:58.000|阅读 8306 次

概述:本文通过示例(含源码)详细介绍了如何使用DevExpress GridView导入与导出Excel的方法。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

1、Excel数据导入到数据库中:
在页面放置1个按钮控件(Button1)和1个文件上传控件(FileUpload1)
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {
            if (FileUpload1.PostedFile.FileName != "")
            {
                string filepath = FileUpload1.PostedFile.FileName;
                string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                string sheetname = "Sheet1";
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
                OleDbConnection conn = new OleDbConnection(strConn);
                OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "$]", conn);
                DataSet ds = new DataSet();
                oada.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                GridView1.Visible = true;
            }
        }
        catch (Exception error)
        {
            this.Label1.Text = "上传发生错误!原因:" + error.ToString();
        }
    }

注意把Excel文件属性中加入ASPNET用户权限,不然不能导入

3、将数据库中的数据导入Excel中,在这里我讲叙自己实践中的两个方法:

方法一:从最基本的写文件入手,将从数据库中读出来的数据写入到Excel中;

//该方法实现将数据导入到Excel文件中,其中的DataTable dt就是你需要将数据写入到Excel中的数据;
public void ExportExcel( DataTable dt , StreamWriter w )
...{
    try
    ...{
        for( int i = 0 ; i < dt.Columns.Count ; i ++ )
        ...{
             w.Write ( dt.Columns[i] );
             w.Write( ' ' );
         }
  &nbsp;      w.Write ( " " );

        object[] values = new object [dt.Columns.Count];
        foreach ( DataRow dr in dt.Rows )
        ...{
             values = dr.ItemArray ;
            for ( int i = 0 ; i < dt.Columns.Count ; i++ )
            ...{
                 w.Write ( values[i] );
                 w.Write ( ' ' );
             }
             w.Write ( " " );
         }
         w.Flush();
         w.Close();
     }
    catch
    ...{
         w.Close();
     }
}

StreamWriter w就是你自己创建的一个流,创建该数据流时,你需要自己指定需要将数据写入到那个文件,即指定文件路径,要实现下载的话,就可以简单的用Response.Redirect

( "指定需要被下载的文件路径" );

方法二:该方法实现的是将数据从DataGrid中导入到Excel中:

//filename为Excel的名字,ToExcelGrid就是数据源,在此为DataGrid数据源;
private void ExportExcelFromDataGrid( string filename , System.Web.UI.WebControls.DataGrid ToExcelGrid )
...{
     Response.Clear();
     Response.Buffer= true;    
     Response.Charset="utf-8";          
     Response.AppendHeader("Content-Disposition","attachment;filename="+Server.UrlEncode ( filename ) );    
     Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文  
     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);  
     ToExcelGrid.RenderControl(oHtmlTextWriter);    
     Response.Write(oStringWriter.ToString());  
     Response.End();

Excel导出:
页面上放一个GridView控件,ID为GridView1,添加个导出按钮ID为Button2

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.GridView1.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
&nbsp;       Response.End();

    }

页面后台必须重写这两个方法,不然出现错误!
    public override void VerifyRenderingInServerForm( Control control )
    {

    }
    protected override void Render(HtmlTextWriter writer)
    {
        if (Page != null)
        {
            Page.VerifyRenderingInServerForm(this);
        }
        base.Render(writer);
    }

将GridView打印出来,添加个打印按钮,ID为Button4
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Write("<script> window.print()</script>");
    }


(慧都控件网版权所有,转载请注明出处,否则追究法律责任)


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:北风的专栏

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP