Search in sources :

Example 26 with CTCol

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

the class TestXSSFSheet method autoSizeColumn.

@Test
public void autoSizeColumn() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Sheet 1");
    sheet.createRow(0).createCell(13).setCellValue("test");
    sheet.autoSizeColumn(13);
    ColumnHelper columnHelper = sheet.getColumnHelper();
    CTCol col = columnHelper.getColumn(13, false);
    assertTrue(col.getBestFit());
    workbook.close();
}
Also used : ColumnHelper(org.apache.poi.xssf.usermodel.helpers.ColumnHelper) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Example 27 with CTCol

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

the class TestXSSFSheet method groupUngroupColumn.

@Test
public void groupUngroupColumn() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet();
    //one level
    sheet.groupColumn(2, 7);
    sheet.groupColumn(10, 11);
    CTCols cols = sheet.getCTWorksheet().getColsArray(0);
    assertEquals(2, cols.sizeOfColArray());
    CTCol[] colArray = cols.getColArray();
    assertNotNull(colArray);
    // 1 based
    assertEquals(2 + 1, colArray[0].getMin());
    // 1 based
    assertEquals(7 + 1, colArray[0].getMax());
    assertEquals(1, colArray[0].getOutlineLevel());
    assertEquals(0, sheet.getColumnOutlineLevel(0));
    //two level
    sheet.groupColumn(1, 2);
    cols = sheet.getCTWorksheet().getColsArray(0);
    assertEquals(4, cols.sizeOfColArray());
    colArray = cols.getColArray();
    assertEquals(2, colArray[1].getOutlineLevel());
    //three level
    sheet.groupColumn(6, 8);
    sheet.groupColumn(2, 3);
    cols = sheet.getCTWorksheet().getColsArray(0);
    assertEquals(7, cols.sizeOfColArray());
    colArray = cols.getColArray();
    assertEquals(3, colArray[1].getOutlineLevel());
    assertEquals(3, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelCol());
    sheet.ungroupColumn(8, 10);
    colArray = cols.getColArray();
    //assertEquals(3, colArray[1].getOutlineLevel());
    sheet.ungroupColumn(4, 6);
    sheet.ungroupColumn(2, 2);
    colArray = cols.getColArray();
    assertEquals(4, colArray.length);
    assertEquals(2, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelCol());
    workbook.close();
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Example 28 with CTCol

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

the class TestXSSFSheet method bug47862.

/**
     * Setting width of a column included in a column span
     */
@Test
public void bug47862() throws IOException {
    XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47862.xlsx");
    XSSFSheet sheet = wb1.getSheetAt(0);
    CTCols cols = sheet.getCTWorksheet().getColsArray(0);
    //<cols>
    //  <col min="1" max="5" width="15.77734375" customWidth="1"/>
    //</cols>
    //a span of columns [1,5]
    assertEquals(1, cols.sizeOfColArray());
    CTCol col = cols.getColArray(0);
    assertEquals(1, col.getMin());
    assertEquals(5, col.getMax());
    //width of columns in the span
    double swidth = 15.77734375;
    assertEquals(swidth, col.getWidth(), 0.0);
    for (int i = 0; i < 5; i++) {
        assertEquals((int) (swidth * 256), sheet.getColumnWidth(i));
    }
    int[] cw = new int[] { 10, 15, 20, 25, 30 };
    for (int i = 0; i < 5; i++) {
        sheet.setColumnWidth(i, cw[i] * 256);
    }
    //the check below failed prior to fix of Bug #47862
    ColumnHelper.sortColumns(cols);
    //<cols>
    //  <col min="1" max="1" customWidth="true" width="10.0" />
    //  <col min="2" max="2" customWidth="true" width="15.0" />
    //  <col min="3" max="3" customWidth="true" width="20.0" />
    //  <col min="4" max="4" customWidth="true" width="25.0" />
    //  <col min="5" max="5" customWidth="true" width="30.0" />
    //</cols>
    //now the span is splitted into 5 individual columns
    assertEquals(5, cols.sizeOfColArray());
    for (int i = 0; i < 5; i++) {
        assertEquals(cw[i] * 256, sheet.getColumnWidth(i));
        assertEquals(cw[i], cols.getColArray(i).getWidth(), 0.0);
    }
    //serialize and check again
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sheet = wb2.getSheetAt(0);
    cols = sheet.getCTWorksheet().getColsArray(0);
    assertEquals(5, cols.sizeOfColArray());
    for (int i = 0; i < 5; i++) {
        assertEquals(cw[i] * 256, sheet.getColumnWidth(i));
        assertEquals(cw[i], cols.getColArray(i).getWidth(), 0.0);
    }
    wb2.close();
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Example 29 with CTCol

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

the class TestXSSFSheet method columnWidth_lowlevel.

/**
     * Get / Set column width and check the actual values of the underlying XML beans
     */
@Test
public void columnWidth_lowlevel() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Sheet 1");
    sheet.setColumnWidth(1, 22 * 256);
    assertEquals(22 * 256, sheet.getColumnWidth(1));
    // Now check the low level stuff, and check that's all
    //  been set correctly
    XSSFSheet xs = sheet;
    CTWorksheet cts = xs.getCTWorksheet();
    assertEquals(1, cts.sizeOfColsArray());
    CTCols cols = cts.getColsArray(0);
    assertEquals(1, cols.sizeOfColArray());
    CTCol col = cols.getColArray(0);
    // XML is 1 based, POI is 0 based
    assertEquals(2, col.getMin());
    assertEquals(2, col.getMax());
    assertEquals(22.0, col.getWidth(), 0.0);
    assertTrue(col.getCustomWidth());
    // Now set another
    sheet.setColumnWidth(3, 33 * 256);
    assertEquals(1, cts.sizeOfColsArray());
    cols = cts.getColsArray(0);
    assertEquals(2, cols.sizeOfColArray());
    col = cols.getColArray(0);
    // POI 1
    assertEquals(2, col.getMin());
    assertEquals(2, col.getMax());
    assertEquals(22.0, col.getWidth(), 0.0);
    assertTrue(col.getCustomWidth());
    col = cols.getColArray(1);
    // POI 3
    assertEquals(4, col.getMin());
    assertEquals(4, col.getMax());
    assertEquals(33.0, col.getWidth(), 0.0);
    assertTrue(col.getCustomWidth());
    workbook.close();
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Example 30 with CTCol

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

the class TestColumnHelper method createHiddenAndBestFitColsWithHelper.

/**
     * Creates and adds a hidden column and then a best fit column with the given min/max pairs.
     * Suitable for testing handling of overlap. 
     */
private static CTCols createHiddenAndBestFitColsWithHelper(int hiddenMin, int hiddenMax, int bestFitMin, int bestFitMax) {
    CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
    ColumnHelper helper = new ColumnHelper(worksheet);
    CTCols cols = worksheet.getColsArray(0);
    CTCol hidden = createCol(hiddenMin, hiddenMax);
    hidden.setHidden(true);
    helper.addCleanColIntoCols(cols, hidden);
    CTCol bestFit = createCol(bestFitMin, bestFitMax);
    bestFit.setBestFit(true);
    helper.addCleanColIntoCols(cols, bestFit);
    return cols;
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol)

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