Let's take a look at the method (which omits some of the judgments and extensions):
build Old Excel code
Copy Code code as follows:
<summary>
Export a group of objects to Excel
</summary>
<typeparam name= "T" > Types of objects to be exported </typeparam>
<param name= "Objlist" > A set of Objects </param>
<param name= "filename" > exported filename </param>
<param name= "Columninfo" > 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 HTML for Excel
String excelstr = "";
Type MyType = objlist[0]. GetType ();
Get the property to be displayed based on reflection from the passed in property name information
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";
}
}
Ends if no available properties are found
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";
}
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 ();
}
By this time I think there should be a friend to see the problem.
This method produces a small amount of Excel data does not occur when the problem, when the data is large, the problem came out. Because a variable of type string is defined inside the method, the content that needs to be populated into Excel is superimposed. Using the + + action for string data is equivalent to using string. Concat method Connection string. A new string is generated whenever a + = operation is performed. Will inevitably open up a piece of memory, such an operation will run out of memory, resulting in a outofmemoryexception.
Knowing the problem, the improvement is also easy, that is, the use of StringBuilder overlay needs to populate the content of Excel, the improved code is as follows:
code to generate Excel after improvement
Copy Code code as follows:
<summary>
Export a group of objects to Excel
</summary>
<typeparam name= "T" > Types of objects to be exported </typeparam>
<param name= "Objlist" > A set of Objects </param>
<param name= "filename" > exported filename </param>
<param name= "Columninfo" > 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 HTML for Excel
StringBuilder excelstr = new StringBuilder (Objlist.count * columninfo.count);
Type MyType = objlist[0]. GetType ();
Get the property to be displayed based on reflection from the passed in property name information
list<propertyinfo> Mypro = new list<propertyinfo> ();
foreach (String cName in Columninfo.keys)
{
PropertyInfo p = mytype.getproperty (cName);
if (P!= null)
{
Mypro.add (P);
Excelstr.append (Columninfo[cname]). Append ("T");
}
}
Ends if no available properties are found
if (Mypro.count = = 0) {return;}
Excelstr.append ("\ n");
foreach (T obj in objlist)
{
foreach (PropertyInfo p in Mypro)
{
Excelstr.append (p.getvalue (obj, null)). Append ("T");
}
Excelstr.append ("\ n");
}
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 ();
}
}
When the StringBuilder excelstr = new StringBuilder (Objlist.count * columninfo.count) is instantiated, the pre-allocation start size can be used to better use StringBuilder. At this point, the improvement is complete.
In addition, if you feel that reflection can affect performance, you can change it to an expression tree or use limit.