问题使用Apache POI在Excel单元格大小配合到内容的大小 [英] Problem in fitting the excel cell size to the size of the content when using apache poi

查看:418
本文介绍了问题使用Apache POI在Excel单元格大小配合到内容的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者到Apache POI API。我试图创建一个使用ArrayList的Excel工作表。

I am beginner to Apache POI api. I am trying to create excel sheet using arraylist.

我的Java code是如下:

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++;
    }

的ArrayList是如下:

The arraylist is as follows:

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表,列不适合的内容的大小正常。我搜索谷歌与此相关的问题,并找到

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);

是解决我的问题。我加在上面的code,但问题仍然存在。我在正确的使用它吗?

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 3.6浦二

P.s: I am using Apache Poi 3.6

推荐答案

您只需将呼叫转移到

sheet.autoSizeColumn(5);

在你的code点的之后的数据已经增加,所以权后,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