現在做的工作需要用到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;
}
很粗淺,也是實作了,能交工了。。。