今天在用jxl来导出数据到execl表格中,遇到列宽不会自动适应问题。原先的代码如下:
'''
CellView cellView = new CellView();
cellView.setAutosize(true); //设置自动大小
sheet1.setColumnView(0, cellView);//根据内容自动设置第一列列宽
sheet1.setColumnView(1, cellView);//根据内容自动设置第二列列宽
'''
若单元格内包含中文,列宽自动适应就失效了
在论坛找到了解决办法,将代码修改如下:
'''
for (int i = 0; i < list.size(); i++) {
CustomerInfo customerInfo = list.get(i);
byte[] bstrLength1 = customerInfo.getCustomerId().getBytes(); //中文字符算两个字节
byte[] bstrLength2 = customerInfo.getCustomerName().getBytes(); //中文字符算两个字节
sheet1.setColumnView(0, bstrLength1.length+2);
sheet1.setColumnView(1, bstrLength2.length+2);
sheet1.addCell(new Label(0, i + 3, customerInfo .getCustomerId()));
sheet1.addCell(new Label(1, i + 3, customerInfo .getCustomerName()));
}
'''
效果如图:
20181221105147dama.png原帖截图:
image.png原帖链接:JXL导出EXCEL:如何自动调整列宽
网友评论