Example usage for org.apache.poi.xssf.usermodel XSSFSheet getPackagePart

List of usage examples for org.apache.poi.xssf.usermodel XSSFSheet getPackagePart

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFSheet getPackagePart.

Prototype

public final PackagePart getPackagePart() 

Source Link

Document

Provides access to the underlying PackagePart

Usage

From source file:br.com.sose.utils.BigGridDemo_temp.java

License:Apache License

public static void main(String[] args) throws Exception {

    // Step 1. Create a template file. Setup sheets and workbook-level objects such as
    // cell styles, number formats, etc.

    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet("Big Grid");

    Map<String, XSSFCellStyle> styles = createStyles(wb);
    //name of the zip entry holding sheet data, e.g. /xl/worksheets/sheet1.xml
    String sheetRef = sheet.getPackagePart().getPartName().getName();

    //save the template
    FileOutputStream os = new FileOutputStream("template.xlsx");
    wb.write(os);/*  ww  w .j a  v  a 2  s .c  om*/
    os.close();

    //Step 2. Generate XML file.
    File tmp = File.createTempFile("sheet", ".xml");
    Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING);
    generate(fw, styles);
    fw.close();

    //Step 3. Substitute the template entry with the generated data
    FileOutputStream out = new FileOutputStream("big-grid.xlsx");
    substitute(new File("template.xlsx"), tmp, sheetRef.substring(1), out);
    out.close();
}

From source file:com.philips.his.pixiu.cdr.poi.BigGridDemo.java

License:Apache License

public static void main(String[] args) throws Exception {

    // Step 1. Create a template file. Setup sheets and workbook-level objects such as
    // cell styles, number formats, etc.

    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet("Big Grid");

    Map<String, XSSFCellStyle> styles = createStyles(wb);
    // name of the zip entry holding sheet data, e.g. /xl/worksheets/sheet1.xml
    String sheetRef = sheet.getPackagePart().getPartName().getName();

    // save the template
    FileOutputStream os = new FileOutputStream("c:/temp/template.xlsx");
    wb.write(os);/*  www . j  a  v  a  2 s  .  c  o  m*/
    os.close();

    // Step 2. Generate XML file.
    File tmp = File.createTempFile("sheet", ".xml");
    Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING);
    generate(fw, styles);
    fw.close();

    // Step 3. Substitute the template entry with the generated data
    FileOutputStream out = new FileOutputStream("c:/temp/big-grid.xlsx");
    substitute(new File("c:/temp/template.xlsx"), tmp, sheetRef.substring(1), out);
    out.close();

    wb.close();
}

From source file:com.vodafone.poms.ii.helpers.ExportManager.java

private static String addFile(XSSFSheet sh, String filePath, double oleId)
        throws IOException, InvalidFormatException {
    File file = new File(filePath);
    FileInputStream fin = new FileInputStream(file);
    byte[] data;/*from w  w  w. j  a  va  2s.c om*/
    data = new byte[fin.available()];
    fin.read(data);
    Ole10Native ole10 = new Ole10Native(file.getAbsolutePath(), file.getAbsolutePath(), file.getAbsolutePath(),
            data);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(500);
    ole10.writeOut(bos);

    POIFSFileSystem poifs = new POIFSFileSystem();
    poifs.getRoot().createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));

    poifs.getRoot().setStorageClsid(ClassID.OLE10_PACKAGE);

    final PackagePartName pnOLE = PackagingURIHelper
            .createPartName("/xl/embeddings/oleObject" + oleId + Math.random() + ".bin");
    final PackagePart partOLE = sh.getWorkbook().getPackage().createPart(pnOLE,
            "application/vnd.openxmlformats-officedocument.oleObject");
    PackageRelationship prOLE = sh.getPackagePart().addRelationship(pnOLE, TargetMode.INTERNAL,
            POIXMLDocument.OLE_OBJECT_REL_TYPE);
    OutputStream os = partOLE.getOutputStream();
    poifs.writeFilesystem(os);
    os.close();
    poifs.close();

    return prOLE.getId();

}

From source file:com.vodafone.poms.ii.helpers.ExportManager.java

private static String addImageToSheet(XSSFSheet sh, int imgId, int pictureType) throws InvalidFormatException {
    final PackagePartName pnIMG = PackagingURIHelper.createPartName(
            "/xl/media/image" + (imgId + 1) + (pictureType == Workbook.PICTURE_TYPE_PNG ? ".png" : ".jpeg"));
    PackageRelationship prIMG = sh.getPackagePart().addRelationship(pnIMG, TargetMode.INTERNAL,
            PackageRelationshipTypes.IMAGE_PART);
    return prIMG.getId();
}

