Search in sources :

Example 1 with CTCol

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol in project poi by apache.

the class SXSSFPicture method getColumnWidthInPixels.

private float getColumnWidthInPixels(int columnIndex) {
    XSSFSheet sheet = getSheet();
    CTCol col = sheet.getColumnHelper().getColumn(columnIndex, false);
    double numChars = col == null || !col.isSetWidth() ? DEFAULT_COLUMN_WIDTH : col.getWidth();
    return (float) numChars * XSSFWorkbook.DEFAULT_CHARACTER_WIDTH;
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol)

Example 2 with CTCol

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol in project poi by apache.

the class TestXSSFColGrouping method testMergingOverlappingCols_OVERLAPS_1_WRAPS.

/**
     * Test the cols element is correct in case of NumericRanges.OVERLAPS_1_WRAPS
     */
@Test
public void testMergingOverlappingCols_OVERLAPS_1_WRAPS() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet = wb1.createSheet("test");
    CTCols cols = sheet.getCTWorksheet().getColsArray(0);
    CTCol col = cols.addNewCol();
    col.setMin(2 + 1);
    col.setMax(4 + 1);
    col.setWidth(20);
    col.setCustomWidth(true);
    sheet.groupColumn((short) 1, (short) 5);
    cols = sheet.getCTWorksheet().getColsArray(0);
    logger.log(POILogger.DEBUG, "testMergingOverlappingCols_OVERLAPS_1_WRAPS/cols:" + cols);
    assertEquals(1, cols.getColArray(0).getOutlineLevel());
    // 1 based
    assertEquals(2, cols.getColArray(0).getMin());
    // 1 based
    assertEquals(2, cols.getColArray(0).getMax());
    assertEquals(false, cols.getColArray(0).getCustomWidth());
    assertEquals(1, cols.getColArray(1).getOutlineLevel());
    // 1 based
    assertEquals(3, cols.getColArray(1).getMin());
    // 1 based        
    assertEquals(5, cols.getColArray(1).getMax());
    assertEquals(true, cols.getColArray(1).getCustomWidth());
    assertEquals(1, cols.getColArray(2).getOutlineLevel());
    // 1 based
    assertEquals(6, cols.getColArray(2).getMin());
    // 1 based
    assertEquals(6, cols.getColArray(2).getMax());
    assertEquals(false, cols.getColArray(2).getCustomWidth());
    assertEquals(3, cols.sizeOfColArray());
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1, "testMergingOverlappingCols_OVERLAPS_1_WRAPS");
    sheet = wb2.getSheet("test");
    for (int i = 2; i <= 4; i++) {
        assertEquals("Unexpected width of column " + i, 20 * 256, sheet.getColumnWidth(i));
    }
    wb2.close();
    wb1.close();
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) Test(org.junit.Test)

Example 3 with CTCol

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol in project poi by apache.

the class TestXSSFColGrouping method testMergingOverlappingCols_OVERLAPS_2_WRAPS.

/**
     * Test the cols element is correct in case of NumericRanges.OVERLAPS_2_WRAPS
     */
@Test
public void testMergingOverlappingCols_OVERLAPS_2_WRAPS() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet = wb1.createSheet("test");
    CTCols cols = sheet.getCTWorksheet().getColsArray(0);
    CTCol col = cols.addNewCol();
    col.setMin(1 + 1);
    col.setMax(4 + 1);
    col.setWidth(20);
    col.setCustomWidth(true);
    sheet.groupColumn((short) 2, (short) 3);
    sheet.getCTWorksheet().getColsArray(0);
    logger.log(POILogger.DEBUG, "testMergingOverlappingCols_OVERLAPS_2_WRAPS/cols:" + cols);
    assertEquals(0, cols.getColArray(0).getOutlineLevel());
    // 1 based
    assertEquals(2, cols.getColArray(0).getMin());
    // 1 based
    assertEquals(2, cols.getColArray(0).getMax());
    assertEquals(true, cols.getColArray(0).getCustomWidth());
    assertEquals(1, cols.getColArray(1).getOutlineLevel());
    // 1 based
    assertEquals(3, cols.getColArray(1).getMin());
    // 1 based        
    assertEquals(4, cols.getColArray(1).getMax());
    assertEquals(true, cols.getColArray(1).getCustomWidth());
    assertEquals(0, cols.getColArray(2).getOutlineLevel());
    // 1 based
    assertEquals(5, cols.getColArray(2).getMin());
    // 1 based
    assertEquals(5, cols.getColArray(2).getMax());
    assertEquals(true, cols.getColArray(2).getCustomWidth());
    assertEquals(3, cols.sizeOfColArray());
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1, "testMergingOverlappingCols_OVERLAPS_2_WRAPS");
    sheet = wb2.getSheet("test");
    for (int i = 1; i <= 4; i++) {
        assertEquals("Unexpected width of column " + i, 20 * 256, sheet.getColumnWidth(i));
    }
    wb2.close();
    wb1.close();
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) Test(org.junit.Test)

