Export multiple worksheets in Java Excel (add multiple sheets)

Source: Internet
Author: User

When the data volume is large and exceeds 65536, the following error occurs during export:

Jxl. write. biff. RowsExceededException: The maximum number of rows permitted on a worksheet been exceeded

(Explanation: jxl. write. biff. RowsExceededException: the maximum number of rows allowed in the worksheet has exceeded)

Therefore, my solution is to export multiple work tables.

Public static void outputExcelData () throws IOException, WriteException {

/** Save the value to the List */

List result = new ArrayList ();

User user = new User ();

User. setId ("1 ");

User. setName ("yfli ");

Result. add (user );

User user2 = new User ();

User2.setId ("1 ");

User2.setName ("zhangjie ");

Result. add (user2 );

User user3 = new User ();

User3.setId ("1 ");

User3.setName ("lzhang ");

Result. add (user3 );

String fileName = "F :\\ sfData.xls ";

// First, use the Workbook class factory method to create a writeable Workbook object.

WritableWorkbook wwb = Workbook. createWorkbook (new File (fileName ));

File dbfFile = new File (fileName );

If (! DbfFile. exists () | dbfFile. isDirectory ()){

DbfFile. createNewFile ();

}

Int totle = result. size (); // gets the size of the List set.

Int mus = 2; // a maximum of 2 data records can be stored in a worksheet in an excel worksheet)

Int avg = totle/mus;

For (int I = 0; I <avg + 1; I ++ ){

WritableSheet ws = wwb. createSheet ("list" + (I + 1), I); // create a writable Worksheet

// Add a header

Ws. addCell (new Label (0, 0, "Serial Number "));

Ws. addCell (new Label (1, 0, "name "));

Int num = I * mus;

Int index = 0;

For (int m = num; m <result. size (); m ++ ){

If (index = mus) {// when determining index = mus, the current for loop exists.

Break;

}

User use = (User) result. get (m );

// Add the generated cells to the worksheet

// (Note that in Excel, the first parameter table represents the column and the second parameter represents the row)

Ws. addCell (new Label (0, index + 1, use. getId ()));

Ws. addCell (new Label (1, index + 1, use. getName ()));

Index ++;

}

}

Wwb. write (); // write data to the file from the memory

Wwb. close (); // close the resource and release the memory.

}

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.