Search in sources :

Example 1 with SpreadsheetMLPackage

use of org.docx4j.openpackaging.packages.SpreadsheetMLPackage in project com.revolsys.open by revolsys.

the class XlsxRecordReader method initDo.

@Override
protected void initDo() {
    super.initDo();
    try (InputStream in = this.resource.newBufferedInputStream()) {
        final SpreadsheetMLPackage spreadsheetPackage = (SpreadsheetMLPackage) OpcPackage.load(in);
        final DocPropsCustomPart customProperties = spreadsheetPackage.getDocPropsCustomPart();
        if (customProperties != null) {
            int srid = 0;
            try {
                srid = Integer.parseInt(customProperties.getProperty("srid").getLpwstr());
            } catch (final Throwable e) {
            }
            int axisCount = 2;
            try {
                axisCount = Integer.parseInt(customProperties.getProperty("axisCount").getLpwstr());
                if (axisCount > 4) {
                    axisCount = 2;
                }
            } catch (final Throwable e) {
            }
            double scaleXy = 0;
            try {
                scaleXy = Double.parseDouble(customProperties.getProperty("scaleXy").getLpwstr());
            } catch (final Throwable e) {
            }
            double scaleZ = 0;
            try {
                scaleZ = Double.parseDouble(customProperties.getProperty("scaleZ").getLpwstr());
            } catch (final Throwable e) {
            }
            final GeometryFactory geometryFactory = GeometryFactory.fixed(srid, axisCount, scaleXy, scaleXy, scaleZ);
            setGeometryFactory(geometryFactory);
        }
        WorksheetPart worksheetPart = null;
        for (final Part part : spreadsheetPackage.getParts().getParts().values()) {
            if (part instanceof WorksheetPart) {
                if (worksheetPart == null) {
                    worksheetPart = (WorksheetPart) part;
                }
            } else if (part instanceof SharedStrings) {
                final SharedStrings sharedStrings = (SharedStrings) part;
                final CTSst contents = sharedStrings.getContents();
                this.sharedStringList = contents.getSi();
            }
        }
        if (worksheetPart != null) {
            final Worksheet worksheet = worksheetPart.getContents();
            final SheetData sheetData = worksheet.getSheetData();
            this.rows = sheetData.getRow();
            final List<String> line = readNextRow();
            final String baseName = this.resource.getBaseName();
            newRecordDefinition(baseName, line);
        }
    } catch (final IOException | Docx4JException e) {
        Logs.error(this, "Unable to open " + this.resource, e);
    } catch (final NoSuchElementException e) {
    }
}
Also used : SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) InputStream(java.io.InputStream) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) Worksheet(org.xlsx4j.sml.Worksheet) IOException(java.io.IOException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) DocPropsCustomPart(org.docx4j.openpackaging.parts.DocPropsCustomPart) CTSst(org.xlsx4j.sml.CTSst) SheetData(org.xlsx4j.sml.SheetData) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) DocPropsCustomPart(org.docx4j.openpackaging.parts.DocPropsCustomPart) Part(org.docx4j.openpackaging.parts.Part) SharedStrings(org.docx4j.openpackaging.parts.SpreadsheetML.SharedStrings) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with SpreadsheetMLPackage

use of org.docx4j.openpackaging.packages.SpreadsheetMLPackage in project Aspose.Cells-for-Java by aspose-cells.

the class Xlsx4jAddComments method main.

/**
 * @param args
 */
public static void main(String[] args) {
    try {
        // The path to the documents directory.
        String dataDir = Utils.getDataDir(Xlsx4jAddComments.class);
        String outputfilepath = dataDir + "AddComments-Xlsx4j.xlsx";
        SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
        WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
        addContent(sheet);
        pkg.save(new File(outputfilepath));
        System.out.println("\n\n done .. " + outputfilepath);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) File(java.io.File) JAXBException(javax.xml.bind.JAXBException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Example 3 with SpreadsheetMLPackage

use of org.docx4j.openpackaging.packages.SpreadsheetMLPackage in project Aspose.Cells-for-Java by aspose-cells.

the class Xlsx4jAddImage method main.

public static void main(String[] args) throws Exception {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(Xlsx4jAddImage.class);
    String outputfilepath = dataDir + "AddImage-Xlsx4j.xlsx";
    String imagefilePath = dataDir + "greentick.png";
    SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
    WorksheetPart worksheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
    // Create Drawing part and add to sheet
    Drawing drawingPart = new Drawing();
    Relationship drawingRel = worksheet.addTargetPart(drawingPart);
    // Add anchor XML to worksheet
    org.xlsx4j.sml.CTDrawing drawing = org.xlsx4j.jaxb.Context.getsmlObjectFactory().createCTDrawing();
    worksheet.getJaxbElement().setDrawing(drawing);
    drawing.setId(drawingRel.getId());
    // Create image part and add to Drawing part
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(pkg, drawingPart, FileUtils.readFileToByteArray(new File(imagefilePath)));
    String imageRelID = imagePart.getSourceRelationship().getId();
    // Create and set drawing part content
    // Take your pick ..
    // .. build it using code
    // drawingPart.setJaxbElement(
    // buildDrawingPartContentUsingCode(imageRelID));
    // .. or build it from an XML string
    drawingPart.setJaxbElement(buildDrawingPartContentFromXmlString(imageRelID));
    // Save the xlsx
    SaveToZipFile saver = new SaveToZipFile(pkg);
    saver.save(outputfilepath);
    System.out.println("\n\n done .. " + outputfilepath);
}
Also used : CTDrawing(org.docx4j.dml.spreadsheetdrawing.CTDrawing) Drawing(org.docx4j.openpackaging.parts.DrawingML.Drawing) SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) Relationship(org.docx4j.relationships.Relationship) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) File(java.io.File)

