How to set fixed column width in Apache POI

setColumnWidth(int, int) should work ... is it because you reset the sizes to auto in your loop?

for (int i = 0; i < headers.length; i++) {
    sheet.autoSizeColumn(i);
}

Start your loop from 1 to headers.length instead.


You can set the column width using setColumnWidth method of XSSFWorkbook. The 1st parameter is the column number (starts from zero) and the 2nd parameter is the width. We need to be little tricky here to set the width. To set the width as 25 we need to pass the parameter as 25 * 256.

XSSFSheet sheet = workbook.createSheet("MySheet");
sheet.setColumnWidth(3, 25 * 256);