使用apache poi时excel单元格大小适配内容大小的问题 [英] Problem in fitting the excel cell size to the size of the content when using apache poi
问题描述
我是 Apache POI api 的初学者.我正在尝试使用 arraylist 创建 excel 表.
I am beginner to Apache POI api. I am trying to create excel sheet using arraylist.
我的java代码如下.
My java code is as follows.
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFRow row4 = sheet.createRow(4);
row4.createCell(4).setCellValue("name");
row4.createCell(5).setCellValue("emailId");
sheet.autoSizeColumn(5);
List<Bean> nameList = this.getArrayList();
Iterator<Bean> nameListIterator = nameList.iterator();
sheet.autoSizeColumn(5);
int i=5;
HSSFRow row = null;
while(nameListIterator.hasNext())
{
Bean bean = nameListIterator.next();
row = sheet.createRow(i);
row.createCell(4).setCellValue(bean.getName());
row.createCell(5).setCellValue(bean.getMailId());
i++;
}
数组列表如下:
List<Bean> beanList = new ArrayList<Bean>();
beanList.add(new Bean("Amy","g@y.comrtyrtyrtyrtyrtyr"));
beanList.add(new Bean("Joan","p@y.comrtyrtyrtyrtyrtyr"));
beanList.add(new Bean("Megan","r@y.comrtyrtyrtyrtyrtyr"));
beanList.add(new Bean("Joe","m@y.comrtyrtyrtyrtyrtyr"));
beanList.add(new Bean("Febi","j@y.comrtyrtyrtyrtyrtyr"));
生成excel表格时,列不适合内容大小.我搜索了与此问题相关的 Google 并找到了
When the excel sheet is generated, the column does not fit to the size of the content correctly. I searched Google related to this problem and found
sheet.autoSizeColumn(5);
sheet.autoSizeColumn(5);
是我问题的解决方案.我在上面的代码中添加了,但问题仍然存在.我是否正确使用它?
is the solution to my problem. I added as in the code above, but still the problem persists. Am I using it correctly?
还有其他解决办法吗?
请帮忙
提前致谢
P.s:我使用的是 Apache Poi 3.6
P.s: I am using Apache Poi 3.6
推荐答案
您只需要将呼叫移至
sheet.autoSizeColumn(5);
到你的代码中添加数据之后的一点,所以在你的 while 循环之后应该可以正常工作.
to a point in your code after the data has been added, so right after your while loop should work.
这篇关于使用apache poi时excel单元格大小适配内容大小的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!