Example usage for org.apache.poi.ss.util AreaReference formatAsString

List of usage examples for org.apache.poi.ss.util AreaReference formatAsString

Introduction

In this page you can find the example usage for org.apache.poi.ss.util AreaReference formatAsString.

Prototype

public String formatAsString() 

Source Link

Document

Returns a text representation of this area reference.

Usage

From source file:CreateTable.java

License:Apache License

public static void main(String[] args) throws FileNotFoundException, IOException {

    Workbook wb = new XSSFWorkbook();
    XSSFSheet sheet = (XSSFSheet) wb.createSheet();

    //Create /*from   w w w . ja v  a 2  s .  c o  m*/
    XSSFTable table = sheet.createTable();
    table.setDisplayName("Test");
    CTTable cttable = table.getCTTable();

    //Style configurations
    CTTableStyleInfo style = cttable.addNewTableStyleInfo();
    style.setName("TableStyleMedium2");
    style.setShowColumnStripes(false);
    style.setShowRowStripes(true);

    //Set which area the table should be placed in
    AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(3, 3));
    cttable.setRef(reference.formatAsString());
    cttable.setId(1);
    cttable.setName("Test");
    cttable.setTotalsRowCount(1);

    CTTableColumns columns = cttable.addNewTableColumns();
    columns.setCount(3);
    CTTableColumn column;
    XSSFRow row;
    XSSFCell cell;
    for (int i = 0; i < 3; i++) {
        //Create column
        column = columns.addNewTableColumn();
        column.setName("Column");
        column.setId(i + 1);
        //Create row
        row = sheet.createRow(i);
        for (int j = 0; j < 3; j++) {
            //Create cell
            cell = row.createCell(j);
            if (i == 0) {
                cell.setCellValue("Column" + j);
            } else {
                cell.setCellValue(i + j + 0.0);
            }
        }
    }

    FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx");
    wb.write(fileOut);
    fileOut.close();
}

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);//from  w  ww  .j a  v a2  s  . co m
    checkName(cname);

    // 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:de.jlo.talendcomp.excel.SpreadsheetOutput.java

License:Apache License

private boolean extendTable(XSSFTable table, int firstRow, int firstCol, int lastRow) throws Exception {
    try {//  w  w w.j  a va2 s. c o  m
        AreaReference currentRef = null;
        if (currentType == SpreadsheetTyp.XLS) {
            currentRef = new AreaReference(table.getCTTable().getRef(), SpreadsheetVersion.EXCEL97);
        } else {
            currentRef = new AreaReference(table.getCTTable().getRef(), SpreadsheetVersion.EXCEL2007);
        }
        CellReference topLeft = currentRef.getFirstCell();
        CellReference buttomRight = currentRef.getLastCell();
        if (topLeft.getRow() <= firstRow && buttomRight.getRow() >= firstRow && topLeft.getCol() <= firstCol
                && buttomRight.getCol() >= firstCol) {
            // this table is within our write area, we have to expand it
            AreaReference newRef = new AreaReference(topLeft, // left top including the header line
                    new CellReference(lastRow, buttomRight.getCol())); // bottom right
            table.getCTTable().setRef(newRef.formatAsString());
            return true;
        } else {
            return false;
        }
    } catch (Exception t) {
        if (workbook instanceof SXSSFWorkbook) {
            throw new Exception(
                    "Extending table ranges cannot work in a workbook which is not fully loaded because of the memory saving mode. Uncheck Memory saving mode in tFileExcelWorkbookOpen!",
                    t);
        } else {
            throw t;
        }
    }
}

From source file:edu.cmu.emfta.actions.CutSet.java

License:Open Source License