From source file:es.tena.foundation.util.POIUtil.java

public static void generateXLS(String tabla, String filename, Connection conn, String encoding)
        throws SQLException {
    String query = "";
    try {/* w  ww.j a va 2s  . c om*/
        query = "SELECT * FROM (" + tabla + ")";
        PreparedStatement stmt = conn.prepareStatement(query);
        ResultSet rset = stmt.executeQuery();

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet(filename);
        String sheetRef = sheet.getPackagePart().getPartName().getName();
        String template = "c:\\temp\\template_" + filename + ".xlsx";
        FileOutputStream os = new FileOutputStream(template);
        wb.write(os);
        os.close();

        File tmp = File.createTempFile("sheet", ".xml");
        Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), encoding);
        generate(fw, rset, encoding);
        rset.close();
        stmt.close();
        fw.close();

        FileOutputStream out = new FileOutputStream(
                "c:\\temp\\" + filename + sdf.format(calendario.getTime()) + ".xlsx");
        FileUtil.substitute(new File(template), tmp, sheetRef.substring(1), out);
        out.close();
        Logger.getLogger(POIUtil.class.getName()).log(Level.INFO, "Creado con exito {0}", filename);
    } catch (Exception ex) {
        ex.printStackTrace();
        Logger.getLogger(POIUtil.class.getName()).log(Level.SEVERE, null, query + "\n" + ex);
        System.out.println(query);
    } finally {
        conn.close();
    }
}

From source file:net.openchrom.msd.converter.supplier.excel.io.ChromatogramWriter.java

License:Open Source License

@Override
public void writeChromatogram(File file, IChromatogramMSD chromatogram, IProgressMonitor monitor)
        throws FileNotFoundException, FileIsNotWriteableException, IOException {

    XSSFWorkbook excelWorkbook = new XSSFWorkbook();
    XSSFSheet excelSheet = excelWorkbook.createSheet(chromatogram.getName());
    Map<String, XSSFCellStyle> styles = createStyles(excelWorkbook);
    String excelSheetReferenceName = excelSheet.getPackagePart().getPartName().getName();
    /*/* ww  w . jav  a2  s.  c om*/
     * Excel Template
     */
    File excelTemplate = new File(PathHelper.getStoragePath() + File.separator + "exceltemplace.xlsx");
    FileOutputStream outputStream = new FileOutputStream(excelTemplate);
    excelWorkbook.write(outputStream);
    outputStream.close();
    /*
     * Temporary XML file
     */
    File xmlDataFile = new File(PathHelper.getStoragePath() + File.separator + "datafile.xml");
    Writer writer = new OutputStreamWriter(new FileOutputStream(xmlDataFile), XML_ENCODING);
    try {
        writeChromatogram(writer, styles, chromatogram, monitor);
    } catch (Exception e) {
        throw new IOException("There has something gone wrong writing the temporary data file.");
    }
    writer.close();
    /*
     * Merge the template and the data file.
     */
    FileOutputStream resultOutputStream = new FileOutputStream(file);
    mergeTemplateAndData(excelTemplate, xmlDataFile, excelSheetReferenceName.substring(1), resultOutputStream);
    resultOutputStream.close();
    /*
     * Delete the temporary files.
     */
    excelTemplate.delete();
    xmlDataFile.delete();
}

From source file:packtest.BigGridDemo.java

License:Apache License

public static void main(String[] args) throws Exception {

    // Step 1. Create a template file. Setup sheets and workbook-level objects such as
    // cell styles, number formats, etc.

    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet("Big Grid");

    Map<String, XSSFCellStyle> styles = createStyles(wb);
    //name of the zip entry holding sheet data, e.g. /xl/worksheets/sheet1.xml
    String sheetRef = sheet.getPackagePart().getPartName().getName();

    //save the template
    FileOutputStream os = new FileOutputStream("template.xlsx");
    wb.write(os);// www .  j  a  va2 s  . co m
    os.close();

    //Step 2. Generate XML file.
    File tmp = File.createTempFile("sheet", ".xml");
    Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING);
    generate(fw, styles);
    fw.close();

    //Step 3. Substitute the template entry with the generated data
    FileOutputStream out = new FileOutputStream(Utils.getPath("big-grid.xlsx"));
    substitute(new File(Utils.getPath("template.xlsx")), tmp, sheetRef.substring(1), out);
    out.close();

    wb.close();
}