Java USES the JXL package to write Excel files for column width implementations

  • 2020-04-01 02:33:30
  • OfStack

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

1. Implement ideas
(1) the length of Chinese characters is twice as long as the length of English letters.
(2) for the data to be written in Excel to calculate the maximum column width of each column, finally, directly set the column width of this column to the maximum value of this column.

2. Implementation code


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 with two rows
         List<String> row1=new ArrayList<String>();    row1.add(" Best column width ");    row1.add(" This is basically achievable ");
        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 are 2 units long
                 if(columnBestWidth[j]<width)    /// find the best column width so far
                     columnBestWidth[j]=width;
            }
        }

        for(int i=0;i<columnBestWidth.length;i++){    /// sets the width of each column
            sheet.setColumnView(i, columnBestWidth[i]);
        }
    }

    public static int getChineseNum(String context){    /// statistics in context is the number of Chinese characters
        int lenOfChinese=0;
        Pattern p = Pattern.compile("[u4e00-u9fa5]");    //Unicode encoding range for Chinese characters
        Matcher m = p.matcher(context);
        while(m.find()){
            lenOfChinese++;
        }
        return lenOfChinese;
    }
}

3. It's just a realization
(1) this is written in a project, automatic derivative Excel electronic report, can be well implemented. Mainly: Chinese characters, Arabic numerals.
(2) Chinese characters are square characters, the size of each character is very uniform, can be very surprising statistical column width.
(3) there are 26 lowercase letters in English, each letter has a small difference in size, and the optimal column width can also be achieved.
(4) for other characters, the placeholder size of each character varies, such as the size of the letter sequence "IIIIIII" and the letter "DDDDDDD".

In summary, jxl.jar does not automatically implement the method that best fits the column width; If the Chinese characters are the majority of the e-report to be exported, this program can be well used to achieve the most suitable column width.


Related articles: