Java uses JXL package to write Excel files for column width implementation _java

Source: Internet
Author: User

Note that this is only basically achievable, basically for Chinese electronic reports.

1. Realize the idea
(1) The average length of Chinese characters is twice times that of the English alphabet, and "character" is very uniform.
(2) for data that you want to write to Excel, the maximum column width for each column is finally set to the maximum value of this column.

2. Implementation code

Copy Code code as follows:

Import Java.io.File;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;

Import JXL. workbook;
Import Jxl.write.Label;
Import Jxl.write.WritableSheet;
Import Jxl.write.WritableWorkbook;

Chinese name
public class Excelbestcolumn
{
public static void Main (String argus[]) throws exception{
Construct data, there are two lines
List<string> row1=new arraylist<string> ();    Row1.add ("Most suitable column width"); Row1.add ("This basic can be achieved");
List<string> row2=new arraylist<string> ();    Row2.add ("Best Column Width"); Row2.add ("Haha");
List list=new ArrayList ();    List.add (ROW1); List.add (ROW2);

Write data to Excel
Writableworkbook book= Workbook.createworkbook (New File ("T.xls"));
Writablesheet sheet=book.createsheet ("test", 0);
Writedatatosheet (sheet,list);
Book.write ();
Book.close ();
}

public static void Writedatatosheet (Writablesheet sheet,list<list<string>> List) throws exception{
int columnbestwidth[]=new int[list.get (0). Size ()]; An array that holds the best column width data

for (int i=0;i<list.size (); i++) {
List<string> Row=list.get (i);
for (int j=0;j<row.size (); j + +) {
Sheet.addcell (New Label (J,i,row.get (j)));

int Width=row.get (j). Length () +getchinesenum (Row.get (j)); Chinese characters account for 2 units of length
if (columnbestwidth[j]<width)///to obtain the best column width so far
Columnbestwidth[j]=width;
}
}

for (int i=0;i<columnbestwidth.length;i++) {///set width per column
Sheet.setcolumnview (i, columnbestwidth[i]);
}
}

public static int Getchinesenum (String context) {///count the number of Chinese characters in the
int lenofchinese=0;
Pattern p = pattern.compile ("[\u4e00-\u9fa5]"); Unicode coding range of Chinese characters
Matcher m = p.matcher (context);
while (M.find ()) {
lenofchinese++;
}
return Lenofchinese;
}
}

3. It's just a realization.
(1) This is a project written in the automatic derivative Excel Electronic report, can be well implemented. Mainly: Chinese characters, Arabic numerals.
(2) Chinese characters are character, each word size is very uniform, can be very surprised by the statistical column width.
(3) English lowercase letters A total of 26, each letter size difference is very small, can also achieve the best column width.
(4) Other characters, the placeholder size of each character varies, such as the size of the alphabetic sequence "IIIIIII" and the letter "DDDDDDD".

In short, Jxl.jar does not automatically implement the most appropriate way to column width, if you want to export the most Chinese characters in the electronic report, you can use this program to achieve the most appropriate column width.

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.