public XSSFWorkbook toSingleSheetWorkbook() {
    XSSFWorkbook workbook = new XSSFWorkbook();
    int cutSetIdentifier = 0;
    double cutsetProbability;

    XSSFSheet sheet = workbook.createSheet();

    XSSFTable table = sheet.createTable();
    table.setDisplayName("Cutsets");
    CTTable cttable = table.getCTTable();

    // Set which area the table should be placed in
    AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2));
    cttable.setRef(reference.formatAsString());
    cttable.setId((long) 1);
    cttable.setName("Cutsets");
    cttable.setTotalsRowCount((long) 1);

    CTTableColumns columns = cttable.addNewTableColumns();
    columns.setCount((long) 3);
    CTTableColumn column;/*from ww w  .j  ava 2  s. c om*/
    XSSFRow row;
    XSSFCell cell;

    column = columns.addNewTableColumn();

    // Create row
    row = sheet.createRow(0);
    CellStyle headingCellStyle = workbook.createCellStyle();
    XSSFFont headingFont = workbook.createFont();
    headingFont.setBold(true);
    headingCellStyle.setFont(headingFont);
    row.setRowStyle(headingCellStyle);

    CellStyle normalCellStyle = workbook.createCellStyle();
    XSSFFont normalFont = workbook.createFont();
    normalFont.setBold(false);
    normalCellStyle.setFont(normalFont);

    for (int j = 0; j < 3; j++) {
        // Create cell
        cell = row.createCell(j);

        switch (j) {
        case 0: {
            cell.setCellValue("Identifier");
            break;
        }
        case 1: {
            cell.setCellValue("Description");
            break;
        }
        case 2: {
            cell.setCellValue("Probability");
            break;
        }

        }
    }

    int rowId = 1;

    for (List<Event> events : cutset) {
        row = sheet.createRow(rowId++);
        row = sheet.createRow(rowId++);
        row.setRowStyle(normalCellStyle);

        cell = row.createCell(0);
        cell.setCellValue("Cutset #" + cutSetIdentifier);

        cutsetProbability = 1;
        for (int i = 0; i < events.size(); i++) {
            cutsetProbability = cutsetProbability * events.get(i).getProbability();
        }

        cell = row.createCell(2);
        if (cutsetProbability != 1) {
            cell.setCellValue("" + cutsetProbability);
        } else {
            cell.setCellValue("" + cutsetProbability);
        }
        //         System.out.println("[CutSet] cutset id=" + cutSetIdentifier);

        for (int i = 0; i < events.size(); i++) {
            Event e = events.get(i);

            //            System.out.println("[CutSet] event name=" + e.getName());

            // Create row
            row = sheet.createRow(rowId++);
            row.setRowStyle(normalCellStyle);
            for (int j = 0; j < 3; j++) {
                // Create cell
                cell = row.createCell(j);

                switch (j) {
                case 0: {
                    cell.setCellValue(e.getName());
                    break;
                }
                case 1: {
                    cell.setCellValue(e.getDescription());
                    break;
                }
                case 2: {
                    cell.setCellValue(e.getProbability());
                    break;
                }

                }
            }
        }
        cutSetIdentifier = cutSetIdentifier + 1;
    }

    return workbook;

}

From source file:edu.cmu.emfta.actions.CutSet.java

License:Open Source License

