org.apache.poi.ss.usermodel.Sheet类的使用及代码示例

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

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

Sheet介绍

[英]High level representation of a Excel worksheet.

Sheets are the central structures within a workbook, and are where a user does most of his spreadsheet work. The most common type of sheet is the worksheet, which is represented as a grid of cells. Worksheet cells can contain text, numbers, dates, and formulas. Cells can also be formatted.
[中]Excel工作表的高级表示。
工作表是工作簿中的中心结构,是用户完成大部分电子表格工作的地方。最常见的工作表类型是工作表,它用单元格网格表示。工作表单元格可以包含文本、数字、日期和公式。单元格也可以格式化。

代码示例

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

InputStream inp = new FileInputStream("wb.xls");
 Workbook wb = WorkbookFactory.create(inp);
 Sheet sheet = wb.getSheetAt([sheet index]);
 Row row = sheet.getRow([row index]);
 Cell cell = row.getCell([cell index]);
 String cellContents = cell.getStringCellValue(); 
 //Modify the cellContents here
 // Write the output to a file
 cell.setCellValue(cellContents); 
 FileOutputStream fileOut = new FileOutputStream("wb.xls");
 wb.write(fileOut);
 fileOut.close();

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

Workbook wb = new HSSFWorkbook();
 //Workbook wb = new XSSFWorkbook();
 CreationHelper createHelper = wb.getCreationHelper();
 Sheet sheet = wb.createSheet("new sheet");
 // Create a row and put some cells in it. Rows are 0 based.
 Row row = sheet.createRow((short)0);
 // Create a cell and put a value in it.
 Cell cell = row.createCell(0);
 cell.setCellValue(1);
 // Or do it on one line.
 row.createCell(1).setCellValue(1.2);
 row.createCell(2).setCellValue(
    createHelper.createRichTextString("This is a string"));
 row.createCell(3).setCellValue(true);
 // Write the output to a file
 FileOutputStream fileOut = new FileOutputStream("workbook.xls");
 wb.write(fileOut);
 fileOut.close();

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

public KCell[] getRow( int rownr ) {
 if ( rownr < sheet.getFirstRowNum() ) {
  return new KCell[] {};
 } else if ( rownr > sheet.getLastRowNum() ) {
  throw new ArrayIndexOutOfBoundsException( "Read beyond last row: " + rownr );
 }
 Row row = sheet.getRow( rownr );
 if ( row == null ) { // read an empty row
  return new KCell[] {};
 }
 int cols = row.getLastCellNum();
 if ( cols < 0 ) { // this happens if a row has no cells, POI returns -1 then
  return new KCell[] {};
 }
 PoiCell[] xlsCells = new PoiCell[cols];
 for ( int i = 0; i < cols; i++ ) {
  Cell cell = row.getCell( i );
  if ( cell != null ) {
   xlsCells[i] = new PoiCell( cell );
  }
 }
 return xlsCells;
}

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

/**
 * 
 * sheet是否为空
 * 
 * @param sheet {@link Sheet}
 * @return sheet是否为空
 * @since 4.0.1
 */
public static boolean isEmpty(Sheet sheet) {
  return null == sheet || (sheet.getLastRowNum() == 0 && sheet.getPhysicalNumberOfRows() == 0);
}

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

/**
 * 获取已有行或创建新行
 * 
 * @param sheet Excel表
 * @param rowIndex 行号
 * @return {@link Row}
 * @since 4.0.2
 */
public static Row getOrCreateRow(Sheet sheet, int rowIndex) {
  Row row = sheet.getRow(rowIndex);
  if (null == row) {
    row = sheet.createRow(rowIndex);
  }
  return row;
}

代码示例来源: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

String file = "c:\\poitest.xlsx";
FileOutputStream outputStream = new FileOutputStream(file);
Workbook wb = new XSSFWorkbook();

CellStyle unlockedCellStyle = wb.createCellStyle();
unlockedCellStyle.setLocked(false);

