/** * 设置某列为自动宽度,不考虑合并单元格<br> * 此方法必须在指定列数据完全写出后调用才有效。 * * @param columnIndex 第几列,从0计数 * @return this * @since 4.0.12 */ public ExcelWriter autoSizeColumn(int columnIndex) { this.sheet.autoSizeColumn(columnIndex); return this; }
/** * 设置某列为自动宽度,不考虑合并单元格<br> * 此方法必须在指定列数据完全写出后调用才有效。 * * @param columnIndex 第几列,从0计数 * @return this * @since 4.0.12 */ public ExcelWriter autoSizeColumn(int columnIndex) { this.sheet.autoSizeColumn(columnIndex); return this; }
/** * 设置某列为自动宽度<br> * 此方法必须在指定列数据完全写出后调用才有效。 * * @param columnIndex 第几列,从0计数 * @param useMergedCells 是否适用于合并单元格 * @return this * @since 3.3.0 */ public ExcelWriter autoSizeColumn(int columnIndex, boolean useMergedCells) { this.sheet.autoSizeColumn(columnIndex, useMergedCells); return this; }
/** * 设置某列为自动宽度<br> * 此方法必须在指定列数据完全写出后调用才有效。 * * @param columnIndex 第几列,从0计数 * @param useMergedCells 是否适用于合并单元格 * @return this * @since 3.3.0 */ public ExcelWriter autoSizeColumn(int columnIndex, boolean useMergedCells) { this.sheet.autoSizeColumn(columnIndex, useMergedCells); return this; }
@Override public void export(FacesContext context, String filename, List<DataTable> tables, boolean pageOnly, boolean selectionOnly, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor, ExporterOptions options, MethodExpression onTableRender) throws IOException { Workbook wb = createWorkBook(); if (preProcessor != null) { preProcessor.invoke(context.getELContext(), new Object[]{wb}); } for (int i = 0; i < tables.size(); i++) { DataTable table = tables.get(i); String sheetName = getSheetName(context, table); if (sheetName == null) { sheetName = table.getId(); } sheetName = WorkbookUtil.createSafeSheetName(sheetName); if (sheetName.equals("empty") || sheetName.equals("null")) { sheetName = "Sheet" + String.valueOf(i + 1); } Sheet sheet = createSheet(wb, sheetName); applyOptions(wb, table, sheet, options); exportTable(context, table, sheet, pageOnly, selectionOnly); for (int j = 0; j < table.getColumnsCount(); j++) { sheet.autoSizeColumn((short) j); } } if (postProcessor != null) { postProcessor.invoke(context.getELContext(), new Object[]{wb}); } writeExcelToResponse(context.getExternalContext(), wb, filename); }
@Override public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor, ExporterOptions options, MethodExpression onTableRender) throws IOException { Workbook wb = createWorkBook(); String sheetName = getSheetName(context, table); if (sheetName == null) { sheetName = table.getId(); } sheetName = WorkbookUtil.createSafeSheetName(sheetName); if (sheetName.equals("empty") || sheetName.equals("null")) { sheetName = "Sheet"; } Sheet sheet = createSheet(wb, sheetName); if (preProcessor != null) { preProcessor.invoke(context.getELContext(), new Object[]{wb}); } applyOptions(wb, table, sheet, options); exportTable(context, table, sheet, pageOnly, selectionOnly); for (int i = 0; i < table.getColumnsCount(); i++) { sheet.autoSizeColumn((short) i); } if (postProcessor != null) { postProcessor.invoke(context.getELContext(), new Object[]{wb}); } writeExcelToResponse(context.getExternalContext(), wb, filename); }
data.sheet.autoSizeColumn( i + data.startingCol ); data.sheet.autoSizeColumn( i + data.startingCol );
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); Sheet sheet = wb.createSheet(); Row row = sheet.createRow(2); Cell cell = row.createCell(2); cell.setCellValue("Use \n with word wrap on to create a new line"); //to enable newlines you need set a cell styles with wrap=true CellStyle cs = wb.createCellStyle(); cs.setWrapText(true); cell.setCellStyle(cs); //increase row height to accomodate two lines of text row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints())); //adjust column width to fit the content sheet.autoSizeColumn((short)2); FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx"); wb.write(fileOut); fileOut.close();
/** * 设置某列为自动宽度,不考虑合并单元格<br> * 此方法必须在指定列数据完全写出后调用才有效。 * * @param columnIndex 第几列,从0计数 * @return this * @since 4.0.12 */ public ExcelWriter autoSizeColumn(int columnIndex) { this.sheet.autoSizeColumn(columnIndex); return this; }
@Override public void accept(final Sheet sheet) { sheet.autoSizeColumn(this.value, this.ismerged); } }
/** * 设置某列为自动宽度<br> * 此方法必须在指定列数据完全写出后调用才有效。 * * @param columnIndex 第几列,从0计数 * @param useMergedCells 是否适用于合并单元格 * @return this * @since 3.3.0 */ public ExcelWriter autoSizeColumn(int columnIndex, boolean useMergedCells) { this.sheet.autoSizeColumn(columnIndex, useMergedCells); return this; }
Sheet sheet = workbook.getSheetAt(0); sheet.autoSizeColumn(0); //adjust width of the first column sheet.autoSizeColumn(1); //adjust width of the second column
private void setColumnLabels(Sheet sheet) { Row headerRow = sheet.createRow(0); for (int i = 0; i < names.size(); i++) { Cell cell = headerRow.createCell(i); String label = names.get(i); if (labels != null && labels.containsKey(label)) label = labels.get(label); cell.setCellValue(label); if (!"message".equals(label)) sheet.autoSizeColumn(i); } }
public ExcelSheet autoSizeColumn(int startCol, int endCol) { if (currentSheet instanceof SXSSFSheet) { ((SXSSFSheet) currentSheet).trackAllColumnsForAutoSizing(); } IntStream.rangeClosed(startCol, endCol).forEach(i -> currentSheet.autoSizeColumn(i)); return this; }
private static final int WIDTH_ARROW_BUTTON = 1300; private void autosizeColumnsFromSheet(final Sheet excelSheet, final int fromColumn, final int toColumn) { for (int i = fromColumn; i <= toColumn; i++) { excelSheet.autoSizeColumn(new Short(String.valueOf(i))); try { excelSheet.setColumnWidth(i, excelSheet.getColumnWidth(i) + WIDTH_ARROW_BUTTON); } catch (final Exception e) { // don't do anything - just let autosize handle it } } }
public static void autoSizeColumn(Sheet sheet){ if(sheet.getLastRowNum()>0){ Row row = sheet.getRow(0); int cols = row.getLastCellNum(); for(int i=0;i<cols;i++){ sheet.autoSizeColumn((short)i); //调整第一列宽度 } } }
private static void adjustColumnWidth(Sheet sheet, int lastColumn) { assert sheet != null; for (int i = 0; i <= lastColumn; i++) { sheet.autoSizeColumn(i); int width = sheet.getColumnWidth(i); if (width < MINIMUM_COLUMN_WIDTH) { sheet.setColumnWidth(i, MINIMUM_COLUMN_WIDTH); } } }
private void adjustColumnWidth(Sheet sheet, CellPosition end) { for (int col = getStartColnum(); col <= end.getCol(); col++) { sheet.autoSizeColumn(col, true); } } }
public static void autoSizeColumns(Workbook workbook) { int sheetCount = workbook.getNumberOfSheets(); for (int i = 0; i < sheetCount; i++) { Sheet sheet = workbook.getSheetAt(i); int firstRowNum = sheet.getFirstRowNum(); if (firstRowNum >= 0) { Row firstRow = sheet.getRow(firstRowNum); for (int cellnum = firstRow.getFirstCellNum(); cellnum < firstRow.getLastCellNum(); cellnum++) sheet.autoSizeColumn(cellnum); } } }
private static void autoSizeColumns(Sheet sheet, int columnNumber) { for (int i = 0; i < columnNumber; i++) { int orgWidth = sheet.getColumnWidth(i); sheet.autoSizeColumn(i, true); int newWidth = (int) (sheet.getColumnWidth(i) + 100); if (newWidth > orgWidth) { sheet.setColumnWidth(i, newWidth); } else { sheet.setColumnWidth(i, orgWidth); } } }