protected void autoSizeColumnsWithHeader() { for (int i = 0; i < headerCellCount; i++) { currentSheet.autoSizeColumn(i); } }
/** * Adjusts the column width to fit the contents. * * 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. * * @param column the column index */ @Override public void autoSizeColumn(int column) { autoSizeColumn(column, false); }
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.autoSizeColumn(0); currentSheet.autoSizeColumn(1);
currentSheet.autoSizeColumn(0); for (int i = 1; i < headerCellCount; i++) { currentSheet.setColumnWidth(i, 20 * 256);
currentSheet.autoSizeColumn(0); for (int i = 1; i < headerCellCount; i++) { currentSheet.setColumnWidth(i, 20 * 256);
/** * Adjusts the column width to fit the contents. * * 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. * * @param column the column index */ public void autoSizeColumn(int column) { autoSizeColumn(column, false); }
protected void autoSizeColumnsWithHeader() { for (int i = 0; i < headerCellCount; i++) { currentSheet.autoSizeColumn(i); } }
/** * Adjusts the column width to fit the contents. * * 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. * * @param column the column index */ @Override public void autoSizeColumn(int column) { autoSizeColumn(column, false); }
private void autoSizeColumn(XSSFSheet sheet) { Row row = sheet.getRow(0); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); int columnIndex = cell.getColumnIndex(); sheet.autoSizeColumn(columnIndex); } }
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet spreadsheet = workbook.createSheet("PersonList"); XSSFRow row; for (Object obj : objectArr)// for (Person person : personService.listPersons()) { Cell cell = row.createCell(cellid++); cell.setCellValue((String)obj); } for(int columnIndex = 0; columnIndex < 24; columnIndex++) { spreadsheet.autoSizeColumn(columnIndex); } if(List.size()!=0) { FileOutputStream out; out = new FileOutputStream(new File(file_path)); workbook.write(out); out.close(); }
private static XSSFWorkbook createXSSFWorkbook(List<Map> list, String sheetName){ //Excel Workbook Create XSSFWorkbook wb = new XSSFWorkbook(); //Excel Sheet Create XSSFSheet sheet = wb.createSheet(sheetName); //Header Row Create Set columnSet = list.get(0).keySet(); createHeaderRow(wb, sheet, columnSet); //Row Columns Create int rowIndex = 1; for(Map map : list){ createDataRow(sheet, columnSet, map, rowIndex); rowIndex++; } //Auto Size Column int cellIndexForSize = 0; for(Object columnName : columnSet){ //Column Width Auto sheet.autoSizeColumn(cellIndexForSize, true); cellIndexForSize++; } return wb; }
@Override protected void endSerialize( RootNode rootNode, OutputStream outputStream ) throws Exception { int columns = sheet.getRow( 0 ).getPhysicalNumberOfCells(); for ( int i = 0; i < columns; i++ ) { sheet.autoSizeColumn( i ); } workbook.write( outputStream ); }
sheet.autoSizeColumn(0); sheet.autoSizeColumn(1); sheet.autoSizeColumn(3);
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); } }
@Override public void generateTitleCell(@Nonnull OutputTableContext ctx, @Nonnull Column column, int rowSpan, boolean asSubTitle, boolean columnWithSeparator) { if (column.getTitle() == null) { return; } incrementColumn(); while (containsRowSpanForColumn()) { incrementColumn(); if (containsRowSpanForColumn()) { columnRowSpanMap.put(columnIndex, columnRowSpanMap.get(columnIndex) - 1); } } XSSFCell cell = row.createCell(columnIndex); cell.setCellValue(column.getTitle()); if (rowSpan > 1) { int spanOffset = rowSpan - 1; columnRowSpanMap.put(columnIndex, spanOffset); viewOutputExcel.getOutput().addMergedRegion(new CellRangeAddress(rowIndex, rowIndex + spanOffset, columnIndex, columnIndex)); } viewOutputExcel.getOutput().autoSizeColumn(cell.getColumnIndex()); }
// initialize objects XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet spreadsheet = workbook.createSheet(sheetName); XSSFRow row = spreadsheet.createRow(0); XSSFCell cell; // font/style XSSFFont font = workbook.createFont(); font.setFontName("Arial"); XSSFCellStyle style = workbook.createCellStyle(); style.setFont(font); // create/set cell & style cell = row.createCell(0); cell.setCellValue("New Cell"); cell.setCellStyle(style); // auto size spreadsheet.autoSizeColumn(0); // create file File aFile = new File("Your Filename"); FileOutputStream out = new FileOutputStream(aFile); workbook.write(out);
for (columnIndex = 0; columnIndex < table.getLastHeaderRow() .getColumns(ReservedFormat.ALL, ReservedFormat.XLSX).size(); columnIndex++) { sheet.autoSizeColumn(columnIndex);
@Override public void generateCell(@Nonnull OutputCellContext ctx) { XSSFCell cell = incrementColumnAndCreateNewCell(); configAlignment(cell.getCellStyle(), ctx.getColumn().getAlignment()); if (ctx.getValue() == null) { return; } switch (ctx.getCell().getColumn().getType()) { case INTEGER: case NUMBER: case MONEY: cell.setCellValue(((Number) ctx.getValue()).doubleValue()); break; case STRING: cell.setCellValue((String) ctx.getValue()); break; default: cell.setCellValue(ctx.generateFormatDisplayString()); break; } viewOutputExcel.getOutput().autoSizeColumn(cell.getColumnIndex()); }