Example usage for org.apache.poi.ss.usermodel Name getSheetName

List of usage examples for org.apache.poi.ss.usermodel Name getSheetName

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Name getSheetName.

Prototype

String getSheetName();

Source Link

Document

Get the sheets name which this named range is referenced to

Usage

From source file:com.miraisolutions.xlconnect.Workbook.java

License:Open Source License

private boolean hasValidWorkSheet(Name region) {
    return (region.getSheetName() != null && !"".equals(region.getSheetName()));
}

From source file:com.miraisolutions.xlconnect.Workbook.java

License:Open Source License

public void writeNamedRegion(DataFrame data, String name, boolean header) {
    Name cname = getName(name);
    checkName(cname);//www .j a v  a  2 s. com

    // Get sheet where name is defined in
    Sheet sheet = workbook.getSheet(cname.getSheetName());

    AreaReference aref = new AreaReference(cname.getRefersToFormula());
    // Get upper left corner
    CellReference topLeft = aref.getFirstCell();

    // Compute bottom right cell coordinates
    int bottomRightRow = Math.max(topLeft.getRow() + data.rows() - 1, topLeft.getRow());
    if (header)
        ++bottomRightRow;
    int bottomRightCol = Math.max(topLeft.getCol() + data.columns() - 1, topLeft.getCol());
    // Create bottom right cell reference
    CellReference bottomRight = new CellReference(sheet.getSheetName(), bottomRightRow, bottomRightCol, true,
            true);

    // Define named range area
    aref = new AreaReference(topLeft, bottomRight);
    // Redefine named range
    cname.setRefersToFormula(aref.formatAsString());

    writeData(data, sheet, topLeft.getRow(), topLeft.getCol(), header);
}

From source file:com.miraisolutions.xlconnect.Workbook.java

License:Open Source License

public DataFrame readNamedRegion(String name, boolean header, ReadStrategy readStrategy, DataType[] colTypes,
        boolean forceConversion, String dateTimeFormat, boolean takeCached, int[] subset) {
    Name cname = getName(name);
    checkName(cname);/*  w  w w  . j ava  2  s  .  c o  m*/

    // Get sheet where name is defined in
    Sheet sheet = workbook.getSheet(cname.getSheetName());

    AreaReference aref = new AreaReference(cname.getRefersToFormula());
    // Get name corners (top left, bottom right)
    CellReference topLeft = aref.getFirstCell();
    CellReference bottomRight = aref.getLastCell();

    // Determine number of rows and columns
    int nrows = bottomRight.getRow() - topLeft.getRow() + 1;
    int ncols = bottomRight.getCol() - topLeft.getCol() + 1;

    return readData(sheet, topLeft.getRow(), topLeft.getCol(), nrows, ncols, header, readStrategy, colTypes,
            forceConversion, dateTimeFormat, takeCached, subset);
}

From source file:com.miraisolutions.xlconnect.Workbook.java

License:Open Source License

public void addImage(File imageFile, String name, boolean originalSize)
        throws FileNotFoundException, IOException {
    Name cname = getName(name);

    // Get sheet where name is defined in
    Sheet sheet = workbook.getSheet(cname.getSheetName());

    AreaReference aref = new AreaReference(cname.getRefersToFormula());
    // Get name corners (top left, bottom right)
    CellReference topLeft = aref.getFirstCell();
    CellReference bottomRight = aref.getLastCell();

    // Determine image type
    int imageType;
    String filename = imageFile.getName().toLowerCase();
    if (filename.endsWith("jpg") || filename.endsWith("jpeg")) {
        imageType = org.apache.poi.ss.usermodel.Workbook.PICTURE_TYPE_JPEG;
    } else if (filename.endsWith("png")) {
        imageType = org.apache.poi.ss.usermodel.Workbook.PICTURE_TYPE_PNG;
    } else if (filename.endsWith("wmf")) {
        imageType = org.apache.poi.ss.usermodel.Workbook.PICTURE_TYPE_WMF;
    } else if (filename.endsWith("emf")) {
        imageType = org.apache.poi.ss.usermodel.Workbook.PICTURE_TYPE_EMF;
    } else if (filename.endsWith("bmp") || filename.endsWith("dib")) {
        imageType = org.apache.poi.ss.usermodel.Workbook.PICTURE_TYPE_DIB;
    } else if (filename.endsWith("pict") || filename.endsWith("pct") || filename.endsWith("pic")) {
        imageType = org.apache.poi.ss.usermodel.Workbook.PICTURE_TYPE_PICT;
    } else/*  w w  w.j av a 2  s  .  c  o m*/
        throw new IllegalArgumentException(
                "Image type \"" + filename.substring(filename.lastIndexOf('.') + 1) + "\" not supported!");

    InputStream is = new FileInputStream(imageFile);
    byte[] bytes = IOUtils.toByteArray(is);
    int imageIndex = workbook.addPicture(bytes, imageType);
    is.close();

    Drawing drawing;
    if (isHSSF()) {
        drawing = ((HSSFSheet) sheet).getDrawingPatriarch();
        if (drawing == null) {
            drawing = sheet.createDrawingPatriarch();
        }
    } else if (isXSSF()) {
        drawing = ((XSSFSheet) sheet).createDrawingPatriarch();
    } else {
        drawing = sheet.createDrawingPatriarch();
    }

    CreationHelper helper = workbook.getCreationHelper();
    ClientAnchor anchor = helper.createClientAnchor();
    anchor.setRow1(topLeft.getRow());
    anchor.setCol1(topLeft.getCol());
    // +1 since we want to include the
    anchor.setRow2(bottomRight.getRow() + 1);
    anchor.setCol2(bottomRight.getCol() + 1);
    anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE);

    Picture picture = drawing.createPicture(anchor, imageIndex);
    if (originalSize)
        picture.resize();
}

From source file:com.miraisolutions.xlconnect.Workbook.java

License:Open Source License

private void checkName(Name name) {
    if (!isValidReference(name.getRefersToFormula()))
        throw new IllegalArgumentException("Name '" + name.getNameName() + "' has invalid reference!");
    else if (!existsSheet(name.getSheetName())) {
        // The reference as such is valid but it doesn't point to a (existing) sheet ...
        throw new IllegalArgumentException(
                "Name '" + name.getNameName() + "' does not refer to a valid sheet!");
    }//  ww  w  .ja va  2 s. co m
}