Sheet sheet = wb.createSheet();
sheet.protectSheet("password");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
cell.setCellStyle(unlockedCellStyle);

wb.write(outputStream);
outputStream.close();

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

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("format sheet");
CellStyle style;
DataFormat format = wb.createDataFormat();
Row row;
Cell cell;
short rowNum = 0;
short colNum = 0;

row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(style);

row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,##0.0000"));
cell.setCellStyle(style);

FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();

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

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

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

Workbook wb = WorkbookFactory.create(new File("myFile.xls")); // Or .xlsx
Sheet s = wb.getSheet(0);
Row r1 = s.getRow(0);
r1.createCell(4).setCellValue(4.5);
r1.createCell(5).setCellValue("Hello");
FileOutputStream out = new FileOutputStream("newFile.xls"); // Or .xlsx
wb.write(out);
out.close();

代码示例来源: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: org.apache.poi/poi-examples

public static void main(String[] args) throws IOException {
    try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
      Sheet sheet = wb.createSheet("new sheet");

      Row row = sheet.createRow((short) 1);
      Cell cell = row.createCell((short) 1);
      cell.setCellValue(new XSSFRichTextString("This is a test of merging"));

      sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));

      // Write the output to a file
      try (FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx")) {
        wb.write(fileOut);
      }
    }
  }
}

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

