private static ContentType newCorePropertiesPart() { try { return new ContentType(ContentTypes.CORE_PROPERTIES_PART); } catch (InvalidFormatException e) { // Should never happen throw new OpenXML4JRuntimeException( "Package.init() : this exception should never happen, " + "if you read this message please send a mail to the developers team. : " + e.getMessage(), e ); } }
/** * Throws an exception if the specified URI is absolute. * * @param partUri * The URI to check. * @throws InvalidFormatException * Throws if the specified URI is absolute. */ private static void throwExceptionIfAbsoluteUri(URI partUri) throws InvalidFormatException { if (partUri.isAbsolute()) { throw new InvalidFormatException("Absolute URI forbidden: " + partUri); } }
/** * Set last modification date. * * @see org.apache.poi.openxml4j.opc.PackageProperties#setModifiedProperty(java.util.Optional) */ public void setModifiedProperty(String modified) { try { this.modified = setDateValue(modified); } catch (InvalidFormatException e) { throw new IllegalArgumentException("modified : " + e.getLocalizedMessage(), e); } }
/** * Constructor. Parses the content of the specified input stream. * * @param in * If different of <i>null</i> then the content types part is * retrieve and parse. * @throws InvalidFormatException * If the content types part content is not valid. */ public ContentTypeManager(InputStream in, OPCPackage pkg) throws InvalidFormatException { this.container = pkg; this.defaultContentType = new TreeMap<>(); if (in != null) { try { parseContentTypesFile(in); } catch (InvalidFormatException e) { InvalidFormatException ex = new InvalidFormatException("Can't read content types part !"); // here it is useful to add the cause to not loose the original stack-trace ex.initCause(e); throw ex; } } }
public static Workbook getWorkbook(String filePath){ try { return WorkbookFactory.create(new FileInputStream(filePath)); } catch (IOException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } return null; }
/** * Wrapper to open a package, which works around shortcomings in java's this() constructor calls * * @param path the path to the document * @return the new OPCPackage * * @exception IOException if there was a problem opening the document */ public static OPCPackage openPackage(String path) throws IOException { try { return OPCPackage.open(path); } catch (InvalidFormatException e) { throw new IOException(e.toString(), e); } }
/** * 读取excel数据 * @param path */ public static ArrayList<Map<String,String>> readExcelToObj(String path) { Workbook wb = null; ArrayList<Map<String,String>> result = null; try { wb = WorkbookFactory.create(new File(path)); result = readExcel(wb, 0, 2, 0); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }
/** * Constructor. Parses the content of the specified input stream. * * @param in * If different of <i>null</i> then the content types part is * retrieve and parse. * @throws InvalidFormatException * If the content types part content is not valid. */ public ContentTypeManager(InputStream in, OPCPackage pkg) throws InvalidFormatException { this.container = pkg; this.defaultContentType = new TreeMap<>(); if (in != null) { try { parseContentTypesFile(in); } catch (InvalidFormatException e) { InvalidFormatException ex = new InvalidFormatException("Can't read content types part !"); // here it is useful to add the cause to not loose the original stack-trace ex.initCause(e); throw ex; } } }
/** * Wrapper to open a package, returning an IOException * in the event of a problem. * Works around shortcomings in java's this() constructor calls */ public static OPCPackage openPackage(String path) throws IOException { try { return OPCPackage.open(path); } catch (InvalidFormatException e) { throw new IOException(e.toString()); } }
/** * Add a marshaller. * * @param contentType * The content type to bind to the specified marshaller. * @param marshaller * The marshaller to register with the specified content type. */ public void addMarshaller(String contentType, PartMarshaller marshaller) { try { partMarshallers.put(new ContentType(contentType), marshaller); } catch (InvalidFormatException e) { logger.log(POILogger.WARN, "The specified content type is not valid: '" + e.getMessage() + "'. The marshaller will not be added !"); } }
/** * Throws an exception if the specified part name doesn't start with a * forward slash character '/'. [M1.4] * * @param partUri * The part name to check. * @throws InvalidFormatException * If the specified part name doesn't start with a forward slash * character '/'. */ private static void throwExceptionIfPartNameNotStartsWithForwardSlashChar( URI partUri) throws InvalidFormatException { String uriPath = partUri.getPath(); if (uriPath.length() > 0 && uriPath.charAt(0) != PackagingURIHelper.FORWARD_SLASH_CHAR) { throw new InvalidFormatException( "A part name shall start with a forward slash ('/') character [M1.4]: " + partUri.getPath()); } }
public Excel(InputStream is) { try { this.wb = WorkbookFactory.create(is); this.sheet = wb.getSheetAt(wb.getActiveSheetIndex()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } }
/** * Set last printed date. * * @see org.apache.poi.openxml4j.opc.PackageProperties#setLastPrintedProperty(java.util.Optional) */ public void setLastPrintedProperty(String lastPrinted) { try { this.lastPrinted = setDateValue(lastPrinted); } catch (InvalidFormatException e) { throw new IllegalArgumentException("lastPrinted : " + e.getLocalizedMessage(), e); } }
/** * Wrapper to open a package, which works around shortcomings in java's this() constructor calls * * @param path the path to the document * @return the new OPCPackage * * @exception IOException if there was a problem opening the document */ public static OPCPackage openPackage(String path) throws IOException { try { return OPCPackage.open(path); } catch (InvalidFormatException e) { throw new IOException(e.toString(), e); } }
/** * Add an unmarshaller. * * @param contentType * The content type to bind to the specified unmarshaller. * @param unmarshaller * The unmarshaller to register with the specified content type. */ public void addUnmarshaller(String contentType, PartUnmarshaller unmarshaller) { try { partUnmarshallers.put(new ContentType(contentType), unmarshaller); } catch (InvalidFormatException e) { logger.log(POILogger.WARN, "The specified content type is not valid: '" + e.getMessage() + "'. The unmarshaller will not be added !"); } }
/** * Throws an exception if the specified part name ends with a forwar slash * character '/'. [M1.5] * * @param partUri * The part name to check. * @throws InvalidFormatException * If the specified part name ends with a forwar slash character * '/'. */ private static void throwExceptionIfPartNameEndsWithForwardSlashChar( URI partUri) throws InvalidFormatException { String uriPath = partUri.getPath(); if (uriPath.length() > 0 && uriPath.charAt(uriPath.length() - 1) == PackagingURIHelper.FORWARD_SLASH_CHAR) { throw new InvalidFormatException( "A part name shall not have a forward slash as the last character [M1.5]: " + partUri.getPath()); } }
wb = WorkbookFactory.create(new File("workbook.xlsx")); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace();
/** * Set last modification date. * * @see org.apache.poi.openxml4j.opc.PackageProperties#setModifiedProperty(java.util.Optional) */ public void setModifiedProperty(String modified) { try { this.modified = setDateValue(modified); } catch (InvalidFormatException e) { throw new IllegalArgumentException("modified : " + e.getLocalizedMessage(), e); } }
throw new InvalidOperationException(e.getMessage()); throw new InvalidOperationException(e.getMessage());
@Override public boolean load(InputStream ios) throws InvalidFormatException { // Grab the data ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { IOUtils.copy(ios, baos); } catch(IOException e) { throw new InvalidFormatException(e.getMessage()); } // Save it data = baos.toByteArray(); // All done return true; }