Search in sources :

Example 21 with HSSFWorkbook

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

the class EmbeddedObjects method main.

public static void main(String[] args) throws Exception {
    XSSFWorkbook workbook = new XSSFWorkbook(args[0]);
    for (PackagePart pPart : workbook.getAllEmbedds()) {
        String contentType = pPart.getContentType();
        InputStream is = pPart.getInputStream();
        Closeable document;
        if (contentType.equals("application/vnd.ms-excel")) {
            // Excel Workbook - either binary or OpenXML
            document = new HSSFWorkbook(is);
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
            // Excel Workbook - OpenXML file format
            document = new XSSFWorkbook(is);
        } else if (contentType.equals("application/msword")) {
            // Word Document - binary (OLE2CDF) file format
            document = new HWPFDocument(is);
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
            // Word Document - OpenXML file format
            document = new XWPFDocument(is);
        } else if (contentType.equals("application/vnd.ms-powerpoint")) {
            // PowerPoint Document - binary file format
            document = new HSLFSlideShow(is);
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
            // PowerPoint Document - OpenXML file format
            document = new XMLSlideShow(is);
        } else {
            // Any other type of embedded object.
            document = is;
        }
        document.close();
        is.close();
    }
    workbook.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) InputStream(java.io.InputStream) Closeable(java.io.Closeable) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 22 with HSSFWorkbook

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

the class DrawingDump method main.

public static void main(String[] args) throws IOException {
    OutputStreamWriter osw = new OutputStreamWriter(System.out, Charset.defaultCharset());
    PrintWriter pw = new PrintWriter(osw);
    NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(args[0]));
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    try {
        pw.println("Drawing group:");
        wb.dumpDrawingGroupRecords(true);
        int i = 1;
        for (Sheet sheet : wb) {
            pw.println("Sheet " + i + "(" + sheet.getSheetName() + "):");
            ((HSSFSheet) sheet).dumpDrawingRecords(true, pw);
        }
    } finally {
        wb.close();
        fs.close();
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OutputStreamWriter(java.io.OutputStreamWriter) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) PrintWriter(java.io.PrintWriter)

Example 23 with HSSFWorkbook

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

the class TestProper method testValidHSSF.

@Test
public void testValidHSSF() {
    HSSFWorkbook wb = new HSSFWorkbook();
    evaluator = new HSSFFormulaEvaluator(wb);
    confirm(wb);
}
Also used : HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 24 with HSSFWorkbook

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

the class TestExcelConverterSuite method testHtml.

@Test
public void testHtml() throws Exception {
    HSSFWorkbook workbook;
    try {
        workbook = ExcelToHtmlUtils.loadXls(child);
    } catch (Exception exc) {
        // unable to parse file -- not ExcelToFoConverter fault
        return;
    }
    ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument());
    excelToHtmlConverter.processWorkbook(workbook);
    StringWriter stringWriter = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "html");
    transformer.transform(new DOMSource(excelToHtmlConverter.getDocument()), new StreamResult(stringWriter));
    assertNotNull(stringWriter.toString());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 25 with HSSFWorkbook

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

the class TestVariantSupport method newNumberTypes.

@Test
public void newNumberTypes() throws Exception {
    ClipboardData cd = new ClipboardData();
    cd.setValue(new byte[10]);
    Object[][] exp = { { Variant.VT_CF, cd.toByteArray() }, { Variant.VT_BOOL, true }, { Variant.VT_LPSTR, "codepagestring" }, { Variant.VT_LPWSTR, "widestring" }, // int, not short ... :(
    { Variant.VT_I2, -1 }, { Variant.VT_UI2, 0xFFFF }, { Variant.VT_I4, -1 }, { Variant.VT_UI4, 0xFFFFFFFFL }, { Variant.VT_I8, -1L }, { Variant.VT_UI8, BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN) }, { Variant.VT_R4, -999.99f }, { Variant.VT_R8, -999.99d } };
    HSSFWorkbook wb = new HSSFWorkbook();
    POIFSFileSystem poifs = new POIFSFileSystem();
    DocumentSummaryInformation dsi = PropertySetFactory.newDocumentSummaryInformation();
    CustomProperties cpList = new CustomProperties();
    for (Object[] o : exp) {
        int type = (Integer) o[0];
        Property p = new Property(PropertyIDMap.PID_MAX + type, type, o[1]);
        cpList.put("testprop" + type, new CustomProperty(p, "testprop" + type));
    }
    dsi.setCustomProperties(cpList);
    dsi.write(poifs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    poifs.writeFilesystem(bos);
    poifs.close();
    poifs = new POIFSFileSystem(new ByteArrayInputStream(bos.toByteArray()));
    dsi = (DocumentSummaryInformation) PropertySetFactory.create(poifs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    cpList = dsi.getCustomProperties();
    int i = 0;
    for (Object[] o : exp) {
        Object obj = cpList.get("testprop" + o[0]);
        if (o[1] instanceof byte[]) {
            assertArrayEquals("property " + i, (byte[]) o[1], (byte[]) obj);
        } else {
            assertEquals("property " + i, o[1], obj);
        }
        i++;
    }
    poifs.close();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) BigInteger(java.math.BigInteger) LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) Test(org.junit.Test)

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