Method of automatic column width setting for poi and JXL

Source: Internet
Author: User


In my previous blog, "Poi and JXL write Excel performance Comparison", listed POI and JXL two ways to write Excel cells.

In general, in order to make the export better, set the cell to set the automatic column width according to the content, in the POI, using the Method Sheet.autosizecolumn ((short) i), can achieve the effect of the column width adaptive, where I is the columns index.


In JXL, column widths are generally set in the following ways

Cellview CV = new Cellview ();
Cv.setautosize (TRUE);//Auto width
cv.setsize (18);//min. width

sheet.setcolumnview (I,CV);

However, the above method cannot achieve the effect of automatic column width in the case of Chinese characters.


The effect of automatic column widths is well achieved in the following ways. Where data is the text content to be written, +4 is to adjust the effect.

if (Strutil.isnotblank (data) && (Data.getbytes (). length+4) >sheet.getcolumnwidth (i-bempty))
	Sheet.setcolumnview (I,data.getbytes (). length+4); A Chinese account of 2 bytes, after debugging +4, the effect is more ideal

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.