0

I require to export list items from a SharePoint list to Excel. The order of items in the Excel sheet should be the same as in the list. Can somebody suggest console application code for this?

5
  • I guess the built in "Export to Excel" functionality in the Ribbon is not making you happy? Jan 17, 2014 at 9:50
  • @RobertLindgren why did you add the sharepoint-enterprise tag? Can't this question be also relevant to SP foundation?
    – MdMazzotti
    Jan 17, 2014 at 9:51
  • @MdMazzotti, I have not added the SharePoint Enterprise tag. The OP has targeted the question with SharePoint Enterprise Jan 17, 2014 at 9:52
  • @RobertLindgren sorry I misinterpreted the "question edited" message.
    – MdMazzotti
    Jan 17, 2014 at 9:53
  • Hello all, this is a requirement to do it using code in c#.not using the out of box functionality. If you know the answer you can tell
    – priya
    Jan 17, 2014 at 9:55

3 Answers 3

2

Here there's one way to do it.

The only thing missing is the OrderBy clause, as you stated you want to keep the same order (I guess you set a sort criterion on your AllItems view).

So you need to add this code snippet:

// Build a query with an orderby clause
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='YOUR_SORT_COLUMN' /></OrderBy>";
SPListItemCollection items = list.GetItems(query);

foreach (SPListItem item in items)
{
   //use the code showed in the linked page
}
1

You are actually looking for the order of columns in a particular view. Use SPView.ViewFields property to get the fields in a view. This will return fields in the order they appear in a given SPView.

0

Add ListItem to List using client object model

check out my solution, its pritty much the same using the for loop... the view would be default as is... when it loops throught it would get all list items within a list as its shown on sharepoint.

as for excel witin the app you need to import the interop reference!

full detail here:

http://www.dotnetperls.com/excel

with those two you should get your result!

2
  • No need to use the old Excel Interop these days. We luckily have the OpenXml SDK at disposal.
    – MdMazzotti
    Jan 17, 2014 at 10:40
  • very interseting! didnt know about this!
    – Ali Jafer
    Jan 17, 2014 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.