简体   繁体   English

XSSF Apache 兴趣点

[英]XSSF Apache POI

I used the below for setting default column style in XSSF sheet?我使用以下设置 XSSF 工作表中的默认列样式? but this is not working can anyone suggest the bug fix.但这不起作用任何人都可以建议错误修复。

format = workbook.createDataFormat();
style = workbook.createCellStyle();
style.setDataFormat(format.getFormat("@"));
sheet.setDefaultColumnStyle(1, style); 

As of POI 4.1.0 it is working with SXSSF, but with a caveat.从 POI 4.1.0 开始,它与 SXSSF 一起工作,但有一个警告。 The default column style only defines which style will be applied when users enter data into empty cells in the sheet created by POI.默认列样式仅定义当用户将数据输入到 POI 创建的工作表中的空单元格时将应用哪种样式。 If you create cells and enter data through POI, the default format does not apply, you have to use setCellStyle(CellStyle) .如果您通过 POI 创建单元格并输入数据,则默认格式不适用,您必须使用setCellStyle(CellStyle)

Eg for text styles:例如,对于文本 styles:

private CellStyle createTextFormat(SXSSFWorkbook workbook) {
    DataFormat fmt = workbook.createDataFormat();
    CellStyle textStyle = workbook.createCellStyle();
    textStyle.setDataFormat(fmt.getFormat("@"));
    return textStyle;
}

// then do both:
int columnIndex = 0;
sheet.setDefaultColumnStyle(columnIndex, textFormat)

SXSSFCell cell = row.createCell(0);
cell.setCellStyle(textFormat);
cell.setCellValue("0100");

Probably this bug is causing you headaches.可能这个错误让你头疼。 Trying your code with Apache POI 3.7 I get the added effect that the second column gets hidden (width = 0) and the format is not applied.使用 Apache POI 3.7 尝试您的代码,我得到的附加效果是第二列被隐藏(宽度 = 0)并且格式未应用。

Cheers, Wim干杯,维姆

PS Note that I talk about the second column, which is what your code is really referring to; PS 请注意,我谈论的是第二列,这是您的代码真正指的是什么; if you wanted to apply the style to the first column you should have used 0 (zero-based).如果您想将样式应用于第一列,您应该使用 0(从零开始)。

Try this it will work:试试这个它会起作用:

       style.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM