public static void setCellWrapText(XSSFWorkbook workbook, XSSFSheet workSheet, int startRow, int endRow, int startColumn, int endColumn, boolean isWrapText) { for (int i = startRow; i <= endRow; i++) { for (int j = startColumn; j <= endColumn; j++) { XSSFCell cell = getWorkSheetCell(workSheet, i, j); XSSFCellStyle style = getCellStyle(workbook, cell); style.setWrapText(isWrapText); cell.setCellStyle(style); } } }
public static XSSFCell getWorkSheetCell(XSSFSheet workSheet, int rowIndex, int cellIndex) { XSSFRow row = workSheet.getRow(rowIndex); if (row == null) { row = workSheet.createRow(rowIndex); } XSSFCell cell = row.getCell(cellIndex); if (cell == null) { cell = row.createCell(cellIndex); } return cell; }
private static XSSFCellStyle getCellStyle(XSSFWorkbook workbook, XSSFCell cell) { XSSFCellStyle style = cell.getCellStyle(); if (style == null) { style = workbook.createCellStyle(); return style; } else { XSSFCellStyle style1 = workbook.createCellStyle(); style1.cloneStyleFrom(style); return style1; } }