Search in sources :

Example 71 with CellRangeAddress

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

the class TestDataSources method testMixedCellDataSource.

public void testMixedCellDataSource() {
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = new SheetBuilder(wb, mixedCells).build();
    CellRangeAddress mixedCellRange = CellRangeAddress.valueOf("A1:F1");
    ChartDataSource<String> strDataSource = DataSources.fromStringCellRange(sheet, mixedCellRange);
    ChartDataSource<Number> numDataSource = DataSources.fromNumericCellRange(sheet, mixedCellRange);
    for (int i = 0; i < mixedCells[0].length; ++i) {
        if (i % 2 == 0) {
            assertNull(strDataSource.getPointAt(i));
            assertEquals(((Number) mixedCells[0][i]).doubleValue(), numDataSource.getPointAt(i).doubleValue(), 0.00001);
        } else {
            assertNull(numDataSource.getPointAt(i));
            assertEquals(mixedCells[0][i], strDataSource.getPointAt(i));
        }
    }
}
Also used : SheetBuilder(org.apache.poi.ss.util.SheetBuilder) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 72 with CellRangeAddress

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

the class TestDataSources method testIOBExceptionOnInvalidIndex.

public void testIOBExceptionOnInvalidIndex() {
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = new SheetBuilder(wb, numericCells).build();
    CellRangeAddress rangeAddress = CellRangeAddress.valueOf("A2:E2");
    ChartDataSource<Number> numDataSource = DataSources.fromNumericCellRange(sheet, rangeAddress);
    IndexOutOfBoundsException exception = null;
    try {
        numDataSource.getPointAt(-1);
    } catch (IndexOutOfBoundsException e) {
        exception = e;
    }
    assertNotNull(exception);
    exception = null;
    try {
        numDataSource.getPointAt(numDataSource.getPointCount());
    } catch (IndexOutOfBoundsException e) {
        exception = e;
    }
    assertNotNull(exception);
}
Also used : SheetBuilder(org.apache.poi.ss.util.SheetBuilder) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 73 with CellRangeAddress

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

the class TestDataSources method testStringCellDataSource.

public void testStringCellDataSource() {
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = new SheetBuilder(wb, stringCells).build();
    CellRangeAddress numCellRange = CellRangeAddress.valueOf("A2:E2");
    ChartDataSource<String> numDataSource = DataSources.fromStringCellRange(sheet, numCellRange);
    assertTrue(numDataSource.isReference());
    assertFalse(numDataSource.isNumeric());
    assertEquals(numericCells[0].length, numDataSource.getPointCount());
    for (int i = 0; i < stringCells[1].length; ++i) {
        assertEquals(stringCells[1][i], numDataSource.getPointAt(i));
    }
}
Also used : SheetBuilder(org.apache.poi.ss.util.SheetBuilder) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 74 with CellRangeAddress

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

the class BaseTestSheet method removeMergedRegions.

/**
     * Remove multiple merged regions
     */
@Test
public void removeMergedRegions() throws IOException {
    Workbook wb = _testDataProvider.createWorkbook();
    Sheet sheet = wb.createSheet();
    Map<Integer, CellRangeAddress> mergedRegions = new HashMap<Integer, CellRangeAddress>();
    for (int r = 0; r < 10; r++) {
        CellRangeAddress region = new CellRangeAddress(r, r, 0, 1);
        mergedRegions.put(r, region);
        sheet.addMergedRegion(region);
    }
    assertCollectionEquals(mergedRegions.values(), sheet.getMergedRegions());
    Collection<Integer> removed = Arrays.asList(0, 2, 3, 6, 8);
    mergedRegions.keySet().removeAll(removed);
    sheet.removeMergedRegions(removed);
    assertCollectionEquals(mergedRegions.values(), sheet.getMergedRegions());
    wb.close();
}
Also used : CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Test(org.junit.Test)

Example 75 with CellRangeAddress

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

the class BaseTestSheet method addMergedRegionWithSingleCellShouldFail.

/*
     * Bug 56345: Reject single-cell merged regions
     */
@Test
public void addMergedRegionWithSingleCellShouldFail() throws IOException {
    final Workbook wb = _testDataProvider.createWorkbook();
    final Sheet sheet = wb.createSheet();
    final CellRangeAddress region = CellRangeAddress.valueOf("A1:A1");
    try {
        sheet.addMergedRegion(region);
        fail("Should not be able to add a single-cell merged region (" + region.formatAsString() + ")");
    } catch (final IllegalArgumentException e) {
    // expected
    }
    wb.close();
}
Also used : CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Test(org.junit.Test)

Aggregations

CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)247 Test (org.junit.Test)74 Row (org.apache.poi.ss.usermodel.Row)53 Cell (org.apache.poi.ss.usermodel.Cell)50 Sheet (org.apache.poi.ss.usermodel.Sheet)30 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)27 ArrayList (java.util.ArrayList)26 Workbook (org.apache.poi.ss.usermodel.Workbook)24 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)19 FileOutputStream (java.io.FileOutputStream)18 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)17 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)17 HSSFConditionalFormattingRule (org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule)16 ConditionalFormattingRule (org.apache.poi.ss.usermodel.ConditionalFormattingRule)16 SheetConditionalFormatting (org.apache.poi.ss.usermodel.SheetConditionalFormatting)15 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)15 CellReference (org.apache.poi.ss.util.CellReference)14 XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)13 HSSFConditionalFormatting (org.apache.poi.hssf.usermodel.HSSFConditionalFormatting)12 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)11