Search in sources :

Example 1 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project poi by apache.

the class AligningCells method main.

public static void main(String[] args) throws IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet();
    XSSFRow row = sheet.createRow(2);
    row.setHeightInPoints(30);
    for (int i = 0; i < 8; i++) {
        //column width is set in units of 1/256th of a character width
        sheet.setColumnWidth(i, 256 * 15);
    }
    createCell(wb, row, 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
    createCell(wb, row, 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM);
    createCell(wb, row, 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER);
    createCell(wb, row, 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER);
    createCell(wb, row, 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY);
    createCell(wb, row, 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP);
    createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);
    //center text over B4, C4, D4
    row = sheet.createRow(3);
    centerAcrossSelection(wb, row, 1, 3, VerticalAlignment.CENTER);
    // Write the output to a file
    OutputStream fileOut = new FileOutputStream("xssf-align.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

Example 2 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project poi by apache.

the class CreateTable method main.

public static void main(String[] args) throws IOException {
    Workbook wb = new XSSFWorkbook();
    XSSFSheet sheet = (XSSFSheet) wb.createSheet();
    //Create 
    XSSFTable table = sheet.createTable();
    table.setDisplayName("Test");
    CTTable cttable = table.getCTTable();
    //Style configurations
    CTTableStyleInfo style = cttable.addNewTableStyleInfo();
    style.setName("TableStyleMedium2");
    style.setShowColumnStripes(false);
    style.setShowRowStripes(true);
    //Set which area the table should be placed in
    AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2));
    cttable.setRef(reference.formatAsString());
    cttable.setId(1);
    cttable.setName("Test");
    cttable.setTotalsRowCount(1);
    CTTableColumns columns = cttable.addNewTableColumns();
    columns.setCount(3);
    CTTableColumn column;
    XSSFRow row;
    XSSFCell cell;
    for (int i = 0; i < 3; i++) {
        //Create column
        column = columns.addNewTableColumn();
        column.setName("Column");
        column.setId(i + 1);
        //Create row
        row = sheet.createRow(i);
        for (int j = 0; j < 3; j++) {
            //Create cell
            cell = row.createCell(j);
            if (i == 0) {
                cell.setCellValue("Column" + j);
            } else {
                cell.setCellValue("0");
            }
        }
    }
    FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : AreaReference(org.apache.poi.ss.util.AreaReference) CTTableColumns(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns) CellReference(org.apache.poi.ss.util.CellReference) XSSFTable(org.apache.poi.xssf.usermodel.XSSFTable) CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) CTTableStyleInfo(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo) FileOutputStream(java.io.FileOutputStream) CTTable(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Example 3 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project poi by apache.

the class XSSFSingleXmlCell method getReferencedCell.

/**
	 * Gets the XSSFCell referenced by the R attribute or creates a new one if cell doesn't exists
	 * @return the referenced XSSFCell, null if the cell reference is invalid
	 */
public XSSFCell getReferencedCell() {
    XSSFCell cell = null;
    CellReference cellReference = new CellReference(singleXmlCell.getR());
    XSSFRow row = parent.getXSSFSheet().getRow(cellReference.getRow());
    if (row == null) {
        row = parent.getXSSFSheet().createRow(cellReference.getRow());
    }
    cell = row.getCell(cellReference.getCol());
    if (cell == null) {
        cell = row.createCell(cellReference.getCol());
    }
    return cell;
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CellReference(org.apache.poi.ss.util.CellReference)

Example 4 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project poi by apache.

the class TestThemesTable method testThemesTableColors.

@Test
public void testThemesTableColors() throws Exception {
    // Load our two test workbooks
    XSSFWorkbook simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
    XSSFWorkbook complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
    // Save and re-load them, to check for stability across that
    XSSFWorkbook simpleRS = XSSFTestDataSamples.writeOutAndReadBack(simple);
    XSSFWorkbook complexRS = XSSFTestDataSamples.writeOutAndReadBack(complex);
    // Fetch fresh copies to test with
    simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
    complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
    // Files and descriptions
    Map<String, XSSFWorkbook> workbooks = new LinkedHashMap<String, XSSFWorkbook>();
    workbooks.put(testFileSimple, simple);
    workbooks.put("Re-Saved_" + testFileSimple, simpleRS);
    workbooks.put(testFileComplex, complex);
    workbooks.put("Re-Saved_" + testFileComplex, complexRS);
    // Sanity check
    assertEquals(rgbExpected.length, rgbExpected.length);
    // For offline testing
    boolean createFiles = false;
    //  for the theme-applied cells in Column A are correct
    for (String whatWorkbook : workbooks.keySet()) {
        XSSFWorkbook workbook = workbooks.get(whatWorkbook);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int startRN = 0;
        if (whatWorkbook.endsWith(testFileComplex))
            startRN++;
        for (int rn = startRN; rn < rgbExpected.length + startRN; rn++) {
            XSSFRow row = sheet.getRow(rn);
            assertNotNull("Missing row " + rn + " in " + whatWorkbook, row);
            String ref = (new CellReference(rn, 0)).formatAsString();
            XSSFCell cell = row.getCell(0);
            assertNotNull("Missing cell " + ref + " in " + whatWorkbook, cell);
            int expectedThemeIdx = rn - startRN;
            ThemeElement themeElem = ThemeElement.byId(expectedThemeIdx);
            assertEquals("Wrong theme at " + ref + " in " + whatWorkbook, themeElem.name.toLowerCase(Locale.ROOT), cell.getStringCellValue());
            // Fonts are theme-based in their colours
            XSSFFont font = cell.getCellStyle().getFont();
            CTColor ctColor = font.getCTFont().getColorArray(0);
            assertNotNull(ctColor);
            assertEquals(true, ctColor.isSetTheme());
            assertEquals(themeElem.idx, ctColor.getTheme());
            // Get the colour, via the theme
            XSSFColor color = font.getXSSFColor();
            // Theme colours aren't tinted
            assertEquals(false, color.hasTint());
            // Check the RGB part (no tint)
            assertEquals("Wrong theme colour " + themeElem.name + " on " + whatWorkbook, rgbExpected[expectedThemeIdx], Hex.encodeHexString(color.getRGB()));
            long themeIdx = font.getCTFont().getColorArray(0).getTheme();
            assertEquals("Wrong theme index " + expectedThemeIdx + " on " + whatWorkbook, expectedThemeIdx, themeIdx);
            if (createFiles) {
                XSSFCellStyle cs = row.getSheet().getWorkbook().createCellStyle();
                cs.setFillForegroundColor(color);
                cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
                row.createCell(1).setCellStyle(cs);
            }
        }
        if (createFiles) {
            FileOutputStream fos = new FileOutputStream("Generated_" + whatWorkbook);
            workbook.write(fos);
            fos.close();
        }
    }
}
Also used : CellReference(org.apache.poi.ss.util.CellReference) CTColor(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor) LinkedHashMap(java.util.LinkedHashMap) ThemeElement(org.apache.poi.xssf.model.ThemesTable.ThemeElement) XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Test(org.junit.Test)

Example 5 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project poi by apache.

the class TestThemesTable method themedAndNonThemedColours.

/**
     * Ensure that, for a file with themes, we can correctly
     *  read both the themed and non-themed colours back.
     * Column A = Theme Foreground
     * Column B = Theme Foreground
     * Column C = Explicit Colour Foreground
     * Column E = Explicit Colour Background, Black Foreground
     * Column G = Conditional Formatting Backgrounds
     * 
     * Note - Grey Row has an odd way of doing the styling... 
     */
@Test
public void themedAndNonThemedColours() {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
    XSSFSheet sheet = wb.getSheetAt(0);
    XSSFCellStyle style;
    XSSFColor color;
    XSSFCell cell;
    String[] names = { "White", "Black", "Grey", "Dark Blue", "Blue", "Red", "Green" };
    String[] explicitFHexes = { "FFFFFFFF", "FF000000", "FFC0C0C0", "FF002060", "FF0070C0", "FFFF0000", "FF00B050" };
    String[] explicitBHexes = { "FFFFFFFF", "FF000000", "FFC0C0C0", "FF002060", "FF0000FF", "FFFF0000", "FF00FF00" };
    assertEquals(7, names.length);
    // Check the non-CF colours in Columns A, B, C and E
    for (int rn = 1; rn < 8; rn++) {
        int idx = rn - 1;
        XSSFRow row = sheet.getRow(rn);
        assertNotNull("Missing row " + rn, row);
        // Theme cells come first
        XSSFCell themeCell = row.getCell(0);
        ThemeElement themeElem = ThemeElement.byId(idx);
        assertCellContents(themeElem.name, themeCell);
        // Sanity check names
        assertCellContents(names[idx], row.getCell(1));
        assertCellContents(names[idx], row.getCell(2));
        assertCellContents(names[idx], row.getCell(4));
        // Check the colours
        //  A: Theme Based, Foreground
        style = themeCell.getCellStyle();
        color = style.getFont().getXSSFColor();
        assertEquals(true, color.isThemed());
        assertEquals(idx, color.getTheme());
        assertEquals(rgbExpected[idx], Hex.encodeHexString(color.getRGB()));
        //  B: Theme Based, Foreground
        cell = row.getCell(1);
        style = cell.getCellStyle();
        color = style.getFont().getXSSFColor();
        assertEquals(true, color.isThemed());
        if (idx != 2) {
            assertEquals(idx, color.getTheme());
            assertEquals(rgbExpected[idx], Hex.encodeHexString(color.getRGB()));
        } else {
            assertEquals(1, color.getTheme());
            assertEquals(0.50, color.getTint(), 0.001);
        }
        //  C: Explicit, Foreground
        cell = row.getCell(2);
        style = cell.getCellStyle();
        color = style.getFont().getXSSFColor();
        assertEquals(false, color.isThemed());
        assertEquals(explicitFHexes[idx], color.getARGBHex());
        // E: Explicit Background, Foreground all Black
        cell = row.getCell(4);
        style = cell.getCellStyle();
        color = style.getFont().getXSSFColor();
        assertEquals(true, color.isThemed());
        assertEquals("FF000000", color.getARGBHex());
        color = style.getFillForegroundXSSFColor();
        assertEquals(false, color.isThemed());
        assertEquals(explicitBHexes[idx], color.getARGBHex());
        color = style.getFillBackgroundColorColor();
        assertEquals(false, color.isThemed());
        assertEquals(null, color.getARGBHex());
    }
// Check the CF colours
// TODO
}
Also used : XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) ThemeElement(org.apache.poi.xssf.model.ThemesTable.ThemeElement) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Test(org.junit.Test)

Aggregations

XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)59 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)39 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)39 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)35 FileOutputStream (java.io.FileOutputStream)17 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)15 FileInputStream (java.io.FileInputStream)14 BufferedReader (java.io.BufferedReader)10 FileNotFoundException (java.io.FileNotFoundException)10 Test (org.junit.Test)10 InputStreamReader (java.io.InputStreamReader)9 Reader (java.io.Reader)9 File (java.io.File)8 HashMap (java.util.HashMap)8 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)8 BufferedWriter (java.io.BufferedWriter)6 FileWriter (java.io.FileWriter)6 HashSet (java.util.HashSet)5 Map (java.util.Map)5