protected void setSizeColumnsWithHeader(int width) { for (int i = 0; i < headerCellCount; i++) { currentSheet.setColumnWidth(i, width); } } }
/** * Set the width (in units of 1/256th of a character width) * <p> * The maximum column width for an individual cell is 255 characters. * This value represents the number of characters that can be displayed * in a cell that is formatted with the standard font. * </p> * * @param columnIndex - the column to set (0-based) * @param width - the width in units of 1/256th of a character width */ @Override public void setColumnWidth(int columnIndex, int width) { _sh.setColumnWidth(columnIndex,width); }
/** * Adjusts the column width to fit the contents. * <p> * This process can be relatively slow on large sheets, so this should * normally only be called once per column, at the end of your * processing. * </p> * You can specify whether the content of merged cells should be considered or ignored. * Default is to ignore merged cells. * * @param column the column index * @param useMergedCells whether to use the contents of merged cells when calculating the width of the column */ @Override public void autoSizeColumn(int column, boolean useMergedCells) { double width = SheetUtil.getColumnWidth(this, column, useMergedCells); if (width != -1) { width *= 256; int maxColumnWidth = 255*256; // The maximum column width for an individual cell is 255 characters if (width > maxColumnWidth) { width = maxColumnWidth; } setColumnWidth(column, (int)(width)); columnHelper.setColBestFit(column, true); } }
private void writeDaySheet(LocalDate day, List<Timeslot> timeslotList, List<Talk> talkList) { nextSheet(DAY_FORMATTER.format(day), 1, 1, true); nextRow(); nextHeaderCell(DAY_FORMATTER.format(day)); writeTimeslotHoursVertically(timeslotList); List<Room> dayRoomList = talkList.stream().map(Talk::getRoom).distinct().collect(toList()); dayRoomList.sort(Comparator.comparing(Room::getName)); for (Room room : dayRoomList) { currentColumnNumber++; currentRowNumber = -1; nextCellVertically().setCellValue(room.getName()); List<Talk> roomTalkList = talkList.stream() .filter(talk -> talk.getRoom() == room) .collect(toList()); writeRoomTalks(timeslotList, room, roomTalkList); } currentSheet.autoSizeColumn(0); for (int i = 1; i < currentSheet.getRow(0).getPhysicalNumberOfCells(); i++) { currentSheet.setColumnWidth(i, 15 * 256); } }
currentSheet.setColumnWidth(i, 20 * 256);
currentSheet.setColumnWidth(i, 20 * 256);
protected void setSizeColumnsWithHeader(int width) { for (int i = 0; i < headerCellCount; i++) { currentSheet.setColumnWidth(i, width); } } }
private static void setColumnsWidths(XSSFSheet sheet) { sheet.setColumnWidth(0, 5250); sheet.setColumnWidth(1, 6000); sheet.setColumnWidth(2, 5000); sheet.setColumnWidth(3, 6250); sheet.setColumnWidth(4, 10000); } }
/** * Set the width (in units of 1/256th of a character width) * <p> * The maximum column width for an individual cell is 255 characters. * This value represents the number of characters that can be displayed * in a cell that is formatted with the standard font. * </p> * * @param columnIndex - the column to set (0-based) * @param width - the width in units of 1/256th of a character width */ public void setColumnWidth(int columnIndex, int width) { _sh.setColumnWidth(columnIndex,width); }
/** * Set the width (in units of 1/256th of a character width) * <p> * The maximum column width for an individual cell is 255 characters. * This value represents the number of characters that can be displayed * in a cell that is formatted with the standard font. * </p> * * @param columnIndex - the column to set (0-based) * @param width - the width in units of 1/256th of a character width */ @Override public void setColumnWidth(int columnIndex, int width) { _sh.setColumnWidth(columnIndex,width); }
/** * Adjusts the column width to fit the contents. * <p> * This process can be relatively slow on large sheets, so this should * normally only be called once per column, at the end of your * processing. * </p> * You can specify whether the content of merged cells should be considered or ignored. * Default is to ignore merged cells. * * @param column the column index * @param useMergedCells whether to use the contents of merged cells when calculating the width of the column */ @Override public void autoSizeColumn(int column, boolean useMergedCells) { double width = SheetUtil.getColumnWidth(this, column, useMergedCells); if (width != -1) { width *= 256; int maxColumnWidth = 255*256; // The maximum column width for an individual cell is 255 characters if (width > maxColumnWidth) { width = maxColumnWidth; } setColumnWidth(column, (int)(width)); columnHelper.setColBestFit(column, true); } }
/** * Adjusts the column width to fit the contents. * <p> * This process can be relatively slow on large sheets, so this should * normally only be called once per column, at the end of your * processing. * </p> * You can specify whether the content of merged cells should be considered or ignored. * Default is to ignore merged cells. * * @param column the column index * @param useMergedCells whether to use the contents of merged cells when calculating the width of the column */ public void autoSizeColumn(int column, boolean useMergedCells) { double width = SheetUtil.getColumnWidth(this, column, useMergedCells); if (width != -1) { width *= 256; int maxColumnWidth = 255*256; // The maximum column width for an individual cell is 255 characters if (width > maxColumnWidth) { width = maxColumnWidth; } setColumnWidth(column, (int)(width)); columnHelper.setColBestFit(column, true); } }
private void writeDaySheet(LocalDate day, List<Timeslot> timeslotList, List<Talk> talkList) { nextSheet(DAY_FORMATTER.format(day), 1, 1, true); nextRow(); nextHeaderCell(DAY_FORMATTER.format(day)); writeTimeslotHoursVertically(timeslotList); List<Room> dayRoomList = talkList.stream().map(Talk::getRoom).distinct().collect(toList()); dayRoomList.sort(Comparator.comparing(Room::getName)); for (Room room : dayRoomList) { currentColumnNumber++; currentRowNumber = -1; nextCellVertically().setCellValue(room.getName()); List<Talk> roomTalkList = talkList.stream() .filter(talk -> talk.getRoom() == room) .collect(toList()); writeRoomTalks(timeslotList, room, roomTalkList); } currentSheet.autoSizeColumn(0); for (int i = 1; i < currentSheet.getRow(0).getPhysicalNumberOfCells(); i++) { currentSheet.setColumnWidth(i, 15 * 256); } }
s.setColumnWidth(0, 70 * 256); s.setColumnWidth(1, 20 * 256); s.setColumnWidth(2, 10 * 256); s.setColumnWidth(3, 255 * 256);
for (int i = 0; i < days.length; i++) { sheet.setColumnWidth(i * 2, 5 * 256); //the column is 5 characters wide sheet.setColumnWidth(i * 2 + 1, 13 * 256); //the column is 13 characters wide sheet.addMergedRegion(new CellRangeAddress(1, 1, i * 2, i * 2 + 1)); XSSFCell monthCell = monthRow.createCell(i * 2);
@Override public void createExcelFromTable(File file, List<E> components) throws Exception { componentTableModel.setComponents(components); XSSFWorkbook workbook = new XSSFWorkbook(); // Feuille XSSFSheet tableSheet = workbook.createSheet(SwingMessages.getString("TableFileWriter.11")); createExcelHeader(workbook, tableSheet); createExcelData(workbook, tableSheet, components); tableSheet.createFreezePane(0, 1); tableSheet.setAutoFilter(new CellRangeAddress(0, tableSheet.getLastRowNum(), 0, table.getColumnCount() - 1)); for (int i = 0; i < table.getColumnCount(); i++) { // tableSheet.autoSizeColumn(i); tableSheet.setColumnWidth(i, 20 * 256); } FileOutputStream out = null; try { out = new FileOutputStream(file); workbook.write(out); } catch (IOException e) { throw e; } finally { if (out != null) { out.close(); } } }
tableSheet.setColumnWidth(i, 20 * 256);
currentSheet.setColumnWidth(i, 20 * 256);
currentSheet.setColumnWidth(i, 20 * 256);
public static void main(String[] args) throws IOException { try (XSSFWorkbook wb = new XSSFWorkbook()) { XSSFSheet sheet = wb.createSheet(); XSSFRow row = sheet.createRow(2); row.setHeightInPoints(30); for (int i = 0; i < 8; i++) { //column width is set in units of 1/256th of a character width sheet.setColumnWidth(i, 256 * 15); } createCell(wb, row, 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM); createCell(wb, row, 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM); createCell(wb, row, 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER); createCell(wb, row, 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER); createCell(wb, row, 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY); createCell(wb, row, 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP); createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP); //center text over B4, C4, D4 row = sheet.createRow(3); centerAcrossSelection(wb, row, 1, 3, VerticalAlignment.CENTER); // Write the output to a file try (OutputStream fileOut = new FileOutputStream("xssf-align.xlsx")) { wb.write(fileOut); } } }