Search in sources :

Example 16 with HSSFSheet

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

the class HyperlinkFormula method main.

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellType(CellType.FORMULA);
    cell.setCellFormula("HYPERLINK(\"http://127.0.0.1:8080/toto/truc/index.html?test=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\", \"test\")");
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : 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 17 with HSSFSheet

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

the class TestOSGiBundle method testHSSF.

@Test
public void testHSSF() throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet("OSGi");
    s.createRow(0).createCell(0).setCellValue("With OSGi");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    wb.write(baos);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    wb = new HSSFWorkbook(bais);
    assertEquals(1, wb.getNumberOfSheets());
    s = wb.getSheet("OSGi");
    assertEquals("With OSGi", s.getRow(0).getCell(0).toString());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 18 with HSSFSheet

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

the class NewLinesInCells method main.

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    HSSFRow r = null;
    HSSFCell c = null;
    HSSFCellStyle cs = wb.createCellStyle();
    HSSFFont f2 = wb.createFont();
    cs = wb.createCellStyle();
    cs.setFont(f2);
    // Word Wrap MUST be turned on
    cs.setWrapText(true);
    r = s.createRow(2);
    r.setHeight((short) 0x349);
    c = r.createCell(2);
    c.setCellType(CellType.STRING);
    c.setCellValue("Use \n with word wrap on to create a new line");
    c.setCellStyle(cs);
    s.setColumnWidth(2, (int) ((50 * 8) / ((double) 1 / 20)));
    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) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 19 with HSSFSheet

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

the class InCellLists method demonstrateMethodCalls.

/**
     * Call each of the list creation methods.
     *
     * @param outputFilename A String that encapsulates the name of and path to
     *                       the Excel spreadsheet file this code will create.
     */
