Search in sources :

Example 26 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class MergingCells method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    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
    FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 27 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class HSSFSheet method setRepeatingColumns.

@Override
public void setRepeatingColumns(CellRangeAddress columnRangeRef) {
    CellRangeAddress rowRangeRef = getRepeatingRows();
    setRepeatingRowsAndColumns(rowRangeRef, columnRangeRef);
}
Also used : CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 28 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class HSSFSheetConditionalFormatting method addConditionalFormatting.

/**
     * Allows to add a new Conditional Formatting set to the sheet.
     *
     * @param regions - list of rectangular regions to apply conditional formatting rules
     * @param cfRules - set of up to three conditional formatting rules
     *
     * @return index of the newly created Conditional Formatting object
     */
public int addConditionalFormatting(CellRangeAddress[] regions, HSSFConditionalFormattingRule[] cfRules) {
    if (regions == null) {
        throw new IllegalArgumentException("regions must not be null");
    }
    for (CellRangeAddress range : regions) range.validate(SpreadsheetVersion.EXCEL97);
    if (cfRules == null) {
        throw new IllegalArgumentException("cfRules must not be null");
    }
    if (cfRules.length == 0) {
        throw new IllegalArgumentException("cfRules must not be empty");
    }
    if (cfRules.length > 3) {
        throw new IllegalArgumentException("Number of rules must not exceed 3");
    }
    CFRuleBase[] rules = new CFRuleBase[cfRules.length];
    for (int i = 0; i != cfRules.length; i++) {
        rules[i] = cfRules[i].getCfRuleRecord();
    }
    CFRecordsAggregate cfra = new CFRecordsAggregate(regions, rules);
    return _conditionalFormattingTable.add(cfra);
}
Also used : CFRuleBase(org.apache.poi.hssf.record.CFRuleBase) CFRecordsAggregate(org.apache.poi.hssf.record.aggregates.CFRecordsAggregate) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 29 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class HSSFSheet method checkForIntersectingMergedRegions.

/**
     * Verify that no merged regions intersect another merged region in this sheet.
     *
     * @throws IllegalStateException if at least one region intersects with another merged region in this sheet
     */
private void checkForIntersectingMergedRegions() {
    final List<CellRangeAddress> regions = getMergedRegions();
    final int size = regions.size();
    for (int i = 0; i < size; i++) {
        final CellRangeAddress region = regions.get(i);
        for (final CellRangeAddress other : regions.subList(i + 1, regions.size())) {
            if (region.intersects(other)) {
                String msg = "The range " + region.formatAsString() + " intersects with another merged region " + other.formatAsString() + " in this sheet";
                throw new IllegalStateException(msg);
            }
        }
    }
}
Also used : CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 30 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class HSSFSheet method getRepeatingRowsOrColums.

private CellRangeAddress getRepeatingRowsOrColums(boolean rows) {
    NameRecord rec = getBuiltinNameRecord(NameRecord.BUILTIN_PRINT_TITLE);
    if (rec == null) {
        return null;
    }
    Ptg[] nameDefinition = rec.getNameDefinition();
    if (nameDefinition == null) {
        return null;
    }
    int maxRowIndex = SpreadsheetVersion.EXCEL97.getLastRowIndex();
    int maxColIndex = SpreadsheetVersion.EXCEL97.getLastColumnIndex();
    for (Ptg ptg : nameDefinition) {
        if (ptg instanceof Area3DPtg) {
            Area3DPtg areaPtg = (Area3DPtg) ptg;
            if (areaPtg.getFirstColumn() == 0 && areaPtg.getLastColumn() == maxColIndex) {
                if (rows) {
                    return new CellRangeAddress(areaPtg.getFirstRow(), areaPtg.getLastRow(), -1, -1);
                }
            } else if (areaPtg.getFirstRow() == 0 && areaPtg.getLastRow() == maxRowIndex) {
                if (!rows) {
                    return new CellRangeAddress(-1, -1, areaPtg.getFirstColumn(), areaPtg.getLastColumn());
                }
            }
        }
    }
    return null;
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) NameRecord(org.apache.poi.hssf.record.NameRecord) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Aggregations

CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)194 Test (org.junit.Test)73 Row (org.apache.poi.ss.usermodel.Row)33 Cell (org.apache.poi.ss.usermodel.Cell)31 Sheet (org.apache.poi.ss.usermodel.Sheet)22 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)21 ArrayList (java.util.ArrayList)19 Workbook (org.apache.poi.ss.usermodel.Workbook)18 HSSFConditionalFormattingRule (org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule)16 ConditionalFormattingRule (org.apache.poi.ss.usermodel.ConditionalFormattingRule)16 FileOutputStream (java.io.FileOutputStream)15 SheetConditionalFormatting (org.apache.poi.ss.usermodel.SheetConditionalFormatting)15 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)14 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)14 XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)13 HSSFConditionalFormatting (org.apache.poi.hssf.usermodel.HSSFConditionalFormatting)12 CellReference (org.apache.poi.ss.util.CellReference)12 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)8 Ptg (org.apache.poi.ss.formula.ptg.Ptg)8