Problems encountered in Excel generation in ASP. NET and improvement methods

Source: Internet
Author: User

Let's take a look at the method (some judgments and extensions are omitted ):
Generate old Excel Code
Copy codeThe Code is as follows: // <summary>
/// Export a group of objects to EXCEL
/// </Summary>
/// <Typeparam name = "T"> type of the object to be exported </typeparam>
/// <Param name = "objList"> A group of objects </param>
/// <Param name = "FileName"> name of the exported file </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 EXCEL HTML
String excelStr = "";

Type myType = objList [0]. GetType ();
// Obtain the attribute to be displayed from the passed attribute name information based on reflection
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 attributes 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 ();
}

At this time, I think some friends can see the problem.

This method does not cause problems when the Excel data size is small. When the data size increases, the problem arises. The method defines a variable of the string type and overwrites the content to Excel. Using the + = operation for data of the string type is equivalent to using the string. Concat method to connect the string. A new string is generated every time a + = operation is performed. It is inevitable that a piece of memory will be opened up, so that more operations will exhaust the memory and generate an OutOfMemoryException.

Knowing the problem, it is easy to improve, that is, to overwrite the content that needs to be filled in Excel using StringBuilder. The improved code is as follows:
Excel Code generated after improvement
Copy codeThe Code is as follows: // <summary>
/// Export a group of objects to EXCEL
/// </Summary>
/// <Typeparam name = "T"> type of the object to be exported </typeparam>
/// <Param name = "objList"> A group of objects </param>
/// <Param name = "FileName"> name of the exported file </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 EXCEL HTML
StringBuilder excelStr = new StringBuilder (objList. Count * columnInfo. Count );

Type myType = objList [0]. GetType ();
// Obtain the attribute to be displayed from the passed attribute name information based on reflection
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 attributes 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 instantiating StringBuilder excelStr = new StringBuilder (objList. Count * columnInfo. Count);, pre-allocate the start size to better use StringBuilder. So far, the improvement has been completed.
In addition, if you think reflection affects performance, you can change the expression tree mode or use limit.

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.