org.apache.poi.hssf.usermodel.HSSFSheet.setColumnWidth()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(231)

本文整理了Java中org.apache.poi.hssf.usermodel.HSSFSheet.setColumnWidth()方法的一些代码示例,展示了HSSFSheet.setColumnWidth()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HSSFSheet.setColumnWidth()方法的具体详情如下:
包路径:org.apache.poi.hssf.usermodel.HSSFSheet
类名称:HSSFSheet
方法名:setColumnWidth

HSSFSheet.setColumnWidth介绍

[英]Set the width (in units of 1/256th of a character width)

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 (first font in the workbook).

Character width is defined as the maximum digit width of the numbers 0, 1, 2, ... 9 as rendered using the default font (first font in the workbook).
Unless you are using a very special font, the default character is '0' (zero), this is true for Arial (default font font in HSSF) and Calibri (default font in XSSF)

Please note, that the width set by this method includes 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines (Section 3.3.1.12 of the OOXML spec). This results is a slightly less value of visible characters than passed to this method (approx. 1/2 of a character).

To compute the actual number of visible characters, Excel uses the following formula (Section 3.3.1.12 of the OOXML spec):
width = Truncate([{Number of Visible Characters} {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256

Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi). If you set a column width to be eight characters wide, e.g. setColumnWidth(columnIndex, 8*256), then the actual value of visible characters (the value shown in Excel) is derived from the following equation: Truncate([numChars*7+5]/7*256)/256 = 8; which gives 7.29.
[中]设置宽度(以字符宽度的1/256为单位)
单个单元格的最大列宽为255个字符。此值表示使用标准字体(工作簿中的第一种字体)格式化的单元格中可以显示的字符数。
字符宽度定义为使用默认字体(工作簿中的第一种字体)呈现的数字0, 1, 2, ... 9的最大数字宽度。
除非您使用的是非常特殊的字体,否则默认字符为“0”(零),对于Arial(HSSF中的默认字体)和Calibri(XSSF中的默认字体)都是如此
请注意,此方法设置的宽度包括4个像素的边距填充(每侧两个),加上网格线的1个像素填充(OOXML规范第3.3.1.12节)。与传递给此方法的可见字符值相比,此结果的可见字符值稍小(约为字符的1/2)。
为了计算可见字符的实际数量,Excel使用以下公式(OOXML规范第3.3.1.12节):
width = Truncate([{Number of Visible Characters} {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256
以Calibri字体为例,11点字体大小的最大数字宽度为7像素(96 dpi)。如果将列宽设置为八个字符宽,例如:setColumnWidth(columnIndex, 8*256),则可见字符的实际值(Excel中显示的值)可从以下公式推导得出:Truncate([numChars*7+5]/7*256)/256 = 8;,该公式给出7.29

代码示例

代码示例来源:origin: org.apache.poi/poi

/**
 * 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));
  }
}

代码示例来源:origin: org.openl.rules/org.openl.lib.poi.dev

/**
 * @deprecated (Sep 2008) use {@link #setColumnWidth(int, int)}
 */
public void setColumnWidth(short columnIndex, short width) {
  setColumnWidth(columnIndex & 0xFFFF, width & 0xFFFF);
}

代码示例来源:origin: qcadoo/mes

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);
}

代码示例来源:origin: com.haulmont.thirdparty/poi

/**
 * @deprecated (Sep 2008) use {@link #setColumnWidth(int, int)}
 */
public void setColumnWidth(short columnIndex, short width) {
  setColumnWidth(columnIndex & 0xFFFF, width & 0xFFFF);
}

代码示例来源:origin: jasperreports/jasperreports

protected void setColumnWidth(int col, int width)
{
  sheet.setColumnWidth(col, width);
}

代码示例来源:origin: qcadoo/mes

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);
}

代码示例来源:origin: qcadoo/mes

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);
  }
}

代码示例来源:origin: org.motechproject/motech-bulk-export-import

private void setColumnWidths(List<ExcelColumn> columns) {
    int columnIndex = 0;
    for (ExcelColumn column : columns) {
      sheet.setColumnWidth(columnIndex, column.getWidth());
      columnIndex++;
    }
  }
}

代码示例来源:origin: com.haulmont.yarg/yarg

@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);
        }
      }
    }
  }
}

代码示例来源:origin: cuba-platform/yarg

@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);
        }
      }
    }
  }
}

代码示例来源:origin: riotfamily/riot

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));			
}

代码示例来源:origin: ckpoint/CheckPoint

/**
 * 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;
}

代码示例来源:origin: bedatadriven/activityinfo

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++;
  }
}

代码示例来源:origin: com.haulmont.yarg/yarg

@Override
  public void apply() {
    for (DataObject dataObject : data) {
      dataObject.resultCell.getSheet().setColumnWidth(dataObject.resultCell.getColumnIndex(), dataObject.templateCell.getSheet().getColumnWidth(dataObject.templateCell.getColumnIndex()));
    }
  }
}

代码示例来源:origin: cuba-platform/yarg

@Override
  public void apply() {
    for (DataObject dataObject : data) {
      dataObject.resultCell.getSheet().setColumnWidth(dataObject.resultCell.getColumnIndex(),
          dataObject.templateCell.getSheet().getColumnWidth(dataObject.templateCell.getColumnIndex()));
    }
  }
}

代码示例来源:origin: org.apache.poi/poi-examples

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 );
}

代码示例来源:origin: TomasKypta/android-lang-tool

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));
}

代码示例来源:origin: TomasKypta/android-lang-tool

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));
}

代码示例来源:origin: org.apache.poi/poi-examples

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);
      }
    }
  }
}

代码示例来源:origin: org.apache.poi/poi-examples

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);
}

相关文章

微信公众号

最新文章

更多

HSSFSheet类方法