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

x33g5p2x  于2022-01-29 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(421)

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

Sheet.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)。如果将列宽设置为八个字符宽,例如:[$2$],则可见字符的实际值(Excel中显示的值)可从以下公式推导得出:Truncate([numChars*7+5]/7*256)/256 = 8;,该公式给出[$4$]。

代码示例

代码示例来源:origin: alibaba/easyexcel

public static Sheet buildSheetStyle(Sheet currentSheet, Map<Integer, Integer> sheetWidthMap){
  currentSheet.setDefaultColumnWidth(20);
  for (Map.Entry<Integer, Integer> entry : sheetWidthMap.entrySet()) {
    currentSheet.setColumnWidth(entry.getKey(), entry.getValue());
  }
  return currentSheet;
}

代码示例来源:origin: looly/hutool

/**
 * 设置列宽(单位为一个字符的宽度,例如传入width为10,表示10个字符的宽度)
 * 
 * @param columnIndex 列号(从0开始计数,-1表示所有列的默认宽度)
 * @param width 宽度(单位1~256个字符宽度)
 * @return this
 * @since 4.0.8
 */
public ExcelWriter setColumnWidth(int columnIndex, int width) {
  if (columnIndex < 0) {
    this.sheet.setDefaultColumnWidth(width);
  } else {
    this.sheet.setColumnWidth(columnIndex, width * 256);
  }
  return this;
}

代码示例来源:origin: looly/hutool

/**
 * 设置列宽(单位为一个字符的宽度,例如传入width为10,表示10个字符的宽度)
 * 
 * @param columnIndex 列号(从0开始计数,-1表示所有列的默认宽度)
 * @param width 宽度(单位1~256个字符宽度)
 * @return this
 * @since 4.0.8
 */
public ExcelWriter setColumnWidth(int columnIndex, int width) {
  if (columnIndex < 0) {
    this.sheet.setDefaultColumnWidth(width);
  } else {
    this.sheet.setColumnWidth(columnIndex, width * 256);
  }
  return this;
}

代码示例来源:origin: stackoverflow.com

public class SO{
public static void main(String[] args) {

  try {
    FileInputStream is = new FileInputStream(new File("D:\\Users\\user2777005\\Desktop\\bob.xlsx"));
    XSSFWorkbook wb = new XSSFWorkbook(is);
    String header = "123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789";
    Sheet sheet = wb.getSheet("Sheet1");
    sheet.setColumnWidth(0, 18000);
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);

    if(header.length() > 50){ //Length of String for my test
      sheet.setColumnWidth(0, 18000); //Set column width, you'll probably want to tweak the second int
      CellStyle style = wb.createCellStyle(); //Create new style
      style.setWrapText(true); //Set wordwrap
      cell.setCellStyle(style); //Apply style to cell
      cell.setCellValue(header); //Write header
    }

    wb.write(new FileOutputStream(new File("D:\\Users\\user2777005\\Desktop\\bob.xlsx")));
  } catch (IOException e) {
    e.printStackTrace();
  }
}
}

代码示例来源:origin: br.com.objectos/xls

@Override
 void apachePOI(Sheet sheet, int colIndex, int size) {
  sheet.setColumnWidth(colIndex, 256 * size);
 }
};

代码示例来源:origin: br.com.objectos/way-io

@Override
 void apachePOI(Sheet sheet, int colIndex, int size) {
  sheet.setColumnWidth(colIndex, 256 * size);
 }
};

代码示例来源:origin: br.com.objectos/xls-core

@Override
 void apachePOI(Sheet sheet, int colIndex, int size) {
  sheet.setColumnWidth(colIndex, 256 * size);
 }
};

代码示例来源:origin: stackoverflow.com

public void autoSizeColumn(Sheet sheet, int column, int plusMinusChars) {
   double width = SheetUtil.getColumnWidth(sheet, column, false);
   if (width != -1) {
     width += plusMinusChars;
     width *= 256;
     int maxColumnWidth = 255*256; // The maximum column width for an individual cell is 255 characters
     if (width > maxColumnWidth) {
       width = maxColumnWidth;
     }
     sheet.setColumnWidth(column, (int)(width));
   }
 }

代码示例来源:origin: Vatavuk/excel-io

@Override
  public void accept(final Sheet sheet) {
    sheet.setColumnWidth(this.col, this.width);
  }
}

代码示例来源:origin: org.apache.solr/solr-cell

void setColWidth(int charWidth) {
 //width in poi is units of 1/256th of a character width for some reason
 this.sh.setColumnWidth(cellIndex - 1, 256*charWidth);
}

代码示例来源:origin: youseries/ureport

sheet.setColumnWidth(i,(short)colWidth);
  org.apache.poi.ss.usermodel.Cell cell = row.getCell(i);
  if(cell!=null){
sheet.setColumnWidth(i,(short)colWidth);
org.apache.poi.ss.usermodel.Cell cell = row.getCell(i);
if(cell!=null){

代码示例来源:origin: youseries/ureport

sheet.setColumnWidth(colNum,(short)colWidth);
org.apache.poi.ss.usermodel.Cell cell = row.getCell(colNum);
if(cell!=null){

代码示例来源:origin: youseries/ureport

sheet.setColumnWidth(colNum,(short)colWidth);
org.apache.poi.ss.usermodel.Cell cell = row.getCell(colNum);
if(cell!=null){

代码示例来源:origin: stackoverflow.com

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

代码示例来源:origin: xiaopotian1990/SpringBootExcel

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

代码示例来源:origin: net.sf.jxls/jxls-core

private void hideColumns(org.apache.poi.ss.usermodel.Workbook workbook) {
  if (columnsToHide != null) {
    for (int i = 0; i < columnsToHide.length; i++) {
      short column = columnsToHide[i];
      for (int sheet_no = 0; sheet_no < workbook.getNumberOfSheets(); sheet_no++) {
        org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(sheet_no);
        sheet.setColumnWidth(column, (int) 0);
      }
    }
  }
}

代码示例来源:origin: subtlelib/poi

@Override
public SheetContext setColumnWidth(int columnNumber, int width) {
  //TODO max col width in excel is 255 characters; check aforementioned condition with a test (whether our 1.1 doesn't reduce this upper boundary), add precondition check
  sheet.setColumnWidth(columnNumber, (int) (getConfiguration().getColumnWidthBaseValue() * width));
  return this;
}

代码示例来源:origin: SheetJS/jxls

private void hideColumns(org.apache.poi.ss.usermodel.Workbook workbook) {
  if (columnsToHide != null) {
    for (int i = 0; i < columnsToHide.length; i++) {
      short column = columnsToHide[i];
      for (int sheet_no = 0; sheet_no < workbook.getNumberOfSheets(); sheet_no++) {
        org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(sheet_no);
        sheet.setColumnWidth(column, (int) 0);
      }
    }
  }
}

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

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

代码示例来源:origin: pentaho/pentaho-reporting

protected void configureSheetColumnWidths( Sheet sheet, SlimSheetLayout sheetLayout, int columnCount ) {
 // Set column widths ..
 for ( int col = 0; col < columnCount; col++ ) {
  final double cellWidth = StrictGeomUtility.toExternalValue( sheetLayout.getCellWidth( col, col + 1 ) );
  final double poiCellWidth = ( cellWidth * getScaleFactor() );
  sheet.setColumnWidth( col, Math.min( 255 * 256, (int) poiCellWidth ) );
 }
}

相关文章

微信公众号

最新文章

更多