/** * Get the WorkbookPart. * * @return * @since 3.3.3 */ public WorkbookPart getWorkbookPart() { return ((SpreadsheetMLPackage)this.getPackage()).getWorkbookPart(); } }
public static void main(String[] args) throws Exception { String outputfilepath = System.getProperty("user.dir") + "/sample-docs/xlsx/test-out.xlsx"; SpreadsheetMLPackage pkg = createPackage(); pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1); SaveToZipFile saver = new SaveToZipFile(pkg); saver.save(outputfilepath); System.out.println("\n\n done .. " + outputfilepath); } }
SpreadsheetMLPackage xlsPack = new SpreadsheetMLPackage(); Context.getsmlObjectFactory().createWorkbook() ); xlsPack.addTargetPart(xlsPack.wb);
/** * 传入一个 xlsx 文件,和一个水印文件,给这个 xlsx 文件加水印 * @author eko.zhan at 2018年9月17日 下午2:10:50 * @throws Docx4JException * @throws Xlsx4jException */ @Override public void process() throws WatermarkException { try { SpreadsheetMLPackage excelMLPackage = SpreadsheetMLPackage.load(file); this.excelMLPackage = excelMLPackage; int size = excelMLPackage.getWorkbookPart().getContents().getSheets().getSheet().size(); for (int i=0;i<size;i++) { WorksheetPart worksheet = excelMLPackage.getWorkbookPart().getWorksheet(i); createBgPic(worksheet); } excelMLPackage.save(file); } catch (Docx4JException e) { throw new WatermarkException("Docx4JException", e); } catch (Xlsx4jException e) { throw new WatermarkException("Xlsx4jException", e); } }
@Override protected OpcPackage generatePackage() throws Docx4JException, JAXBException { SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage(); WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/" + SHEET + ".xml"), SHEET, 1); SheetData sheetData = sheet.getJaxbElement().getSheetData(); org.xlsx4j.sml.ObjectFactory factory = org.xlsx4j.jaxb.Context.getsmlObjectFactory(); CTCalcPr ctCalcPr = factory.createCTCalcPr(); ctCalcPr.setCalcMode(STCalcMode.AUTO); pkg.getWorkbookPart().getJaxbElement().setCalcPr(ctCalcPr); pkg.getWorkbookPart().getJaxbElement().setDefinedNames(definedNames); sheet.addTargetPart(styles); Parts parts = pkg.getParts(); Part workBook = parts.get(new PartName("/xl/workbook.xml")); workBook.addTargetPart(styles);
protected void init() { try { template = Document.create(SpreadsheetMLPackage.load(reportTemplate.getDocumentContent())); result = Document.create(SpreadsheetMLPackage.load(reportTemplate.getDocumentContent())); result.getWorkbook().getCalcPr().setCalcMode(STCalcMode.AUTO); result.getWorkbook().getCalcPr().setFullCalcOnLoad(true); marshaller = XmlCopyUtils.createMarshaller(Context.jcSML); unmarshaller = XmlCopyUtils.createUnmarshaller(Context.jcSML); } catch (Exception e) { throw wrapWithReportingException(String.format("An error occurred while loading template [%s]", reportTemplate.getDocumentName()), e); } }
protected CTCalcChain getCalculationChain() { CTCalcChain calculationChain = null; try { CalcChain part = (CalcChain) result.getPackage().getParts().get(new PartName("/xl/calcChain.xml")); if (part != null) { try { calculationChain = part.getContents(); } catch (Docx4JException e) { throw new RuntimeException("Unable to get contents of part", e); } calculationChain.getC().clear(); } } catch (InvalidFormatException e) { //do nothing } return calculationChain; }
public static Document create(SpreadsheetMLPackage thePackage) { Document document = new Document(); document.thePackage = thePackage; RelationshipsPart rp = thePackage.getRelationshipsPart(); document.traverse(null, rp); return document; }
p = new SpreadsheetMLPackage(this); p.setContentType(new ContentType(pkgContentType)); return p;
protected void init() { try { template = Document.create(SpreadsheetMLPackage.load(reportTemplate.getDocumentContent())); result = Document.create(SpreadsheetMLPackage.load(reportTemplate.getDocumentContent())); result.getWorkbook().getCalcPr().setCalcMode(STCalcMode.AUTO); result.getWorkbook().getCalcPr().setFullCalcOnLoad(true); marshaller = XmlCopyUtils.createMarshaller(Context.jcSML); unmarshaller = XmlCopyUtils.createUnmarshaller(Context.jcSML); } catch (Exception e) { throw wrapWithReportingException(String.format("An error occurred while loading template [%s]", reportTemplate.getDocumentName()), e); } }
protected CTCalcChain getCalculationChain() { CTCalcChain calculationChain = null; try { CalcChain part = (CalcChain) result.getPackage().getParts().get(new PartName("/xl/calcChain.xml")); if (part != null) { calculationChain = part.getJaxbElement(); calculationChain.getC().clear(); } } catch (InvalidFormatException e) { //do nothing } return calculationChain; }
public static Document create(SpreadsheetMLPackage thePackage) { Document document = new Document(); document.thePackage = thePackage; RelationshipsPart rp = thePackage.getRelationshipsPart(); document.traverse(null, rp); return document; }
p = new SpreadsheetMLPackage(this); p.setContentType(new ContentType(pkgContentType)); return p;
/** * Remove protection from this package. * * @throws Xlsx4jException */ public void removeWorkbookProtection() throws Xlsx4jException { WorkbookPart workbookPart = ((SpreadsheetMLPackage)this.pkg).getWorkbookPart(); if (workbookPart==null) throw new Xlsx4jException("No WorkbookPart in this pkg!"); Workbook workbook = workbookPart.getJaxbElement(); if (workbook==null) throw new Xlsx4jException("WorkbookPart not initialised with Workbook content"); workbook.setWorkbookProtection(null); } /**
private void putImage(WorksheetPart worksheetPart, SpreadsheetMLPackage pkg, BinaryPartAbstractImage imagePart, CTOneCellAnchor anchor) throws Docx4JException { PartName drawingPart = new PartName(worksheetPart.getPartName().getName().replace("worksheets/sheet", "drawings/drawing")); String imagePartName = imagePart.getPartName().getName(); Drawing drawing = (Drawing) pkg.getParts().get(drawingPart); int currentId = 0; if (drawing == null) { pkg.getParts().remove(relPart.getPartName()); pkg.getParts().put(relPart); pkg.getParts().remove(drawing.getPartName()); pkg.getParts().put(drawing);
public static void main(String[] args) throws Exception { String outputfilepath = System.getProperty("user.dir") + "/sample-docs/xlsx/test-out.xlsx"; SpreadsheetMLPackage pkg = createPackage(); pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1); SaveToZipFile saver = new SaveToZipFile(pkg); saver.save(outputfilepath); System.out.println("\n\n done .. " + outputfilepath); } }
SpreadsheetMLPackage xlsPack = new SpreadsheetMLPackage(); Context.getsmlObjectFactory().createWorkbook() ); xlsPack.addTargetPart(xlsPack.wb);
WorkbookPart workbookPart = ((SpreadsheetMLPackage)this.pkg).getWorkbookPart(); if (workbookPart==null) throw new Xlsx4jException("No WorkbookPart in this pkg!");
"worksheets/sheet", "drawings/drawing")); String imagePartName = imagePart.getPartName().getName(); Part part = pkg.getParts().get(drawingPart); if (part != null && !(part instanceof Drawing)) throw new ReportFormattingException("Wrong Class: not a Drawing"); pkg.getParts().remove(relPart.getPartName()); pkg.getParts().put(relPart); pkg.getParts().remove(drawing.getPartName()); pkg.getParts().put(drawing);