天天看点

Easyui Datagid导出Excel

现在做的工作需要用到Easyui,有关Easyui Datagid导出Excel这方面查看了很多资料,前端很菜,最后只能取了个巧转回后台了。记录一下

$("#lbtnExport").click(function () {
                //alert(document.getElementById("hddlDepartmentName").value);
                window.location.href = "IrRegularExcel.aspx?hddlDepartmentName=" + document.getElementById("hddlDepartmentName").value;
            });
           

IrRegularExcel.aspx页面上我只放了一个Label用于记录,也可以不做处理。

IrRegularExcel.aspx.cs:

<pre name="code" class="csharp">protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["hddlDepartmentName"] != null)
        {
            string RDepartmentName = Request["hddlDepartmentName"].ToString();
            string xlsPath = OutputStudentGrade(RDepartmentName);
            FileOperation.DownloadFile(this, xlsPath);

            Label1.Text = "导出成功!";
        }
        else 
        {
            Label1.Text = "未能获取到导出对象,导出失败!";
        }
    }

    public string OutputStudentGrade(string RDepartmentName)
    {
        User user = Session["user"] as User;
           
DataTable dtdepart = CMLoanApplyLogic.GetApplyListByPWDepartmentID(new Guid(RDepartmentName));
        
        dtdepart.DefaultView.Sort = "ModifyDate desc";
        DataTable datNew = dtdepart.DefaultView.ToTable(false, new string[] { 
            "CName","CID","Tel","ModifyDate","SState"
        });

        string xlsPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/Template/Public/贷款信息.xls");
        string cxlsPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/App_Data/贷款信息.xls");
        File.Copy(xlsPath, cxlsPath, true);
        string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                        "Extended Properties=Excel 8.0;" +
                        "data source=" + cxlsPath;

        OleDbConnection objConn = new OleDbConnection(connStr);
        OleDbCommand objCmd = new OleDbCommand();
        objCmd.Connection = objConn;
        objConn.Open();
        foreach (DataRow data in datNew.Rows)
        {
            objCmd.CommandText = "INSERT INTO [贷款信息$] VALUES ('" + data["CName"] + "','" + data["CID"] + "','" + data["Tel"] + "','"
                + string.Format("{0:d}",data["ModifyDate"]) + "','" + data["SState"] + "')";
            objCmd.ExecuteNonQuery();//string.Format("{0:d}",dt)
        }
        objConn.Close();
        return cxlsPath;
    }
           

很粗浅,也是实现了,能交工了。。。

Easyui Datagid导出Excel