/** * 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)); } }
/** * @deprecated (Sep 2008) use {@link #setColumnWidth(int, int)} */ public void setColumnWidth(short columnIndex, short width) { setColumnWidth(columnIndex & 0xFFFF, width & 0xFFFF); }
private void setColumnsWidths(HSSFSheet sheet) { sheet.setColumnWidth(0, 5000); sheet.setColumnWidth(1, 4000); sheet.setColumnWidth(2, 3500); sheet.setColumnWidth(3, 3500); sheet.setColumnWidth(4, 5000); sheet.setColumnWidth(5, 6000); sheet.setColumnWidth(6, 6000); }
/** * @deprecated (Sep 2008) use {@link #setColumnWidth(int, int)} */ public void setColumnWidth(short columnIndex, short width) { setColumnWidth(columnIndex & 0xFFFF, width & 0xFFFF); }
protected void setColumnWidth(int col, int width) { sheet.setColumnWidth(col, width); }
private void setColumnsWidths(HSSFSheet sheet) { sheet.setColumnWidth(0, 5000); sheet.setColumnWidth(1, 3500); sheet.setColumnWidth(2, 3500); sheet.setColumnWidth(3, 4000); sheet.setColumnWidth(4, 4000); sheet.setColumnWidth(5, 2500); sheet.setColumnWidth(6, 5000); sheet.setColumnWidth(7, 5000); sheet.setColumnWidth(8, 4000); sheet.setColumnWidth(9, 4000); sheet.setColumnWidth(10, 4500); sheet.setColumnWidth(11, 5000); }
private void setColumnWidths(final HSSFSheet sheet, List<ReportColumn> columns) { int index = 0; for (ReportColumn column : columns) { sheet.setColumnWidth(index, column.getColumnWidth()); index++; } for (int columnNumber = columns.size(); columnNumber < columns.size() + 21; columnNumber++) { sheet.setColumnWidth(columnNumber, 6 * 256); } }
private void setColumnWidths(List<ExcelColumn> columns) { int columnIndex = 0; for (ExcelColumn column : columns) { sheet.setColumnWidth(columnIndex, column.getWidth()); columnIndex++; } } }
@Override public void apply() { for (DataObject dataObject : data) { HSSFCell resultCell = dataObject.resultCell; HSSFCell templateCell = dataObject.templateCell; String templateCellValue = templateCell.getStringCellValue(); Matcher matcher = pattern.matcher(templateCellValue); if (matcher.find()) { String paramName = matcher.group(1); Integer width = (Integer) dataObject.bandData.getParameterValue(paramName); if (width != null) { resultCell.getSheet().setColumnWidth(resultCell.getColumnIndex(), width); } } } } }
@Override public void apply() { for (DataObject dataObject : data) { HSSFCell resultCell = dataObject.resultCell; HSSFCell templateCell = dataObject.templateCell; String templateCellValue = templateCell.getStringCellValue(); Matcher matcher = pattern.matcher(templateCellValue); if (matcher.find()) { String paramName = matcher.group(1); Integer width = (Integer) dataObject.bandData.getParameterValue(paramName); if (width != null) { resultCell.getSheet().setColumnWidth(resultCell.getColumnIndex(), width); } } } } }
private void createRows(Collection<?> items) { int i = 1; for (Object item : items) { MessageBundleEntry entry = (MessageBundleEntry) item; HSSFRow row = sheet.createRow(i++); addCell(row, 0, getCategory(entry), editable); addCell(row, 1, entry.getCode(), locked); addCell(row, 2, entry.getDefaultText(), locked); addCell(row, 3, entry.getComment(), editable); } sheet.autoSizeColumn(0); sheet.setColumnWidth(1, (25 * 256)); sheet.setColumnWidth(2, (50 * 256)); sheet.setColumnWidth(3, (50 * 256)); }
/** * Create title cell cell. * * @param str the str * @param width the width * @return the cell */ public Cell createTitleCell(String str, double width) { int cellCnt = this.getCellCnt(); Cell cell = this.getLastRow().createCell(cellCnt); cell.setCellValue(str); cell.setCellType(CellType.STRING); cell.setCellStyle(this.style.getStringCs()); sheet.setColumnWidth(cellCnt, (int) (sheet.getColumnWidth(cellCnt) * width)); return cell; }
private void writeHeaders(HSSFSheet sheet, String title, List<XlsColumn> columns) { Cell titleCell = sheet.createRow(0).createCell(0); titleCell.setCellValue(book.getCreationHelper().createRichTextString(title)); titleCell.setCellStyle(titleStyle); Row columnHeaderRow = sheet.createRow(1); columnHeaderRow.setHeightInPoints(HEADER_CELL_HEIGHT); int columnIndex = 0; for (int i = 0; i < columns.size(); i++) { XlsColumn column = columns.get(i); Cell cell = columnHeaderRow.createCell(columnIndex); cell.setCellStyle(headerStyle); cell.setCellValue(column.getHeading()); sheet.setColumnWidth(columnIndex, width(column.getHeading())); columnIndex++; } }
@Override public void apply() { for (DataObject dataObject : data) { dataObject.resultCell.getSheet().setColumnWidth(dataObject.resultCell.getColumnIndex(), dataObject.templateCell.getSheet().getColumnWidth(dataObject.templateCell.getColumnIndex())); } } }
@Override public void apply() { for (DataObject dataObject : data) { dataObject.resultCell.getSheet().setColumnWidth(dataObject.resultCell.getColumnIndex(), dataObject.templateCell.getSheet().getColumnWidth(dataObject.templateCell.getColumnIndex())); } } }
private static void drawSheet2( HSSFSheet sheet2 ) { // Create a row and size one of the cells reasonably large. HSSFRow row = sheet2.createRow(2); row.createCell(1); row.setHeightInPoints(240); sheet2.setColumnWidth(2, 9000); // Create the drawing patriarch. This is the top level container for // all shapes. This will clear out any existing shapes for that sheet. HSSFPatriarch patriarch = sheet2.createDrawingPatriarch(); // Draw a grid in one of the cells. drawGrid( patriarch ); }
private static void createTilte(HSSFWorkbook wb, HSSFSheet sheet) { HSSFRow titleRow = sheet.getRow(0); HSSFCell cell = titleRow.createCell(0); cell.setCellStyle(createTilteStyle(wb)); cell.setCellValue("KEY"); sheet.setColumnWidth(cell.getColumnIndex(), (40 * 256)); }
private static void addLang2Tilte(HSSFWorkbook wb, HSSFSheet sheet, String lang) { HSSFRow titleRow = sheet.getRow(0); HSSFCell lastCell = titleRow.getCell((int)titleRow.getLastCellNum() - 1); if (lang.equals(lastCell.getStringCellValue())) { // language column already exists return; } HSSFCell cell = titleRow.createCell((int)titleRow.getLastCellNum()); cell.setCellStyle(createTilteStyle(wb)); cell.setCellValue(lang); sheet.setColumnWidth(cell.getColumnIndex(), (60 * 256)); }
public static void main( String[] args ) throws IOException { try (HSSFWorkbook wb = new HSSFWorkbook()) { HSSFSheet s = wb.createSheet(); HSSFFont f2 = wb.createFont(); HSSFCellStyle cs = wb.createCellStyle(); cs.setFont(f2); // Word Wrap MUST be turned on cs.setWrapText(true); HSSFRow r = s.createRow(2); r.setHeight((short) 0x349); HSSFCell c = r.createCell(2); c.setCellType(CellType.STRING); c.setCellValue("Use \n with word wrap on to create a new line"); c.setCellStyle(cs); s.setColumnWidth(2, (int) ((50 * 8) / ((double) 1 / 20))); try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) { wb.write(fileOut); } } } }
private static void drawSheet1( HSSFSheet sheet1 ) { // Create a row and size one of the cells reasonably large. HSSFRow row = sheet1.createRow(2); row.setHeight((short) 2800); row.createCell(1); sheet1.setColumnWidth(2, 9000); // Create the drawing patriarch. This is the top level container for // all shapes. HSSFPatriarch patriarch = sheet1.createDrawingPatriarch(); // Draw some lines and an oval. drawLinesToCenter( patriarch ); drawManyLines( patriarch ); drawOval( patriarch ); drawPolygon( patriarch ); // Draw a rectangle. HSSFSimpleShape rect = patriarch.createSimpleShape( new HSSFClientAnchor(100, 100, 900, 200, (short)0, 0, (short)0, 0) ); rect.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); }