Showing posts with label Export to Excel. Show all posts
Showing posts with label Export to Excel. Show all posts

August 30, 2017

Export large number of records from Database to Excel using C#

Objective
In this blog, I've explained how we can export large number of records in Excel file within very few seconds. I have implemented this solution in one of the project, where I need to export around 100,000 records in less than a minute.

Approach
  • First step is to download the using NuGet Package with following command:
      
 
  • It will add DocumentFormat.OpenXml.dll into your project:
    
 
  • I had a situation where I was getting data in Generic List, So I converted Generic List to DataTable first. (Note: If you have all the records in DataTable itself, you don't need to execute below function!)
 public static DataSet ToDataSet<T>(this IList<T> list)  
 {  
       Type elementType = typeof(T);  
       DataSet ds = new DataSet();  
       DataTable t = new DataTable();  
       ds.Tables.Add(t);  
       foreach(var propInfo in elementType.GetProperties())  
       {  
            Type ColType = Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType;  
            t.Columns.Add(propInfo.Name, ColType);  
       }  
       foreach (T item in list)  
       {  
            DataRow row = t.NewRow();  
            foreach (var propInfo in elementType.GetProperties())  
            {  
                     row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value;  
            }  
            t.Rows.Add(row);  
       }  
       return ds;  
 }  

  • Now use below code to export the records in Excel file.
 //Convert Generic List to DataSet  
 DataSet ds = ToDataSet(objList);   
 //Specify the column names which would be the Headers in Excel File when exported.  
 ds.Tables[0].Columns[0].ColumnName = "Account Name";  
 ds.Tables[0].Columns[1].ColumnName = "NAV Date";  
 ds.Tables[0].Columns[2].ColumnName = "NAV ₹";  
 var filename = ExportDataSet(ds);  
 return filename;  
 private static string ExportDataSet(DataSet ds)  
 {  
      string destination = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".xlsx");  
      using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))  
      {  
           var workbookPart = workbook.AddWorkbookPart();  
           workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();  
           workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();  
           foreach (System.Data.DataTable table in ds.Tables)  
           {  
             var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();  
             var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();  
             sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);  
             DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();  
             string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);  
             uint sheetId = 1;  
             if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)  
             {  
                       sheetId = sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s =>s.SheetId.Value).Max() + 1;  
             }  
             DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };  
             sheets.Append(sheet);  
             DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();  
             List<String> columns = new List<string>();  
             foreach (System.Data.DataColumn column in table.Columns)  
             {  
                  columns.Add(column.ColumnName);  
                  DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();  
                  cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;  
                  cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);  
                  headerRow.AppendChild(cell);  
             }  
             sheetData.AppendChild(headerRow);  
             foreach (System.Data.DataRow dsrow in table.Rows)  
             {  
                  DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();  
                  foreach (String col in columns)  
                  {  
                      DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();  
                      cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;  
                      cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString());  
                      newRow.AppendChild(cell);  
                  }  
                  sheetData.AppendChild(newRow);  
             }  
           }  
      }  
      return destination;  
 }  


If you have any questions you can reach out our SharePoint Consulting team here.

June 27, 2016

SharePoint Survey List returns only 30 items while exporting to spreadsheet.

Problem:
While working with SharePoint Survey List and modifying items, we have encountered the following issue: When a user uses the “Export to Spreadsheet” function offered by SharePoint for Survey List, is not allowed to export more than 30 items, even if survey list contains more than 30 responses.

The survey list is a special list, and cannot add new view to it as we do for other lists or libraries, but we can modify the existing views because actually it uses list view web part.

Resolution:
To resolve, please follow below steps:

Step 1: Open SharePoint Site in SharePoint Designer



1. Open SharePoint Designer and click on “Open Site”.
2. Enter SharePoint Site URL (SharePoint Site that contains Survey List). This can be any - SharePoint On Premise OR SharePoint Online Site.
3. Click on “Open”.


Step 2: Navigate To Survey List In SharePoint Designer



1. In navigation section go to Lists and Libraries.
2. Navigate to survey list and click to open.

Step 3: Open View in Advance Mode



In navigation section go to Lists and Libraries, 
1. Go to View Section and Right Click on View named “Overview”
2. In pop-up menu select “Edit File in Advance Mode”.


Step 4: Modify Export Limit


1. Opening view in advance mode will open “overview.aspx” page in new tab.
2. In <XmlDefinition> tag go to <RowLimit> as highlighted in above image.
3. Change this tag to : 
                          Default tag: <RowLimit Paged=”FALSE”>30</RowLimit>
                          Change tag to:  <RowLimit Paged=”FALSE”>1000</RowLimit>
4. Than press “ctrl+s” to save page. This will change item limit 30 to 1000.Now, Export to Excel can be done for 1000 items. This item limit can be increase as per number of items in list. 

Note: Item limit can be increased up to threshold limit of the list. Default limit for threshold is 5000.

So, this way we can export more than 30 items to spreadsheet from Survey List, too.

If you have any questions you can reach out our SharePoint Consulting team here.