public XSSFWorkbook toMultiSheetsWorkbook() {
    XSSFWorkbook workbook = new XSSFWorkbook();
    int cutSetIdentifier = 0;
    double cutsetProbability;

    for (List<Event> events : cutset) {

        cutsetProbability = 1;/*from www.j  av a 2s. c  o  m*/
        for (int i = 0; i < events.size(); i++) {
            cutsetProbability = cutsetProbability * events.get(i).getProbability();
        }

        //         System.out.println("[CutSet] cutset id=" + cutSetIdentifier);
        XSSFSheet sheet = workbook.createSheet();

        XSSFTable table = sheet.createTable();
        table.setDisplayName("Cutset");
        CTTable cttable = table.getCTTable();

        // Set which area the table should be placed in
        AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2));
        cttable.setRef(reference.formatAsString());
        cttable.setId((long) 1);
        cttable.setName("Cutset " + cutSetIdentifier);
        cttable.setTotalsRowCount((long) 1);

        CTTableColumns columns = cttable.addNewTableColumns();
        columns.setCount((long) 3);
        CTTableColumn column;
        XSSFRow row;
        XSSFCell cell;

        column = columns.addNewTableColumn();

        // Create row
        row = sheet.createRow(0);
        CellStyle headingCellStyle = workbook.createCellStyle();
        XSSFFont headingFont = workbook.createFont();
        headingFont.setBold(true);
        headingCellStyle.setFont(headingFont);
        row.setRowStyle(headingCellStyle);

        CellStyle normalCellStyle = workbook.createCellStyle();
        XSSFFont normalFont = workbook.createFont();
        normalFont.setBold(false);
        normalCellStyle.setFont(normalFont);

        for (int j = 0; j < 3; j++) {
            // Create cell
            cell = row.createCell(j);

            switch (j) {
            case 0: {
                cell.setCellValue("Identifier");
                break;
            }
            case 1: {
                cell.setCellValue("Description");
                break;
            }
            case 2: {
                if (cutsetProbability == 1) {
                    cell.setCellValue("Probability");
                } else {
                    cell.setCellValue("Probability (" + cutsetProbability + ")");
                }
                break;
            }

            }
        }

        for (int i = 0; i < events.size(); i++) {
            Event e = events.get(i);

            System.out.println("[CutSet] event name=" + e.getName());
            // Create column
            column = columns.addNewTableColumn();
            column.setName("Column");
            column.setId((long) i + 1);
            // Create row
            row = sheet.createRow(i + 1);
            row.setRowStyle(normalCellStyle);
            for (int j = 0; j < 3; j++) {
                // Create cell
                cell = row.createCell(j);

                switch (j) {
                case 0: {
                    cell.setCellValue(e.getName());
                    break;
                }
                case 1: {
                    cell.setCellValue(e.getDescription());
                    break;
                }
                case 2: {
                    cell.setCellValue(e.getProbability());
                    break;
                }

                }
            }
        }
        cutSetIdentifier = cutSetIdentifier + 1;
    }

    return workbook;

}

From source file:packtest.CreateTable.java

License:Apache License

public static void main(String[] args) throws FileNotFoundException, IOException {

    Workbook wb = new XSSFWorkbook();
    XSSFSheet sheet = (XSSFSheet) wb.createSheet();

    //Create /*  ww  w. ja  va 2s . c  o m*/
    XSSFTable table = sheet.createTable();
    table.setDisplayName("Test");
    CTTable cttable = table.getCTTable();

    //Style configurations
    CTTableStyleInfo style = cttable.addNewTableStyleInfo();
    style.setName("TableStyleMedium2");
    style.setShowColumnStripes(false);
    style.setShowRowStripes(true);

    //Set which area the table should be placed in
    AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2));
    cttable.setRef(reference.formatAsString());
    cttable.setId(1);
    cttable.setName("Test");
    cttable.setTotalsRowCount(1);

    CTTableColumns columns = cttable.addNewTableColumns();
    columns.setCount(3);
    CTTableColumn column;
    XSSFRow row;
    XSSFCell cell;
    for (int i = 0; i < 3; i++) {
        //Create column
        column = columns.addNewTableColumn();
        column.setName("Column");
        column.setId(i + 1);
        //Create row
        row = sheet.createRow(i);
        for (int j = 0; j < 3; j++) {
            //Create cell
            cell = row.createCell(j);
            if (i == 0) {
                cell.setCellValue("Column" + j);
            } else {
                cell.setCellValue("0");
            }
        }
    }

    FileOutputStream fileOut = new FileOutputStream(Utils.getPath("ooxml-table.xlsx"));
    wb.write(fileOut);
    fileOut.close();
}