如何使用自定义编码的计时器作业将SharePoint列表数据导出到Excel?

时间:2018-06-17 12:55:14

标签: excel sharepoint sharepoint-2013

我是 SharePoint 编程的新手 谁能告诉我如何使用带有一些自定义代码的计时器作业将列表数据导出到 Excel

1 个答案:

答案 0 :(得分:0)

请通过以下链接在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()); 
     }
&#13;
&#13;
&#13;