Search in sources :

Example 86 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell in project poi by apache.

the class FrillsAndFills method main.

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow(1);
    // Aqua background
    HSSFCellStyle style = wb.createCellStyle();
    style.setFillBackgroundColor(HSSFColorPredefined.AQUA.getIndex());
    style.setFillPattern(FillPatternType.BIG_SPOTS);
    HSSFCell cell = row.createCell(1);
    cell.setCellValue("X");
    cell.setCellStyle(style);
    // Orange "foreground", foreground being the fill foreground not the font color.
    style = wb.createCellStyle();
    style.setFillForegroundColor(HSSFColorPredefined.ORANGE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    cell = row.createCell(2);
    cell.setCellValue("X");
    cell.setCellStyle(style);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) 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) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 87 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell in project poi by apache.

the class RepeatingRowsAndColumns method main.

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("first sheet");
    HSSFSheet sheet2 = wb.createSheet("second sheet");
    HSSFSheet sheet3 = wb.createSheet("third sheet");
    HSSFFont boldFont = wb.createFont();
    boldFont.setFontHeightInPoints((short) 22);
    boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    HSSFCellStyle boldStyle = wb.createCellStyle();
    boldStyle.setFont(boldFont);
    HSSFRow row = sheet1.createRow(1);
    HSSFCell cell = row.createCell(0);
    cell.setCellValue("This quick brown fox");
    cell.setCellStyle(boldStyle);
    // Set the columns to repeat from column 0 to 2 on the first sheet
    sheet1.setRepeatingColumns(CellRangeAddress.valueOf("A:C"));
    // Set the rows to repeat from row 0 to 2 on the second sheet.
    sheet2.setRepeatingRows(CellRangeAddress.valueOf("1:3"));
    // Set the the repeating rows and columns on the third sheet.
    CellRangeAddress cra = CellRangeAddress.valueOf("D1:E2");
    sheet3.setRepeatingColumns(cra);
    sheet3.setRepeatingRows(cra);
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) 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) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 88 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell in project poi by apache.

the class SVTableModel method getValueAt.

@Override
public Object getValueAt(int row, int col) {
    HSSFRow r = st.getRow(row);
    HSSFCell c = null;
    if (r != null) {
        c = r.getCell(col);
    }
    return c;
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow)

Example 89 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell in project poi by apache.

the class ExcelToFoConverter method processRow.

/**
     * @return maximum 1-base index of column that were rendered, zero if none
     */
protected int processRow(HSSFWorkbook workbook, CellRangeAddress[][] mergedRanges, HSSFRow row, Element tableRowElement) {
    final HSSFSheet sheet = row.getSheet();
    final short maxColIx = row.getLastCellNum();
    if (maxColIx <= 0) {
        return 0;
    }
    final List<Element> emptyCells = new ArrayList<Element>(maxColIx);
    if (isOutputRowNumbers()) {
        Element tableRowNumberCellElement = processRowNumber(row);
        emptyCells.add(tableRowNumberCellElement);
    }
    int maxRenderedColumn = 0;
    for (int colIx = 0; colIx < maxColIx; colIx++) {
        if (!isOutputHiddenColumns() && sheet.isColumnHidden(colIx))
            continue;
        CellRangeAddress range = ExcelToHtmlUtils.getMergedRange(mergedRanges, row.getRowNum(), colIx);
        if (range != null && (range.getFirstColumn() != colIx || range.getFirstRow() != row.getRowNum()))
            continue;
        HSSFCell cell = row.getCell(colIx);
        // spanning using overlapping blocks
        int divWidthPx = 0;
        {
            divWidthPx = getColumnWidth(sheet, colIx);
            boolean hasBreaks = false;
            for (int nextColumnIndex = colIx + 1; nextColumnIndex < maxColIx; nextColumnIndex++) {
                if (!isOutputHiddenColumns() && sheet.isColumnHidden(nextColumnIndex))
                    continue;
                if (row.getCell(nextColumnIndex) != null && !isTextEmpty(row.getCell(nextColumnIndex))) {
                    hasBreaks = true;
                    break;
                }
                divWidthPx += getColumnWidth(sheet, nextColumnIndex);
            }
            if (!hasBreaks)
                divWidthPx = Integer.MAX_VALUE;
        }
        Element tableCellElement = foDocumentFacade.createTableCell();
        if (range != null) {
            if (range.getFirstColumn() != range.getLastColumn())
                tableCellElement.setAttribute("number-columns-spanned", String.valueOf(range.getLastColumn() - range.getFirstColumn() + 1));
            if (range.getFirstRow() != range.getLastRow())
                tableCellElement.setAttribute("number-rows-spanned", String.valueOf(range.getLastRow() - range.getFirstRow() + 1));
        }
        boolean emptyCell;
        if (cell != null) {
            emptyCell = processCell(workbook, cell, tableCellElement, getColumnWidth(sheet, colIx), divWidthPx, row.getHeight() / 20f);
        } else {
            tableCellElement.appendChild(foDocumentFacade.createBlock());
            emptyCell = true;
        }
        if (emptyCell) {
            emptyCells.add(tableCellElement);
        } else {
            for (Element emptyCellElement : emptyCells) {
                tableRowElement.appendChild(emptyCellElement);
            }
            emptyCells.clear();
            tableRowElement.appendChild(tableCellElement);
            maxRenderedColumn = colIx;
        }
    }
    return maxRenderedColumn + 1;
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 90 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell in project poi by apache.

the class TestIndirect method testMultipleWorkbooks.

@Test
public void testMultipleWorkbooks() throws Exception {
    HSSFWorkbook wbA = createWBA();
    HSSFCell cellA = wbA.getSheetAt(0).createRow(10).createCell(0);
    HSSFFormulaEvaluator feA = new HSSFFormulaEvaluator(wbA);
    HSSFWorkbook wbB = createWBB();
    HSSFCell cellB = wbB.getSheetAt(0).createRow(10).createCell(0);
    HSSFFormulaEvaluator feB = new HSSFFormulaEvaluator(wbB);
    String[] workbookNames = { "MyBook", "Figures for January" };
    HSSFFormulaEvaluator[] evaluators = { feA, feB };
    HSSFFormulaEvaluator.setupEnvironment(workbookNames, evaluators);
    // same wb
    confirm(feB, cellB, "INDIRECT(\"'[Figures for January]## Look here!'!A1\")", 42);
    // across workbooks
    confirm(feA, cellA, "INDIRECT(\"'[Figures for January]## Look here!'!A1\")", 42);
    // 2 level recursion
    // set up (and check) first level
    confirm(feB, cellB, "INDIRECT(\"[MyBook]Sheet2!A1\")", 50);
    // points to cellB
    confirm(feA, cellA, "INDIRECT(\"'[Figures for January]Sheet1'!A11\")", 50);
    wbB.close();
    wbA.close();
}
Also used : HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Aggregations

HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)147 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)96 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)93 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)91 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)31 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)30 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)28 Test (org.junit.Test)25 FileOutputStream (java.io.FileOutputStream)24 IOException (java.io.IOException)18 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)16 File (java.io.File)13 CellValue (org.apache.poi.ss.usermodel.CellValue)13 ArrayList (java.util.ArrayList)11 AssertionFailedError (junit.framework.AssertionFailedError)10 HashMap (java.util.HashMap)9 OutputStream (java.io.OutputStream)8 Map (java.util.Map)8 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)8 FileInputStream (java.io.FileInputStream)6