Apache POI autoSizeColumn 调整大小不正确 [英] Apache POI autoSizeColumn Resizes Incorrectly

查看:40
本文介绍了Apache POI autoSizeColumn 调整大小不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 java 中使用 Apache POI 创建一个 excel 文件.我填写数据然后尝试自动调整每列的大小,但是大小总是错误的(我认为一致).前两行总是(?)完全折叠.当我在 excel 中自动调整列的大小时,它工作得很好.

I'm using Apache POI in java to create an excel file. I fill in the data then try to autosize each column, however the sizes are always wrong (and I think consistent). The first two rows are always(?) completely collapsed. When I autosize the columns in excel, it works perfectly.

没有写入空白单元格(我相信),调整大小是我最后要做的事情.

No blank cells are being written (I believe) and the resizing is the last thing I do.

相关代码如下:这是一个没有错误处理等的简化版本

Here's the relevant code: This is a boiled down version without error handling, etc.

public static synchronized String storeResults(ArrayList<String> resultList, String file) {
    if (resultList == null || resultList.size() == 0) {
        return file;
    }
    FileOutputStream stream = new FileOutputStream(file);

    //Create workbook and result sheet
    XSSFWorkbook book = new XSSFWorkbook();
    Sheet results = book.createSheet("Results");

    //Write results to workbook
    for (int x = 0; x < resultList.size(); x++) {
        String[] items = resultList.get(x).split(PRIM_DELIM);

        Row row = results.createRow(x);
        for (int i = 0; i < items.length; i++) {
            row.createCell(i).setCellValue(items[i]);
        }
    }

    //Auto size all the columns
    for (x = 0; x < results.getRow(0).getPhysicalNumberOfCells(); x++) {
        results.autoSizeColumn(x);
    }

    //Write the book and close the stream
    book.write(stream);
    stream.flush();
    stream.close();

    return file;
}

我知道有一些类似的问题,但其中大多数只是在填写数据之前调整大小的情况.少数不是更复杂/没有答案.

I know there are a few questions out there similar, but most of them are simply a case of sizing before filling in the data. And the few that aren't are more complicated/unanswered.

我尝试使用几种不同的字体,但没有奏效.这并不奇怪,因为无论字体是什么,所有列都应该完全折叠或不折叠.

I tried using a couple different fonts and it didn't work. Which isn't too surprising, as no matter what the font either all the columns should be completely collapsed or none should be.

另外,由于字体问题,我在 Windows 7 上运行该程序.

Also, because the font issue came up, I'm running the program on Windows 7.

已解决:这是字体问题.我发现唯一有效的字体是 Serif.

SOLVED: It was a font issue. The only font that I found that worked was Serif.

推荐答案

只是为了从我的评论中做出一个答案.行无法正确调整大小,因为 Java 不知道您尝试使用的字体 此链接 如果您想在 Java 中安装新字体以便您可以使用更高级的字体,应该会有所帮助.它还包含 Java 知道的默认字体列表.

Just to make an answer out of my comment. The rows couldn't size properly because Java was unaware of the font you were trying to use this link should help if you want to install new fonts into Java so you could use something fancier. It also has the list of default fonts that Java knows.

很高兴这有助于您解决问题!

Glad this helped and you got your issue solved!

这篇关于Apache POI autoSizeColumn 调整大小不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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