Asp. Problems encountered in Excel generation in net and the improvement methods

Source: Internet
Author: User
Tags add foreach copy count end httpcontext net string
Let's take a look at the method (which omits some of the judgments and extensions):
build Old Excel code
Copy CodeThe code is 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 CodeThe code is 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.