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?
-
I guess the built in "Export to Excel" functionality in the Ribbon is not making you happy?– Robert LindgrenCommented 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?– MdMazzottiCommented Jan 17, 2014 at 9:51
-
@MdMazzotti, I have not added the SharePoint Enterprise tag. The OP has targeted the question with SharePoint Enterprise– Robert LindgrenCommented Jan 17, 2014 at 9:52
-
@RobertLindgren sorry I misinterpreted the "question edited" message.– MdMazzottiCommented 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– priyaCommented Jan 17, 2014 at 9:55
3 Answers
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
}
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.
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!
-
No need to use the old Excel Interop these days. We luckily have the OpenXml SDK at disposal. Commented Jan 17, 2014 at 10:40
-