none
Exporting sharepoint list items to excel by csom c# RRS feed

  • Question

  • I am getting items from a list using caml query. After that I need to export to excel by csom (c#).

    Is there any code demo which I could refer ?

    Wednesday, December 4, 2019 10:54 AM

Answers

  • Wednesday, December 4, 2019 3:36 PM
  • Hi developer sp,

    Here is a code demo for your reference:

                string csvFilePath = @"c:\temp\myCSV.csv";
                StringBuilder items = new StringBuilder();
                ClientContext clientContext = new ClientContext("http://sp/sites/dev/");
                List list = clientContext.Web.Lists.GetByTitle("CustomList");
                ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery());
                clientContext.Load(list);
                clientContext.Load(listItems);
                clientContext.ExecuteQuery();
                foreach (ListItem listItem in listItems)
                {
                    items.Append(listItem.Id + ",");
                    items.Append(listItem.FieldValues["Name"]);
                    items.AppendLine();
                }
                System.IO.File.WriteAllText(csvFilePath, items.ToString());

    This will export all list items into a CSV file located in local disk path.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, December 5, 2019 2:45 AM

All replies

  • Wednesday, December 4, 2019 3:36 PM
  • Hi developer sp,

    Here is a code demo for your reference:

                string csvFilePath = @"c:\temp\myCSV.csv";
                StringBuilder items = new StringBuilder();
                ClientContext clientContext = new ClientContext("http://sp/sites/dev/");
                List list = clientContext.Web.Lists.GetByTitle("CustomList");
                ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery());
                clientContext.Load(list);
                clientContext.Load(listItems);
                clientContext.ExecuteQuery();
                foreach (ListItem listItem in listItems)
                {
                    items.Append(listItem.Id + ",");
                    items.Append(listItem.FieldValues["Name"]);
                    items.AppendLine();
                }
                System.IO.File.WriteAllText(csvFilePath, items.ToString());

    This will export all list items into a CSV file located in local disk path.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, December 5, 2019 2:45 AM