Search in sources :

Example 6 with CellRangeAddress

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

the class CFHeaderBase method read.

protected void read(RecordInputStream in) {
    field_1_numcf = in.readShort();
    field_2_need_recalculation_and_id = in.readShort();
    field_3_enclosing_cell_range = new CellRangeAddress(in);
    field_4_cell_ranges = new CellRangeAddressList(in);
}
Also used : CellRangeAddressList(org.apache.poi.ss.util.CellRangeAddressList) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 7 with CellRangeAddress

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

the class MergedCells method main.

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFRow row = sheet.createRow(1);
    HSSFCell cell = row.createCell(1);
    cell.setCellValue("This is a test of merging");
    sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) FileOutputStream(java.io.FileOutputStream) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 8 with CellRangeAddress

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

the class CFRecordsAggregate method shiftRange.

private static CellRangeAddress shiftRange(FormulaShifter shifter, CellRangeAddress cra, int currentExternSheetIx) {
    // FormulaShifter works well in terms of Ptgs - so convert CellRangeAddress to AreaPtg (and back) here
    AreaPtg aptg = new AreaPtg(cra.getFirstRow(), cra.getLastRow(), cra.getFirstColumn(), cra.getLastColumn(), false, false, false, false);
    Ptg[] ptgs = { aptg };
    if (!shifter.adjustFormula(ptgs, currentExternSheetIx)) {
        return cra;
    }
    Ptg ptg0 = ptgs[0];
    if (ptg0 instanceof AreaPtg) {
        AreaPtg bptg = (AreaPtg) ptg0;
        return new CellRangeAddress(bptg.getFirstRow(), bptg.getLastRow(), bptg.getFirstColumn(), bptg.getLastColumn());
    }
    if (ptg0 instanceof AreaErrPtg) {
        return null;
    }
    throw new IllegalStateException("Unexpected shifted ptg class (" + ptg0.getClass().getName() + ")");
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg)

Example 9 with CellRangeAddress

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

the class CFRecordsAggregate method updateFormulasAfterCellShift.

/**
     * @return <code>false</code> if this whole {@link CFHeaderRecord} / {@link CFRuleRecord}s should be deleted
     */
public boolean updateFormulasAfterCellShift(FormulaShifter shifter, int currentExternSheetIx) {
    CellRangeAddress[] cellRanges = header.getCellRanges();
    boolean changed = false;
    List<CellRangeAddress> temp = new ArrayList<CellRangeAddress>();
    for (CellRangeAddress craOld : cellRanges) {
        CellRangeAddress craNew = shiftRange(shifter, craOld, currentExternSheetIx);
        if (craNew == null) {
            changed = true;
            continue;
        }
        temp.add(craNew);
        if (craNew != craOld) {
            changed = true;
        }
    }
    if (changed) {
        int nRanges = temp.size();
        if (nRanges == 0) {
            return false;
        }
        CellRangeAddress[] newRanges = new CellRangeAddress[nRanges];
        temp.toArray(newRanges);
        header.setCellRanges(newRanges);
    }
    for (CFRuleBase rule : rules) {
        Ptg[] ptgs;
        ptgs = rule.getParsedExpression1();
        if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
            rule.setParsedExpression1(ptgs);
        }
        ptgs = rule.getParsedExpression2();
        if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
            rule.setParsedExpression2(ptgs);
        }
        if (rule instanceof CFRule12Record) {
            CFRule12Record rule12 = (CFRule12Record) rule;
            ptgs = rule12.getParsedExpressionScale();
            if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
                rule12.setParsedExpressionScale(ptgs);
            }
        }
    }
    return true;
}
Also used : CFRuleBase(org.apache.poi.hssf.record.CFRuleBase) Ptg(org.apache.poi.ss.formula.ptg.Ptg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) CFRule12Record(org.apache.poi.hssf.record.CFRule12Record) ArrayList(java.util.ArrayList) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 10 with CellRangeAddress

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

the class MergedCellsTable method addMergeCellsRecord.

private void addMergeCellsRecord(MergeCellsRecord mcr) {
    int nRegions = mcr.getNumAreas();
    for (int i = 0; i < nRegions; i++) {
        CellRangeAddress cra = mcr.getAreaAt(i);
        _mergedRegions.add(cra);
    }
}
Also used : 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