将现有文件复制到服务器SharePoint

时间:2019-11-01 02:52:56

标签: sharepoint

我面临着将文件更改为CSV并将其保存到本地环境的困难。我该如何实现?尝试环顾四周,但似乎不是我要找的东西。

我正在SharePoint 2010上运行。在此之前,此代码仅从SharePoint中获取数据并将其转换为xlsx并将其更新到我们的网站中。

    private static void GenerateSPGroupUsersReport() //
    {
        Log("Generate Sharepoint Group Users Report");

        DataSet dsSecurityReport = new DataSet();
        string ConnectedWebURL = ConfigurationManager.AppSettings["SPGroupUsersWebURL"];
        //string[] strURL = ConnectedWebURL.Split(';');
        DataTable dTblSPGroupUser = new DataTable();
        dTblSPGroupUser.Columns.Add("SiteURL", typeof(string));
        dTblSPGroupUser.Columns.Add("SharepointGroup", typeof(string));
        dTblSPGroupUser.Columns.Add("User", typeof(string));
        // Hafees add 10/22/2019
        dTblSPGroupUser.Columns.Add("UserLanID", typeof(string));
        dTblSPGroupUser.Columns.Add("Email", typeof(string));

        SPSite site = new SPSite(ConnectedWebURL);

        SPWebApplication webApp = site.WebApplication;

        foreach (SPSite s in webApp.Sites)
        {
            SPGroupCollection groupCol = s.RootWeb.SiteGroups;

            foreach (SPGroup group in groupCol)
            {
                // Hafees include group.Users, user.Email
                foreach (SPUser user in group.Users)
                {
                    dTblSPGroupUser.Rows.Add(s.Url, group.Name, user.Name, user.LoginName, user.Email);
                }
                //bool contains = dTblSPGroupUser.AsEnumerable().Any(rowC => group.Name == rowC.Field<string>("SharepointGroup"));

                //if (!contains)
                //{
                //    foreach (SPUser user in group.Users)
                //    {
                //        dTblSPGroupUser.Rows.Add(s.Url, group.Name, user.Name);
                //    }
                //}
            }
        }

        DataSet dsSPGroup = new DataSet();
        dsSPGroup.Tables.Add(dTblSPGroupUser);

        SaveIntoSPLibrary(site, dsSPGroup, "GroupUsers_" + ConnectedWebURL.Replace("http://", "").Replace("https://", "").Replace(":", "-").Trim());

        Log("Generate Sharepoint Group Users Report Complete");
    }

//这是我生成用户报告组的地方。

    private static void SaveIntoSPLibrary(SPSite site, DataSet ds, string fileName)
    {
        string UIResourceServerRelativeWebURL = ConfigurationManager.AppSettings["UIResourceServerRelativeWebURL"];
        using (SPWeb web = site.OpenWeb(UIResourceServerRelativeWebURL))
        {
            byte[] byteArray = GenerateExcelFile(ds);

            string CustomReportLibrary = ConfigurationManager.AppSettings["CustomReportLibrary"];
            string strFileName = String.Format(fileName + ".{0}.xlsx", DateTime.Today.ToString("yyyyMMdd"));

            Log("Saving into SP Library. " + CustomReportLibrary + strFileName);

            web.AllowUnsafeUpdates = true;
            SPFile file = web.Files.Add(CustomReportLibrary + strFileName, byteArray, true);
            file.Item["Year"] = DateTime.Now.ToString("yyyy");
            file.Item["Month"] = string.Format("{0}. {1}", DateTime.Now.Month, DateTime.Now.ToString("MMMM"));
            file.Item.Update();
            file.Update();
            web.AllowUnsafeUpdates = false;
        }

    }

//这是文件保存到xlsx并将其更新到SharePoint库的地方。

我尝试做一些修改后的SaveIntoLibrary副本,更改为CSV文件并创建一个新的configurationManager,它将指向我的本地目录。但似乎我在某个地方错了。这些文件仍然没有进入我的本地目录。请指教。

1 个答案:

答案 0 :(得分:0)

您应该将DataTable报告数据导出到本地CSV,

检查this thread中的代码,这将输出csv文件,以便您保存到本地。

var dataTable = GetData();
    StringBuilder builder = new StringBuilder();
    List<string> columnNames = new List<string>();
    List<string> rows = new List<string>();

    foreach (DataColumn column in dataTable.Columns)
    {
        columnNames.Add(column.ColumnName); 
    }

    builder.Append(string.Join(",", columnNames.ToArray())).Append("\n");

    foreach (DataRow row in dataTable.Rows)
    {
        List<string> currentRow = new List<string>();

        foreach (DataColumn column in dataTable.Columns)
        {
            object item = row[column];

            currentRow.Add(item.ToString());
        }

        rows.Add(string.Join(",", currentRow.ToArray()));
    }

    builder.Append(string.Join("\n", rows.ToArray()));

    Response.Clear();
    Response.ContentType = "text/csv";
    Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");
    Response.Write(builder.ToString());
    Response.End();