poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdhanara <rdhan...@ford.com>
Subject Re: Loading Large XLSM file using POI without Memory issue .
Date Mon, 15 Apr 2019 15:33:00 GMT
Hi,

As suggested trying to do below steps,

1. Unzip the large xlsm file 
2.Delete the Sheet as per requirement
3.repack the source once again to xlsm file

I have done step 1 & 3 as follow. Please have a look and correct me if i am
going in wrong direction ..
***************
*1.Tried unzipping the large xlsm file as follows*

String zipFilePath = "C:\\PROJECTS\\Source.xlsm";
	        
String destDir = "C:\\PROJECTS\\unzip";


 private static void unzip(String zipFilePath, String destDir) {
	        File dir = new File(destDir);
	        // create output directory if it doesn't exist
	        if(!dir.exists()) 
	        	dir.mkdirs();
	        FileInputStream fis;
	        //buffer for read and write data to file
	        byte[] buffer = new byte[1024];
	        try {
	            fis = new FileInputStream(zipFilePath);
	            ZipInputStream zis = new ZipInputStream(fis);
	            ZipEntry ze = zis.getNextEntry();
	            while(ze != null){
	                String fileName = ze.getName();
	                File newFile = new File(destDir + File.separator +
fileName);
	                System.out.println("Unzipping to
"+newFile.getAbsolutePath());
	                //create directories for sub directories in zip
	                new File(newFile.getParent()).mkdirs();
	                FileOutputStream fos = new FileOutputStream(newFile);
	                int len;
	                while ((len = zis.read(buffer)) > 0) {
	                fos.write(buffer, 0, len);
	                }
	                fos.close();
	                //close this ZipEntry
	                zis.closeEntry();
	                ze = zis.getNextEntry();
	            }
	            //close last ZipEntry
	            zis.closeEntry();
	            zis.close();
	            fis.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        
	    }

Once after executing above program it extract xlsm content as follows
<http://apache-poi.1045710.n5.nabble.com/file/t340680/unzip.png> 

*********************

*2. Repacking *

ZipOutputStream out = new ZipOutputStream(new
FileOutputStream("C:\\PROJECTS\\TEST.xlsm"));

              Files.walkFileTree(Paths.get("C:\\PROJECTS\\unzip"), new
SimpleFileVisitor<Path>() {
                  public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
                	  out.putNextEntry(new
ZipEntry(Paths.get("C:\\PROJECTS\\unzip").relativize(file).toString()));
                      Files.copy(file, out);
                      out.closeEntry();
                      return FileVisitResult.CONTINUE;
                  }
              });

If I open TEST.xlsm  I am able to see the original xlsm file content here..

But as per the suggestion before repacking i need to delete the expected
sheet and then repack it.
 I am trying to parse this file and delete the sheet tag..

*\xl\workbook.xml*

Will update you shortly.


*Note: If you have any better approach than the one i mentioned here do let
me know ..*




--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message