Search in sources :

Example 11 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class WorkingWithPictures method main.

public static void main(String[] args) throws IOException {
    //create a new workbook
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    try {
        CreationHelper helper = wb.getCreationHelper();
        //add a picture in this workbook.
        InputStream is = new FileInputStream(args[0]);
        byte[] bytes = IOUtils.toByteArray(is);
        is.close();
        int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
        //create sheet
        Sheet sheet = wb.createSheet();
        //create drawing
        Drawing<?> drawing = sheet.createDrawingPatriarch();
        //add a picture shape
        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setCol1(1);
        anchor.setRow1(1);
        Picture pict = drawing.createPicture(anchor, pictureIdx);
        //auto-size picture
        pict.resize(2);
        //save workbook
        String file = "picture.xls";
        if (wb instanceof XSSFWorkbook) {
            // NOSONAR
            file += "x";
        }
        OutputStream fileOut = new FileOutputStream(file);
        try {
            wb.write(fileOut);
        } finally {
            fileOut.close();
        }
    } finally {
        wb.close();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) FileInputStream(java.io.FileInputStream) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) Picture(org.apache.poi.ss.usermodel.Picture) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 12 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class UpdateEmbeddedDoc method checkUpdatedDoc.

/**
     * Called to test whether or not the embedded workbook was correctly
     * updated. This method simply recovers the first cell from the first row
     * of the first workbook and tests the value it contains.
     * <p/>
     * Note that execution will not continue up to the assertion as the
     * embedded workbook is now corrupted and causes an IllegalArgumentException
     * with the following message
     * <p/>
     * <em>java.lang.IllegalArgumentException: Your InputStream was neither an
     * OLE2 stream, nor an OOXML stream</em>
     * <p/>
     * to be thrown when the WorkbookFactory.createWorkbook(InputStream) method
     * is executed.
     *
     * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
     *                             Rather
     *                             than use the specific classes (HSSF/XSSF) to handle the embedded
     *                             workbook this method uses those defeined in the SS stream. As
     *                             a result, it might be the case that a SpreadsheetML file is
     *                             opened for processing, throwing this exception if that file is
     *                             invalid.
     * @throws java.io.IOException Thrown if a problem occurs in the underlying
     *                             file system.
     */
public void checkUpdatedDoc() throws OpenXML4JException, IOException {
    for (PackagePart pPart : this.doc.getAllEmbedds()) {
        String ext = pPart.getPartName().getExtension();
        if (BINARY_EXTENSION.equals(ext) || OPENXML_EXTENSION.equals(ext)) {
            InputStream is = pPart.getInputStream();
            Workbook workbook = null;
            try {
                workbook = WorkbookFactory.create(is);
                Sheet sheet = workbook.getSheetAt(SHEET_NUM);
                Row row = sheet.getRow(ROW_NUM);
                Cell cell = row.getCell(CELL_NUM);
                assertEquals(cell.getNumericCellValue(), NEW_VALUE, 0.0001);
            } finally {
                IOUtils.closeQuietly(workbook);
                IOUtils.closeQuietly(is);
            }
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Row(org.apache.poi.ss.usermodel.Row) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 13 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class TestExcelAntWorkbookUtil method testGetWorkbook.

public void testGetWorkbook() {
    fixture = new ExcelAntWorkbookUtilTestHelper(mortgageCalculatorFileName);
    assertNotNull(fixture);
    Workbook workbook = fixture.getWorkbook();
    assertNotNull(workbook);
}
Also used : Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 14 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class TestExcelAntWorkbookUtil method testWorkbookConstructor.

public void testWorkbookConstructor() throws InvalidFormatException, IOException {
    File workbookFile = new File(mortgageCalculatorFileName);
    FileInputStream fis = new FileInputStream(workbookFile);
    Workbook workbook = WorkbookFactory.create(fis);
    fixture = new ExcelAntWorkbookUtilTestHelper(workbook);
    assertNotNull(fixture);
}
Also used : File(java.io.File) FileInputStream(java.io.FileInputStream) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 15 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class CellStyleDetails method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        throw new IllegalArgumentException("Filename must be given");
    }
    Workbook wb = WorkbookFactory.create(new File(args[0]));
    DataFormatter formatter = new DataFormatter();
    for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
        Sheet sheet = wb.getSheetAt(sn);
        System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());
        for (Row row : sheet) {
            System.out.println("  Row " + row.getRowNum());
            for (Cell cell : row) {
                CellReference ref = new CellReference(cell);
                System.out.print("    " + ref.formatAsString());
                System.out.print(" (" + cell.getColumnIndex() + ") ");
                CellStyle style = cell.getCellStyle();
                System.out.print("Format=" + style.getDataFormatString() + " ");
                System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
                System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");
                Font font = wb.getFontAt(style.getFontIndex());
                System.out.print("Font=" + font.getFontName() + " ");
                System.out.print("FontColor=");
                if (font instanceof HSSFFont) {
                    System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb)));
                }
                if (font instanceof XSSFFont) {
                    System.out.print(renderColor(((XSSFFont) font).getXSSFColor()));
                }
                System.out.println();
                System.out.println("        " + formatter.formatCellValue(cell));
            }
        }
        System.out.println();
    }
    wb.close();
}
Also used : CellReference(org.apache.poi.ss.util.CellReference) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) Font(org.apache.poi.ss.usermodel.Font) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter)

Aggregations

Workbook (org.apache.poi.ss.usermodel.Workbook)296 Sheet (org.apache.poi.ss.usermodel.Sheet)209 Test (org.junit.Test)187 Cell (org.apache.poi.ss.usermodel.Cell)139 Row (org.apache.poi.ss.usermodel.Row)121 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)95 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)90 FileOutputStream (java.io.FileOutputStream)33 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)30 CellStyle (org.apache.poi.ss.usermodel.CellStyle)25 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)24 File (java.io.File)22 IOException (java.io.IOException)22 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)17 InternalSheet (org.apache.poi.hssf.model.InternalSheet)15 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)15 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)15 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)14