c sharp exports the code for Excel from the collection of entity objects

  • 2020-05-05 11:47:50
  • OfStack

Or will Datagrid or Gridview output is exported, mainly is divided into call COM + components or using Response (B/S architecture project, of course) of the output, COM + components have been applied in the project before, but to be honest the effect is not good, it is deployed very troublesome, 2 it is to remember at that time a process like WEB server-side wouldn't shut off, and there is a problem that is installed on the server EXCEL version is different, in a program called the method of the incoming parameter number is not the same, really depressed, But the upside is that this approach is certainly the most flexible.

We are still in a B/talk about the export for the project application of S architecture, a little general or derived from the data set out to the right, after all, to export the output of a Gridview applicability to the individual feels this way is not high, because most of the application of our page Gridview are engaged to paging, we do it or directly from the data set out to guide, and guide from the DataTable out many ready-made online, but used in a strictly hierarchical architecture project, will still be some unpleasant, if we are strict with our project is a standard three-tier architecture, We passed between layer and layer is our entity object, we don't transfer DataTable this type of thing, we export data can only be entity object collection, so we need to write a derivation of the entity object as the data source method, I still don't feel very perfect, such as "column information derived" parameter in the parameter design, feel there's a better way, ha ha, later free again want to!

Post my implementation, everyone also help to see!


/// <summary> 
///  Export a set of objects into EXCEL 
/// </summary> 
/// <typeparam name="T"> The type of the object to be exported </typeparam> 
/// <param name="objList"> A set of objects </param> 
/// <param name="FileName"> The exported file name </param> 
/// <param name="columnInfo"> The column name information </param> 
public static void ExExcel<T>(List<T> objList, string FileName, Dictionary<string, string> columnInfo) 
{ 

if (columnInfo.Count == 0) { return; } 
if (objList.Count == 0) { return; } 
// generate EXCEL the HTML 
string excelStr = ""; 

Type myType = objList[0].GetType(); 
// Based on the reflection, you get the property to display from the property name information passed in  
List<PropertyInfo> myPro = new List<PropertyInfo>(); 
foreach (string cName in columnInfo.Keys) 
{ 
PropertyInfo p = myType.GetProperty(cName); 
if (p != null) 
{ 
myPro.Add(p); 
excelStr += columnInfo[cName] + "\t"; 
} 
} 
// If no properties are found available, it ends  
if (myPro.Count == 0) { return; } 
excelStr += "\n"; 

foreach (T obj in objList) 
{ 
foreach (PropertyInfo p in myPro) 
{ 
excelStr += p.GetValue(obj, null)+"\t"; 
} 
excelStr += "\n"; 
} 

// The output EXCEL 
HttpResponse rs = System.Web.HttpContext.Current.Response; 
rs.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 
rs.AppendHeader("Content-Disposition", "attachment;filename=" + FileName); 
rs.ContentType = "application/ms-excel"; 
rs.Write(excelStr); 
rs.End(); 
} 

Related articles: