Export a list of LINQ objects to Excel file

前端 未结 3 607
一整个雨季
一整个雨季 2021-02-10 22:21

From a web application, is there a simple way to export a list of LINQ objects to an Excel file? Are there any good libraries that can do this?

3条回答
  •  执念已碎
    2021-02-10 22:34

    This is the Excel Export I ended up with based on the link to the VB video above. It takes any List of Objects (It excludes navigation properties and collections on Entity Framework objects) and exports them to Excel. It exports about 35K records in ~4 seconds.

    public void ExportToExcel(List list)
        {
            int columnCount = 0;
    
            DateTime StartTime = DateTime.Now;
    
            StringBuilder rowData = new StringBuilder();
    
            PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
            rowData.Append("");
            foreach (PropertyInfo p in properties)
            {
                if (p.PropertyType.Name != "EntityCollection`1" && p.PropertyType.Name != "EntityReference`1" && p.PropertyType.Name != p.Name)
                {
                    columnCount++;
                    rowData.Append("" + p.Name + "");
                }
                else
                    break;
    
            }
            rowData.Append("");
    
            foreach (T item in list)
            {                
                rowData.Append("");
                for (int x = 0; x < columnCount; x++) //each (PropertyInfo p in properties)
                {                    
                    object o = properties[x].GetValue(item, null);
                    string value = o == null ? "" : o.ToString();                    
                    rowData.Append("" + value + "");
    
                }
                rowData.Append("");
            }
    
            var sheet = @"
                        
                        
                            
                                MSADMIN
                                MSADMIN
                                2011-07-12T23:40:11Z
                                Microsoft
                                12.00
                            
                            
                                6600
                                12255
                                0
                                60
                                False
                                False
                            
                            
                                
                                
                            
                            
                                
                                    " + rowData.ToString() +@"
                                
    300 300 3 2 False False
    False False
    False False "; System.Diagnostics.Debug.Print(StartTime.ToString() + " - " + DateTime.Now); System.Diagnostics.Debug.Print((DateTime.Now - StartTime).ToString()); string attachment = "attachment; filename=Report.xml"; HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.Write(sheet); HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.End(); }

提交回复
热议问题