Drawing<?> drawing=sheet.createDrawingPatriarch();
List<Row> rows=page.getRows();
for(int rowIndex=0;rowIndex<rows.size();rowIndex++){
  Row r=rows.get(rowIndex);
  org.apache.poi.ss.usermodel.Row row = sheet.getRow(rowNumber);
  if(row==null){
    row=sheet.createRow(rowNumber);
    sheet.setColumnWidth(colNum,(short)colWidth);
    org.apache.poi.ss.usermodel.Cell cell = row.getCell(colNum);
    if(cell!=null){
      continue;
    cell=row.createCell(colNum);
    com.bstek.ureport.model.Cell cellInfo=null;
    if(colCell!=null){
      org.apache.poi.ss.usermodel.Row rr=sheet.getRow(j);
      if(rr==null){
        rr=sheet.createRow(j);
        cc.setCellStyle(style);
      sheet.addMergedRegion(cellRegion);
  rowNumber++;                    
sheet.setRowBreak(rowNumber-1);

代码示例来源:origin: spring-projects/spring-framework

@Override
  protected void buildExcelDocument(Map<String, Object> model, Workbook workbook,
      HttpServletRequest request, HttpServletResponse response) throws Exception {
    Sheet sheet = workbook.createSheet("Test Sheet");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("Test Value");
  }
};

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

Drawing<?> drawing=sheet.createDrawingPatriarch();
  List<Row> rows=page.getRows();
  for(Row r:rows){
    org.apache.poi.ss.usermodel.Row row = sheet.getRow(rowNumber);
    if(row==null){
      row=sheet.createRow(rowNumber);
      sheet.setColumnWidth(i,(short)colWidth);
      org.apache.poi.ss.usermodel.Cell cell = row.getCell(i);
      if(cell!=null){
        continue;
        org.apache.poi.ss.usermodel.Row rr=sheet.getRow(j);
        if(rr==null){
          rr=sheet.createRow(j);
        sheet.addMergedRegion(cellRegion);
  sheet.setRowBreak(rowNumber-1);
Drawing<?> drawing=sheet.createDrawingPatriarch();
List<Row> rows=report.getRows();
int rowNumber=0;
  org.apache.poi.ss.usermodel.Row row = sheet.getRow(rowNumber);
  if(row==null){
    row=sheet.createRow(rowNumber);
    sheet.setColumnWidth(i,(short)colWidth);

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

Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
Sheet sheet = wb.getSheetAt(0);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol()); 
      System.out.println(cell.getBooleanCellValue());
      break;
    case Cell.CELL_TYPE_NUMERIC:
      System.out.println(cell.getNumericCellValue());
      break;
    case Cell.CELL_TYPE_STRING:
      System.out.println(cell.getStringCellValue());
      break;
    case Cell.CELL_TYPE_BLANK:

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

Workbook wb = meta.getExtension().equalsIgnoreCase( "xlsx" ) ? new XSSFWorkbook() : new HSSFWorkbook();
 BufferedOutputStreamWithCloseDetection out = new BufferedOutputStreamWithCloseDetection( KettleVFS.getOutputStream( data.file, false ) );
 wb.createSheet( data.realSheetname );
 wb.write( out );
 out.close();
 wb.close();
XSSFWorkbook xssfWorkbook = new XSSFWorkbook( KettleVFS.getInputStream( data.file ) );
if ( meta.isStreamingData() && !meta.isTemplateEnabled() ) {
 data.wb = new SXSSFWorkbook( xssfWorkbook, 100 );
data.wb = new HSSFWorkbook( KettleVFS.getInputStream( data.file ) );
 data.wb.setSheetOrder( data.sheet.getSheetName(), replacingSheetAt );
if ( data.sheet.getPhysicalNumberOfRows() > 0 ) {
 data.posY = data.sheet.getLastRowNum();
 data.posY++;
Sheet templateSheet = ((XSSFWorkbook) data.wb).getSheet( data.realSheetname );
int currentRowNum = templateSheet.getLastRowNum();
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook( (XSSFWorkbook) data.wb, 100 );
Sheet aNewSheet = sxssfWorkbook.getSheet( data.realSheetname );
int aNewSheetRowCount = aNewSheet.getLastRowNum();
while ( currentRowNum > aNewSheetRowCount ) {
 templateSheet.removeRow( templateSheet.getRow( currentRowNum ) );
 currentRowNum--;

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

stepData.wb = stepMeta.getExtension().equalsIgnoreCase( "xlsx" ) ? new XSSFWorkbook() : new HSSFWorkbook();
stepData.sheet = stepData.wb.createSheet();
stepData.file = null;
stepData.clearStyleCache( numOfFields );
DataFormat format = stepData.wb.createDataFormat();
Row xlsRow = stepData.sheet.createRow( 0 );
Cell cell = xlsRow.createCell( 5 );
CellStyle cellStyle = stepData.wb.createCellStyle();
cellStyle.setBorderRight( BorderStyle.THICK );
cellStyle.setFillPattern( FillPatternType.FINE_DOTS );
cell.setCellStyle( cellStyle );
cellStyle = stepData.wb.createCellStyle();
cellStyle.cloneStyleFrom( cell.getCellStyle() );
cell = xlsRow.createCell( 6 );
cellStyle.setDataFormat( format.getFormat( "##0,000.0" ) );
cell.setCellStyle( cellStyle );

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings("resource")
public void testXlsxStreamingView() throws Exception {
  View excelView = new AbstractXlsxStreamingView() {
    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
      Sheet sheet = workbook.createSheet("Test Sheet");
      Row row = sheet.createRow(0);
      Cell cell = row.createCell(0);
      cell.setCellValue("Test Value");
    }
  };
  excelView.render(new HashMap<>(), request, response);
  Workbook wb = new XSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
  assertEquals("Test Sheet", wb.getSheetName(0));
  Sheet sheet = wb.getSheet("Test Sheet");
  Row row = sheet.getRow(0);
  Cell cell = row.getCell(0);
  assertEquals("Test Value", cell.getStringCellValue());
}

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

Workbook wb = WorkbookFactory.create(new File("existing.xls"));
CellStyle origStyle = wb.getCellStyleAt(1); // Or from a cell

Workbook newWB = new XSSFWorkbook();
Sheet sheet = newWB.createSheet();
Row r1 = sheet.createRow(0);
Cell c1 = r1.createCell(0);

CellStyle newStyle = newWB.createCellStyle();
newStyle.cloneStyleFrom(origStyle);
c1.setCellStyle(newStyle);

newWB.write(new FileOutpuStream("new.xlsx"));

相关文章

微信公众号

最新文章

更多