Example usage for org.apache.poi.xssf.usermodel XSSFSheet getCTWorksheet

List of usage examples for org.apache.poi.xssf.usermodel XSSFSheet getCTWorksheet

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFSheet getCTWorksheet.

Prototype

@Internal
public CTWorksheet getCTWorksheet() 

Source Link

Document

Provide access to the CTWorksheet bean holding this sheet's data

Usage

From source file:org.tiefaces.components.websheet.utility.WebSheetUtility.java

License:MIT License

/**
 * return the last column of the sheet./*ww w. j  ava2s . com*/
 *
 * @param xsheet
 *            the xsheet
 * @return last column number (A column will return 0).
 */
private static int getSheetRightColFromDimension(final XSSFSheet xsheet) {
    CTSheetDimension dimension = xsheet.getCTWorksheet().getDimension();
    String sheetDimensions = dimension.getRef();
    if (sheetDimensions.indexOf(':') < 0) {
        return -1;
    } else {
        return CellRangeAddress.valueOf(sheetDimensions).getLastColumn();
    }
}

From source file:utils.ReadWriteExcelFile.java

public static void readXLSXFile(String aFile, int SheetNo) throws IOException {

    InputStream ExcelFileToRead = new FileInputStream(aFile);
    XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);

    XSSFWorkbook test = new XSSFWorkbook();

    XSSFSheet sheet = wb.getSheetAt(SheetNo);
    XSSFRow row;//  w  ww  .j  av a  2 s.c  om
    XSSFCell cell;
    CTSheetDimension dimension = sheet.getCTWorksheet().getDimension();
    String sheetDimensions = dimension.getRef();
    System.out.println(sheetDimensions);
    List<String> dimensions = StringUtils.split(sheetDimensions, ":", true);
    String[] Dimensions = dimensions.get(1).toString().split("(?<=\\D)(?=\\d)");
    int Colums = CharToInt(Dimensions[0]);
    int Rows = Integer.parseInt(Dimensions[1]);
    System.out.println();
    Iterator rows = sheet.rowIterator();
    ArrayList[][] TableName = new ArrayList[Rows][Colums];
    System.out.println(TableName.length);
    int currentRow = 0;
    while (rows.hasNext()) {

        row = (XSSFRow) rows.next();
        Iterator cells = row.cellIterator();
        int currentCell = 0;
        //         System.out.println("currentRow="+currentRow+" And Current Colum is ="+currentCell);
        while (cells.hasNext()) {
            cell = (XSSFCell) cells.next();

            if (cell.getStringCellValue().isEmpty()) {
                TableName[cell.getRowIndex()][cell.getColumnIndex()].add(" - ");
                //               System.out.print(cell.getStringCellValue()+" ");
                //                                        TableName[currentRow][currentCell].add(" ");
                //            System.out.println("Cell Value is : "+cell.toString());
                //                                System.out.println("Empty cell currentRow="+currentRow+" And Current Colum is ="+currentCell);

            } else if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                System.out.println("current Row=" + cell.getRowIndex() + " And Current Colum is ="
                        + cell.getColumnIndex());
                System.out.println(cell.getRichStringCellValue());
                TableName[cell.getRowIndex()][cell.getColumnIndex()]
                        .add(cell.getRichStringCellValue().toString());
                //                                    System.out.println("Cell Type is :"+cell.getCellType());
                //                                    System.out.println("Cell Value is : "+cell.getRichStringCellValue());
            } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
                TableName[cell.getRowIndex()][cell.getColumnIndex()].add(cell.getNumericCellValue());
                //                                    System.out.println("Cell Type is :"+cell.getCellType());
                //                                    System.out.println("current Row="+cell.getRowIndex()+" And Current Colum is ="+cell.getColumnIndex());
                //                                    int numericValue = (int) cell.getNumericCellValue();
                //                                    System.out.println("Cell Value is : "+numericValue);

            }
            //            else
            //            {
            //               //U Can Handel Boolean, Formula, Errors
            //            }
            currentCell++;
        }
        System.out.println();
        currentRow++;
    }
    for (int i = 0; TableName.length > i; i++) {
        for (int j = 0; TableName[i].length > j; j++) {
            System.out.println(TableName[i][j].toString());
        }

    }
}