Npoi large Data volume multiple sheet export Source (original)

Source: Internet
Author: User

#region npoi Large Data volume multiple sheet export

<summary>
Large data volume multiple sheet export
</summary>
<typeparam name= "T" > Data source entity class </typeparam>
<param name= "objlist" > Data source </param>
<param name= "FileName" > File name </param>
<param name= "Btybytes" > Export Data Flow </param>
<param name= "Columninfo" > display column corresponding data dictionary </param>
<param name= "ListCount" > Each sheet contains the number of data bars </param>
<returns></returns>
public static bool Exportexceltest<t> (List<t> objlist, string fileName, ref byte[] Btybytes, dictionary< String, string> columninfo = Null,int listcount=10000)
{
BOOL Bresult = false;
Generate an Excel file in memory:
Hssfworkbook book = new Hssfworkbook ();
if (objlist! = null && objlist.count > 0)
{
Double Sheetcount = math.ceiling ((double) objlist.count/listcount);
for (int i = 0; i < Sheetcount; i++)
{
Isheet sheet = null;
Sheet = Book. Createsheet ("sheet" + i);
Sheet. Defaultrowheight = 20 * 10;
list<t> list = new list<t> ();
List = objlist.skip<t> (ListCount * i). Take<t> (ListCount). ToList ();

int rowIndex = 0;
int startcolindex = 0;
int colindex = Startcolindex;

To create a table header style
Icellstyle style = Book. Createcellstyle ();
Style. Alignment = HorizontalAlignment.Center;
Style. WrapText = true;
IFont font = Book. CreateFont ();
Font. fontheightinpoints = 16;
Font. Boldweight = (short) npoi. Ss. UserModel.FontBoldWeight.Bold;
Font. FontName = "Simplified Chinese";
Style. SetFont (font);//head style

Type myType = null;
MyType = objlist[0]. GetType ();
Gets the properties to display based on the property name information passed in from the reflection
list<propertyinfo> Mypro = new list<propertyinfo> ();
Propertyinfo[] Properties = mytype.getproperties ();

#region Defining a table header
int m = 0;
if (columninfo! = null)
{
var rowheader = sheet. CreateRow (0);
Rowheader. Height = Rowheader. Height = 20 * 20;
foreach (String cName in Columninfo.keys)
{
PropertyInfo p = mytype.getproperty (cName);
if (P! = null)
{
Mypro.add (P);
Rowheader. Createcell (M). Setcellvalue (Columninfo[cname]);
m++;
}
}
}
#endregion
#region Define a table body and assign values
End If no available attributes are found
if (Mypro.count = = 0) {return bresult;}
foreach (T obj in list)
{
int n = 0;
if (sheet! = null)
{
rowindex++;
var sheetrow = sheet. CreateRow (RowIndex);
Sheetrow. Height = Sheetrow. Height = 20 * 20;
foreach (PropertyInfo p in Mypro)
{
Dynamic val = p.getvalue (obj, null)?? "";
String Valtype = val. GetType (). ToString ();
if (Valtype. ToLower (). IndexOf ("decimal", stringcomparison.ordinal) >-1)
{
val = convert.todouble (val);
}
else if (valtype. ToLower (). IndexOf ("datetime", StringComparison.Ordinal) >-1)
{
val = val. ToString ("Yyyy-mm-dd HH:mm:ss");
if (Val. Equals ("0001-01-01 00:00:00"))
{
val = "";
}
}
Sheetrow. Createcell (n). Setcellvalue (Val);
n++;
}
}

}
#endregion
}
}
Else
{
Create a worksheet in a workbook
Hssfsheet sheet = Book. Createsheet () as Hssfsheet;
Sheet. Setcolumnwidth (0, 30 * 256);
if (sheet! = null) sheet. CreateRow (0). Createcell (0). Setcellvalue ("No data! ");
}

Try
{
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";
using (MemoryStream ms = new MemoryStream ())
{
Book. Write (MS);
Rs. BinaryWrite (Ms. ToArray ());
Ms. Flush ();
}
}
catch (SystemException ex)
{
Loghelper.write (ex);
}
catch (ApplicationException ex)
{
Loghelper.write (ex);
}
return bresult;
}


#endregion

Npoi large Data volume multiple sheet export Source (original)

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.