簡體   English   中英

如何使用帶有自定義編碼的計時器作業將SharePoint列表數據導出到excel?

[英]How to export SharePoint list data to excel using a timer job with custom coding?

我是SharePoint編程的新手。
誰能告訴我如何使用帶有一些自定義代碼的計時器作業將列表數據導出到Excel

請通過以下鏈接在sharepoint中創建計時器作業。 本文包含詳細的過程。 https://www.mssqltips.com/sqlservertip/3801/custom-sharepoint-timer-job/

以下代碼將幫助您導出列表數據。

 public void Export(List<int> ids) { DataTable table = new DataTable(); try { SPSite site = SPContext.Current.Site; SPWeb web = SPContext.Current.Web; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite siteE = new SPSite(site.ID)) { using (SPWeb webE = siteE.OpenWeb(web.ID)) { webE.AllowUnsafeUpdates = true; SPList list = webE.Lists["Stationery"]; table.Columns.Add("Product", typeof(string)); table.Columns.Add("Quantity", typeof(Decimal)); DataRow newRow; GridView gv = new GridView(); foreach (SPListItem item in list.Items) { if (ids.Contains(Convert.ToInt32(item["ID"].ToString())) && (item["Status"].ToString() == "New")) { newRow = table.Rows.Add(); newRow["Product"] = item["Product"].ToString(); newRow["Quantity"] = Convert.ToDecimal(item["Quantity"].ToString()); item["Status"] = "Exported"; item.Update(); } } SPBoundField boundField = new SPBoundField(); boundField.HeaderText = "Product"; boundField.DataField = "Product"; gv.Columns.Add(boundField); boundField = new SPBoundField(); boundField.HeaderText = "Quantity"; boundField.DataField = "Quantity"; boundField.ControlStyle.Width = new Unit(120); gv.Columns.Add(boundField); gv.AutoGenerateColumns = false; gv.DataSource = table.DefaultView; gv.DataBind(); gv.AllowSorting = false; HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); string attachment = "attachment; filename=export" + "_" + DateTime.Now.ToShortTimeString() + ".xls"; HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "application/Excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gv.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Close(); HttpContext.Current.Response.End(); webE.AllowUnsafeUpdates = false; } } }); } catch (Exception ex) { StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); HttpContext.Current.Response.Write(ex.ToString()); } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM