Search in sources :

Example 6 with HSSFWorkbook

use of org.apache.poi.hssf.usermodel.HSSFWorkbook 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 7 with HSSFWorkbook

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

the class NewSheet method main.

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    wb.createSheet("new sheet");
    // create with default name
    wb.createSheet();
    final String name = "second sheet";
    // setting sheet name later
    wb.setSheetName(1, WorkbookUtil.createSafeSheetName(name));
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 8 with HSSFWorkbook

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

the class NewWorkbook method main.

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 9 with HSSFWorkbook

use of org.apache.poi.hssf.usermodel.HSSFWorkbook 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 10 with HSSFWorkbook

use of org.apache.poi.hssf.usermodel.HSSFWorkbook 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

HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)532 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)189 Test (org.junit.Test)173 Workbook (org.apache.poi.ss.usermodel.Workbook)151 Sheet (org.apache.poi.ss.usermodel.Sheet)138 Row (org.apache.poi.ss.usermodel.Row)113 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)102 Cell (org.apache.poi.ss.usermodel.Cell)101 FileOutputStream (java.io.FileOutputStream)99 IOException (java.io.IOException)99 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)79 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)72 FileInputStream (java.io.FileInputStream)61 File (java.io.File)59 ArrayList (java.util.ArrayList)52 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)38 InputStream (java.io.InputStream)34 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)32 CellStyle (org.apache.poi.ss.usermodel.CellStyle)31 OutputStream (java.io.OutputStream)28