Example 4 with SpreadsheetMLPackage

use of org.docx4j.openpackaging.packages.SpreadsheetMLPackage in project Aspose.Cells-for-Java by aspose-cells.

the class Xlsx4jNewSpreadSheet method main.

public static void main(String[] args) throws Exception {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(Xlsx4jNewSpreadSheet.class);
    String outputfilepath = dataDir + "newWorksheet_Xlsx4j.xlsx";
    SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
    WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
    addContent(sheet);
    SaveToZipFile saver = new SaveToZipFile(pkg);
    saver.save(outputfilepath);
    System.out.println("\n\n done .. " + outputfilepath);
}
Also used : SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile)

Example 5 with SpreadsheetMLPackage

use of org.docx4j.openpackaging.packages.SpreadsheetMLPackage in project Aspose.Cells-for-Java by aspose-cells.

the class Xlsx4jHeightAdjustment method main.

/**
 * @param args
 * @throws JAXBException
 * @throws Docx4JException
 */
public static void main(String[] args) throws JAXBException, Docx4JException {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(Xlsx4jHeightAdjustment.class);
    // TODO Auto-generated method stub
    SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
    WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
    CTSheetFormatPr format = Context.getsmlObjectFactory().createCTSheetFormatPr();
    format.setDefaultRowHeight(5);
    format.setCustomHeight(Boolean.TRUE);
    sheet.getJaxbElement().setSheetFormatPr(format);
    SheetData sheetData = sheet.getJaxbElement().getSheetData();
    Row row = Context.getsmlObjectFactory().createRow();
    row.setHt(66.0);
    row.setCustomHeight(Boolean.TRUE);
    row.setR(1L);
    Cell cell1 = Context.getsmlObjectFactory().createCell();
    cell1.setV("1234");
    row.getC().add(cell1);
    Cell cell2 = Context.getsmlObjectFactory().createCell();
    cell2.setV("56");
    row.getC().add(cell2);
    sheetData.getRow().add(row);
    SaveToZipFile saver = new SaveToZipFile(pkg);
    saver.save(dataDir + "RowHeight-Xlsx4j.xlsx");
}
Also used : SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) SheetData(org.xlsx4j.sml.SheetData) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) Row(org.xlsx4j.sml.Row) CTSheetFormatPr(org.xlsx4j.sml.CTSheetFormatPr) Cell(org.xlsx4j.sml.Cell)

Aggregations

SpreadsheetMLPackage (org.docx4j.openpackaging.packages.SpreadsheetMLPackage)5 WorksheetPart (org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart)5 PartName (org.docx4j.openpackaging.parts.PartName)4 SaveToZipFile (org.docx4j.openpackaging.io.SaveToZipFile)3 File (java.io.File)2 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)2 SheetData (org.xlsx4j.sml.SheetData)2 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 NoSuchElementException (java.util.NoSuchElementException)1 JAXBException (javax.xml.bind.JAXBException)1 CTDrawing (org.docx4j.dml.spreadsheetdrawing.CTDrawing)1 DocPropsCustomPart (org.docx4j.openpackaging.parts.DocPropsCustomPart)1 Drawing (org.docx4j.openpackaging.parts.DrawingML.Drawing)1 Part (org.docx4j.openpackaging.parts.Part)1 SharedStrings (org.docx4j.openpackaging.parts.SpreadsheetML.SharedStrings)1 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)1 Relationship (org.docx4j.relationships.Relationship)1 CTSheetFormatPr (org.xlsx4j.sml.CTSheetFormatPr)1