Java Code Examples for org.apache.poi.poifs.filesystem.POIFSFileSystem#writeFilesystem()
The following examples show how to use
org.apache.poi.poifs.filesystem.POIFSFileSystem#writeFilesystem() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Poi3Test.java From easyexcel with Apache License 2.0 | 6 votes |
@Test public void Encryption() throws Exception { String file = TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx"; POIFSFileSystem fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile); Encryptor enc = info.getEncryptor(); enc.confirmPassword("foobaa"); OPCPackage opc = OPCPackage.open(new File(file), PackageAccess.READ_WRITE); OutputStream os = enc.getDataStream(fs); opc.save(os); opc.close(); // Write out the encrypted version FileOutputStream fos = new FileOutputStream("D:\\test\\99999999999.xlsx"); fs.writeFilesystem(fos); fos.close(); fs.close(); }
Example 2
Source File: HSSFWorkbook.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Adds an OLE package manager object with the given POIFS to the sheet * * @param poiData an POIFS containing the embedded document, to be added * @param label the label of the payload * @param fileName the original filename * @param command the command to open the payload * @return the index of the added ole object * @throws IOException if the object can't be embedded */ public int addOlePackage(POIFSFileSystem poiData, String label, String fileName, String command) throws IOException { DirectoryNode root = poiData.getRoot(); Map<String,ClassID> olemap = getOleMap(); for (Map.Entry<String,ClassID> entry : olemap.entrySet()) { if (root.hasEntry(entry.getKey())) { root.setStorageClsid(entry.getValue()); break; } } ByteArrayOutputStream bos = new ByteArrayOutputStream(); poiData.writeFilesystem(bos); return addOlePackage(bos.toByteArray(), label, fileName, command); }
Example 3
Source File: WriteContextImpl.java From easyexcel with Apache License 2.0 | 5 votes |
/** * To encrypt */ private boolean doOutputStreamEncrypt07() throws Exception { if (StringUtils.isEmpty(writeWorkbookHolder.getPassword()) || !ExcelTypeEnum.XLSX.equals(writeWorkbookHolder.getExcelType())) { return false; } if (writeWorkbookHolder.getFile() != null) { return false; } File tempXlsx = FileUtils.createTmpFile(UUID.randomUUID().toString() + ".xlsx"); FileOutputStream tempFileOutputStream = new FileOutputStream(tempXlsx); try { writeWorkbookHolder.getWorkbook().write(tempFileOutputStream); } finally { try { writeWorkbookHolder.getWorkbook().close(); tempFileOutputStream.close(); } catch (Exception e) { if (!tempXlsx.delete()) { throw new ExcelGenerateException("Can not delete temp File!"); } throw e; } } POIFSFileSystem fileSystem = null; try { fileSystem = openFileSystemAndEncrypt(tempXlsx); fileSystem.writeFilesystem(writeWorkbookHolder.getOutputStream()); } finally { if (fileSystem != null) { fileSystem.close(); } if (!tempXlsx.delete()) { throw new ExcelGenerateException("Can not delete temp File!"); } } return true; }
Example 4
Source File: HPSFPropertiesOnlyDocument.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Write out, with any properties changes, but nothing else */ public void write(File newFile) throws IOException { POIFSFileSystem fs = POIFSFileSystem.create(newFile); try { write(fs); fs.writeFilesystem(); } finally { fs.close(); } }
Example 5
Source File: AttachmentExportUtil.java From myexcel with Apache License 2.0 | 5 votes |
/** * 加密导出 * * @param workbook workbook * @param fileName fileName * @param response response * @param password password */ public static void encryptExport(final Workbook workbook, String fileName, HttpServletResponse response, final String password) { if (workbook instanceof HSSFWorkbook) { throw new IllegalArgumentException("Document encryption for.xls is not supported"); } Path path = null; try { String suffix = Constants.XLSX; path = TempFileOperator.createTempFile("encrypt_temp", suffix); workbook.write(Files.newOutputStream(path)); final POIFSFileSystem fs = new POIFSFileSystem(); final EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard); final Encryptor enc = info.getEncryptor(); enc.confirmPassword(password); try (OPCPackage opc = OPCPackage.open(path.toFile(), PackageAccess.READ_WRITE); OutputStream os = enc.getDataStream(fs)) { opc.save(os); } if (!fileName.endsWith(suffix)) { fileName += suffix; } response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); setAttachmentConfig(fileName, response); fs.writeFilesystem(response.getOutputStream()); } catch (IOException | InvalidFormatException | GeneralSecurityException e) { throw new RuntimeException(e); } finally { clear(workbook); TempFileOperator.deleteTempFile(path); } }
Example 6
Source File: FileExportUtil.java From myexcel with Apache License 2.0 | 5 votes |
/** * 加密导出 * * @param workbook workbook * @param file file * @param password password * @throws Exception Exception */ public static void encryptExport(final Workbook workbook, File file, final String password) throws Exception { if (workbook instanceof HSSFWorkbook) { throw new IllegalArgumentException("Document encryption for.xls is not supported"); } String suffix = Constants.XLSX; if (!file.getName().endsWith(suffix)) { file = Paths.get(file.getAbsolutePath() + suffix).toFile(); } try (FileOutputStream fos = new FileOutputStream(file)) { workbook.write(fos); if (workbook instanceof SXSSFWorkbook) { ((SXSSFWorkbook) workbook).dispose(); } final POIFSFileSystem fs = new POIFSFileSystem(); final EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard); final Encryptor enc = info.getEncryptor(); enc.confirmPassword(password); try (OPCPackage opc = OPCPackage.open(file, PackageAccess.READ_WRITE); OutputStream os = enc.getDataStream(fs)) { opc.save(os); } try (FileOutputStream fileOutputStream = new FileOutputStream(file)) { fs.writeFilesystem(fileOutputStream); } } finally { workbook.close(); } }
Example 7
Source File: Export.java From spring-boot-start-current with Apache License 2.0 | 5 votes |
public static void exportDoc ( HttpServletResponse response , String fileName , String html ) throws IOException { // 将 html 导出成 doc 文档 setResponse( response , "doc" , fileName ); POIFSFileSystem fs = new POIFSFileSystem(); fs.createDocument( new ByteArrayInputStream( html.getBytes( StandardCharsets.UTF_8 ) ) , "WordDocument" ); fs.writeFilesystem( response.getOutputStream() ); }
Example 8
Source File: Export.java From spring-boot-start-current with Apache License 2.0 | 5 votes |
public static void exportCsv ( HttpServletResponse response , String fileName , LinkedHashMap< String, String > titleMap , List< ? > dataList ) throws IOException { String content = convertCsv( titleMap , dataList ); // 导出 csv setResponse( response , "csv" , fileName ); response.getOutputStream().write( content.getBytes( StandardCharsets.UTF_8 ) ); POIFSFileSystem poifsFileSystem = new POIFSFileSystem(); poifsFileSystem.createDocument( new ByteArrayInputStream( content.getBytes( "GBK" ) ) , "WordDocument" ); poifsFileSystem.writeFilesystem( response.getOutputStream() ); }
Example 9
Source File: MSExcelOOXMLSignUtil.java From hadoopoffice with Apache License 2.0 | 5 votes |
private void signEncryptedPackage(InputStream tmpFileInputStream, SignatureConfig sc, String password) throws IOException, InvalidFormatException, FormatNotUnderstoodException, XMLSignatureException, MarshalException { POIFSFileSystem poifsTemp = new POIFSFileSystem(tmpFileInputStream); EncryptionInfo info = new EncryptionInfo(poifsTemp); Decryptor d = Decryptor.getInstance(info); try { if (!d.verifyPassword(password)) { throw new FormatNotUnderstoodException("Error: Cannot decrypt new Excel file (.xlsx) for signing. Invalid password"); } // signing OPCPackage pkg = OPCPackage.open(d.getDataStream(poifsTemp)); sc.setOpcPackage(pkg); SignatureInfo si = new SignatureInfo(); si.setSignatureConfig(sc); si.confirmSignature(); // encrypt again Encryptor enc = info.getEncryptor(); enc.confirmPassword(password); POIFSFileSystem poifs = new POIFSFileSystem(); OutputStream os = enc.getDataStream(poifs); pkg.save(os); pkg.close(); if (os!=null) { os.close(); } poifs.writeFilesystem(this.finalOutputStream); if (poifs!=null) { poifs.close(); } if (poifsTemp!=null) { poifsTemp.close(); } } catch (GeneralSecurityException e) { LOG.error(e); throw new FormatNotUnderstoodException("Error: Cannot decrypt new Excel file (.xlsx) for signing."); } }
Example 10
Source File: ProjectCleanUtility.java From mpxj with GNU Lesser General Public License v2.1 | 4 votes |
/** * Process a project file to make it anonymous. * * @param input input file name * @param output output file name */ private void processMPP(String input, String output) throws IOException { String varDataFileName; String projectDirName; int mppFileType = NumberHelper.getInt(m_project.getProjectProperties().getMppFileType()); switch (mppFileType) { case 8: { projectDirName = " 1"; varDataFileName = "FixDeferFix 0"; break; } case 9: { projectDirName = " 19"; varDataFileName = "Var2Data"; break; } case 12: { projectDirName = " 112"; varDataFileName = "Var2Data"; break; } case 14: { projectDirName = " 114"; varDataFileName = "Var2Data"; break; } default: { throw new IllegalArgumentException("Unsupported file type " + mppFileType); } } // // Load the raw file // FileInputStream is = new FileInputStream(input); POIFSFileSystem fs = new POIFSFileSystem(is); is.close(); // // Locate the root of the project file system // DirectoryEntry root = fs.getRoot(); m_projectDir = (DirectoryEntry) root.getEntry(projectDirName); // // Process Tasks // processFile((DirectoryEntry) m_projectDir.getEntry("TBkndTask"), varDataFileName, m_project.getTasks(), true, TaskField.NAME); // // Process Resources // processFile((DirectoryEntry) m_projectDir.getEntry("TBkndRsc"), varDataFileName, m_project.getResources(), true, ResourceField.NAME, ResourceField.INITIALS); // // Process project properties // List<ProjectProperties> projectProperties = Arrays.asList(m_project.getProjectProperties()); processFile(m_projectDir, "Props", projectProperties, true, PROJECT_FIELDS); processFile(root, "\005SummaryInformation", projectProperties, false, PROJECT_FIELDS); processFile(root, "\005DocumentSummaryInformation", projectProperties, false, PROJECT_FIELDS); // // Write the replacement raw file // FileOutputStream os = new FileOutputStream(output); fs.writeFilesystem(os); os.flush(); os.close(); fs.close(); }
Example 11
Source File: HSSFWorkbook.java From lams with GNU General Public License v2.0 | 3 votes |
/** * Method write - write out this workbook to a new {@link File}. Constructs * a new POI POIFSFileSystem, passes in the workbook binary representation and * writes it out. If the file exists, it will be replaced, otherwise a new one * will be created. * * Note that you cannot write to the currently open File using this method. * If you opened your Workbook from a File, you <i>must</i> use the {@link #write()} * method instead! * * @param newFile The new File you wish to write the XLS to * * @exception IOException if anything can't be written. * @see org.apache.poi.poifs.filesystem.POIFSFileSystem */ @Override public void write(File newFile) throws IOException { POIFSFileSystem fs = POIFSFileSystem.create(newFile); try { write(fs); fs.writeFilesystem(); } finally { fs.close(); } }