Implementation of an Excel export class (III): NPOI component

Source: Internet
Author: User
Tags export class

The NPOI component is powerful and efficient. Here we only use it to write data in Excel. If you need more content, please search for it by yourself.

  • IWorkbook: The workbook object, which can be instantiated by NPOI. HSSF. UserModel. HSSFWorkbook. Write (Stream stream) is as intended;
  • ISheet: Worksheet object, which can be created by the CreateSheet () method of the IWorkbook instance;
  • IRow: Row object, which can be created by the Create () method of the ISheet instance;
  • ICell: Cell object, which can be created by the CreateCell () method of the IRow instance.

First, handle the page turning problem. Up to 1048576 lines are supported in 10 versions of Excel, and up to 65536 lines are supported in 03 versions of Excel. Only version 03 Excel can be processed. The paging algorithm is as follows:

IWorkbook workbook = =  Int32 RowPerSheet =  (Int32 r = ; r < records.Count; r++ ((r % RowPerSheet) == = (Int32)((Double)r / RowPerSheet) + = workbook.CreateSheet( + (Int32 i = ; i < headers.Count; i++    IRow row = sheet.CreateRow(r % RowPerSheet +  (Int32 i = ; i < props.Length; i++

Note that the NPOI. SS. UserModel and NPOI. HSSF. UserModel namespaces are referenced. Now we have Row, so it is easy to create cells and write content.

Considering that a worksheet with 65536 rows is exported, the reader will be crazy, so the export method is encapsulated into the ExcelHelper class, And the row number attribute and export method are provided, at the same time, set the Header class to its internal class (this is not necessary, but it may be correct to rename it). The implementation is as follows:

   String Name { ;   String PrintName { ;  ==  Int32 MaxRowPerSheet =  Int32 rowPerSheet =  {  (value <  || value >  ArgumentOutOfRangeException(= IWorkbook Export<T>(IList<T> (records ==   ArgumentNullException(= (T).GetProperties().Select(p => Export<T> IWorkbook Export<T>(IList<T> records, IList<String> (records ==   ArgumentNullException( (headers ==  || headers.Count ==   ArgumentNullException(= (T).GetProperties().Select(p =>  Export<T> IWorkbook Export<T>(IList<T> records, IList<Header> (records ==   ArgumentNullException( (headers ==  || headers.Count ==   ArgumentNullException(=  ( i = ; i < headers.Count; i++= (T).GetProperty(headers[i].Name); = = =  ( r = ; r < records.Count; r++ ((r % RowPerSheet) == = (Int32)((Double)r / RowPerSheet) + = workbook.CreateSheet( += sheet.CreateRow( ( i = ; i < headers.Count; i++            row = sheet.CreateRow(r % RowPerSheet +  ( i = ; i < props.Length; i++ (props[i] != ) = props[i].GetValue(records[r],  (value !=  (Int32 i = ; i < workbook.NumberOfSheets; i++= (Int32 h = ; h < headers.Count; h++

The client call is as follows:

  Main(<Person> persons =  List<Person>=  (Int32 i = ; i < records; i++ Person { ID = i, Name =  + i, Birth =  DateTime(, , ), Salary = <ExcelHelper.Header> headers =  List<ExcelHelper.Header> ExcelHelper.Header( ExcelHelper.Header(,  ExcelHelper.Header(,  ExcelHelper.Header(, = = = excelHelper.Export<Person>=  (FileStream stream =

So far, the function has been completed, but the details need to be improved. Observe that all values in the "Birthday" column are 00:00:00, which is the cell value setting statement row. createCell (I ). setCellValue (value. toString () is too simple. For more information, see the ICell attributes and methods of the NPOI component.

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.