Example 4 with CTCol

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol in project poi by apache.

the class TestXSSFColGrouping method testNoColsWithoutWidthWhenGrouping.

/**
     * Tests that POI doesn't produce "col" elements without "width" attribute. 
     * POI-52186
     */
@Test
public void testNoColsWithoutWidthWhenGrouping() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet = wb1.createSheet("test");
    sheet.setColumnWidth(4, 5000);
    sheet.setColumnWidth(5, 5000);
    sheet.groupColumn((short) 4, (short) 7);
    sheet.groupColumn((short) 9, (short) 12);
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1, "testNoColsWithoutWidthWhenGrouping");
    sheet = wb2.getSheet("test");
    CTCols cols = sheet.getCTWorksheet().getColsArray(0);
    logger.log(POILogger.DEBUG, "test52186/cols:" + cols);
    for (CTCol col : cols.getColArray()) {
        assertTrue("Col width attribute is unset: " + col, col.isSetWidth());
    }
    wb2.close();
    wb1.close();
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) Test(org.junit.Test)

Example 5 with CTCol

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol in project poi by apache.

the class TestColumnHelper method testGetSetColDefaultStyle.

@Test
public void testGetSetColDefaultStyle() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet();
    CTWorksheet ctWorksheet = sheet.getCTWorksheet();
    ColumnHelper columnHelper = sheet.getColumnHelper();
    // POI column 3, OOXML column 4
    CTCol col = columnHelper.getOrCreateColumn1Based(4, false);
    assertNotNull(col);
    assertNotNull(columnHelper.getColumn(3, false));
    columnHelper.setColDefaultStyle(3, 2);
    assertEquals(2, columnHelper.getColDefaultStyle(3));
    assertEquals(-1, columnHelper.getColDefaultStyle(4));
    StylesTable stylesTable = workbook.getStylesSource();
    CTXf cellXf = CTXf.Factory.newInstance();
    cellXf.setFontId(0);
    cellXf.setFillId(0);
    cellXf.setBorderId(0);
    cellXf.setNumFmtId(0);
    cellXf.setXfId(0);
    stylesTable.putCellXf(cellXf);
    CTCol col_2 = ctWorksheet.getColsArray(0).addNewCol();
    col_2.setMin(10);
    col_2.setMax(12);
    col_2.setStyle(1);
    assertEquals(1, columnHelper.getColDefaultStyle(11));
    XSSFCellStyle cellStyle = new XSSFCellStyle(0, 0, stylesTable, null);
    columnHelper.setColDefaultStyle(11, cellStyle);
    assertEquals(0, col_2.getStyle());
    assertEquals(1, columnHelper.getColDefaultStyle(10));
    workbook.close();
}
Also used : CTXf(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) StylesTable(org.apache.poi.xssf.model.StylesTable) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Test(org.junit.Test)

Aggregations

CTCol (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol)38 Test (org.junit.Test)19 CTCols (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols)18 CTWorksheet (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet)7 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)5 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)3 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 StylesTable (org.apache.poi.xssf.model.StylesTable)1 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)1 ColumnHelper (org.apache.poi.xssf.usermodel.helpers.ColumnHelper)1 CTXf (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf)1