public void demonstrateMethodCalls(String outputFilename) throws IOException {
    HSSFWorkbook workbook = new HSSFWorkbook();
    try {
        HSSFSheet sheet = workbook.createSheet("In Cell Lists");
        HSSFRow row = sheet.createRow(0);
        // Create a cell at A1 and insert a single, bulleted, item into
        // that cell.
        HSSFCell cell = row.createCell(0);
        this.bulletedItemInCell(workbook, "List Item", cell);
        // Create a cell at A2 and insert a plain list - that is one
        // whose items are neither bulleted or numbered - into that cell.
        row = sheet.createRow(1);
        cell = row.createCell(0);
        ArrayList<String> listItems = new ArrayList<String>();
        listItems.add("List Item One.");
        listItems.add("List Item Two.");
        listItems.add("List Item Three.");
        listItems.add("List Item Four.");
        this.listInCell(workbook, listItems, cell);
        // The row height and cell width are set here to ensure that the
        // list may be seen.
        row.setHeight((short) 1100);
        sheet.setColumnWidth(0, 9500);
        // Create a cell at A3 and insert a numbered list into that cell.
        // Note that a couple of items have been added to the listItems
        // ArrayList
        row = sheet.createRow(2);
        cell = row.createCell(0);
        listItems.add("List Item Five.");
        listItems.add("List Item Six.");
        this.numberedListInCell(workbook, listItems, cell, 1, 2);
        row.setHeight((short) 1550);
        // Create a cell at A4 and insert a numbered list into that cell.
        // Note that a couple of items have been added to the listItems
        // ArrayList
        row = sheet.createRow(3);
        cell = row.createCell(0);
        listItems.add("List Item Seven.");
        listItems.add("List Item Eight.");
        listItems.add("List Item Nine.");
        listItems.add("List Item Ten.");
        this.bulletedListInCell(workbook, listItems, cell);
        row.setHeight((short) 2550);
        // Insert a plain, multi-level list into cell A5. Note that
        // the major difference here is that the list items are passed as
        // an ArrayList of MultiLevelListItems. Note that an ArrayList
        // of instances of an inner class was used here in preference to
        // a Hashtable or HashMap as the ArrayList will preserve the
        // ordering of the items added to it; the first item added will
        // be the first item recovered and the last item added, the last
        // item recovered. Alternatively, a LinkedHashMap could be used
        // to preserve order.
        row = sheet.createRow(4);
        cell = row.createCell(0);
        ArrayList<MultiLevelListItem> multiLevelListItems = new ArrayList<MultiLevelListItem>();
        listItems = new ArrayList<String>();
        listItems.add("ML List Item One - Sub Item One.");
        listItems.add("ML List Item One - Sub Item Two.");
        listItems.add("ML List Item One - Sub Item Three.");
        listItems.add("ML List Item One - Sub Item Four.");
        multiLevelListItems.add(new MultiLevelListItem("List Item One.", listItems));
        // Passing either null or an empty ArrayList will signal that
        // there are no lower level items associated with the top level
        // item
        multiLevelListItems.add(new MultiLevelListItem("List Item Two.", null));
        multiLevelListItems.add(new MultiLevelListItem("List Item Three.", null));
        listItems = new ArrayList<String>();
        listItems.add("ML List Item Four - Sub Item One.");
        listItems.add("ML List Item Four - Sub Item Two.");
        listItems.add("ML List Item Four - Sub Item Three.");
        multiLevelListItems.add(new MultiLevelListItem("List Item Four.", listItems));
        this.multiLevelListInCell(workbook, multiLevelListItems, cell);
        row.setHeight((short) 2800);
        // Insert a numbered multi-level list into cell A6. Note that the
        // same ArrayList as constructed for the above plain multi-level
        // list example will be re-used
        row = sheet.createRow(5);
        cell = row.createCell(0);
        this.multiLevelNumberedListInCell(workbook, multiLevelListItems, cell, 1, 1, 1, 2);
        row.setHeight((short) 2800);
        // Insert a numbered multi-level list into cell A7. Note that the
        // same ArrayList as constructed for the plain multi-level list
        // example will be re-used
        row = sheet.createRow(6);
        cell = row.createCell(0);
        this.multiLevelBulletedListInCell(workbook, multiLevelListItems, cell);
        row.setHeight((short) 2800);
        // Save the completed workbook
        FileOutputStream fos = new FileOutputStream(new File(outputFilename));
        try {
            workbook.write(fos);
        } finally {
            fos.close();
        }
    } catch (FileNotFoundException fnfEx) {
        System.out.println("Caught a: " + fnfEx.getClass().getName());
        System.out.println("Message: " + fnfEx.getMessage());
        System.out.println("Stacktrace follows...........");
        fnfEx.printStackTrace(System.out);
    } catch (IOException ioEx) {
        System.out.println("Caught a: " + ioEx.getClass().getName());
        System.out.println("Message: " + ioEx.getMessage());
        System.out.println("Stacktrace follows...........");
        ioEx.printStackTrace(System.out);
    } finally {
        workbook.close();
    }
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) IOException(java.io.IOException) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) FileOutputStream(java.io.FileOutputStream) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) File(java.io.File)

Example 20 with HSSFSheet

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

the class WorkingWithFonts 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);
    // Create a new font and alter it.
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 24);
    font.setFontName("Courier New");
    font.setItalic(true);
    font.setStrikeout(true);
    // Fonts are set into a style so create a new one to use.
    HSSFCellStyle style = wb.createCellStyle();
    style.setFont(font);
    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell(1);
    cell.setCellValue("This is a test of fonts");
    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) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Aggregations

HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)157 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)138 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)70 Test (org.junit.Test)65 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)58 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)27 FileOutputStream (java.io.FileOutputStream)23 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)20 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)16 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)14 EscherAggregate (org.apache.poi.hssf.record.EscherAggregate)13 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)12 ArrayList (java.util.ArrayList)11 AssertionFailedError (junit.framework.AssertionFailedError)11 CellValue (org.apache.poi.ss.usermodel.CellValue)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 InputStream (java.io.InputStream)8 HashMap (java.util.HashMap)8 RecordBase (org.apache.poi.hssf.record.RecordBase)8 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)8