Finally found a way to parse xml and delete the reference of the sheet which
i planned to delete from my excel workbook

*To delete the unwanted sheet data from workbook :*

          public static void delete_Merged_report_sheet(String destDir) {
                  String merged_rprt_sheet_Path = 
destDir+"\\xl\\worksheets\\sheet3.xml";
                  File merged_sheet = new File(merged_rprt_sheet_Path);
                  merged_sheet.delete();
          }
*******************************
*To remove the deleted sheet references from the worksheet :*
          public static void modifyXml(String destDir)  {
                  try {
                  String filepath = destDir+"\\xl\\workbook.XML";
                        DocumentBuilderFactory docFactory = 
DocumentBuilderFactory.newInstance();
                        DocumentBuilder docBuilder = 
docFactory.newDocumentBuilder();
                        Document doc = docBuilder.parse(filepath);
                        
                        Node sheets = 
doc.getElementsByTagName("sheets").item(0);
                        NodeList list = sheets.getChildNodes();
                        for (int i = 0; i < list.getLength(); i++) {
                            Node node = list.item(i);
                            NamedNodeMap attributes =   node.getAttributes();
                            attributes.getNamedItem("r:id").getTextContent();
                            if 
("rId3".equals(attributes.getNamedItem("r:id").getTextContent()))
{
                                sheets.removeChild(node);
                                   }
                        }
                        // write the content into xml file
                        TransformerFactory transformerFactory = 
TransformerFactory.newInstance();
                        Transformer transformer = 
transformerFactory.newTransformer();
                        DOMSource source = new DOMSource(doc);
                        StreamResult result = new StreamResult(new 
File(filepath));
                        transformer.transform(source, result);
                  } catch (ParserConfigurationException pce) {
                                pce.printStackTrace();
                           } catch (TransformerException tfe) {
                                tfe.printStackTrace();
                           } catch (IOException ioe) {
                                ioe.printStackTrace();
                           } catch (SAXException sae) {
                                sae.printStackTrace();
                           }
                }
          

*Thanks everyone for helping me in identifying the solution without memory
issue .. *




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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to