Christian
Christian

Reputation: 269

getting xmlvaluedisconnectedexception error when saving xlsx file java poi

save.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String excelFilePath = "sample.xlsx";
            FileInputStream inputStream = null;
            try {
                inputStream = new FileInputStream(new File(excelFilePath));
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }

            Workbook workbook = null;
            try {
                workbook = new XSSFWorkbook(inputStream);
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            //Sheet sheet = workbook.getSheetAt(0);
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                Sheet sheet = workbook.getSheetAt(i);

                Iterator<Row> iterator = sheet.iterator();
                Row row = sheet.getRow(0);
                while (iterator.hasNext()) {
                    Row nextRow = iterator.next();

                    Iterator<Cell> cellIterator = nextRow.cellIterator();
                    Iterator<Cell> scellIterator = nextRow.cellIterator();

                    cellIterator.next();
                    scellIterator.next();
                    scellIterator.next();
                    Cell topicsCell = cellIterator.next();
                    Cell topicSentimentCell = scellIterator.next();

                    String cellContents = topicsCell.getStringCellValue();
                    String scellContents = topicSentimentCell.getStringCellValue();

                    String[] topics = cellContents.split(";");
                    String[] topicSentiment = scellContents.split(";");


                    for (int in = 0; in < topics.length; in++) {
                        Cell cell = row.getCell(in);
                        cell.setCellValue(textArea.getText());
                    }


                }

                try {
                    workbook.write(new FileOutputStream("sample.xlsx"));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

            }
        }
    });

The Error

Exception in thread "AWT-EventQueue-0" org.apache.xmlbeans.impl.values.XmlValueDisconnectedException at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1258) at org.apache.xmlbeans.impl.values.XmlObjectBase.newCursor(XmlObjectBase.java:286) at org.apache.xmlbeans.impl.values.XmlComplexContentImpl.arraySetterHelper(XmlComplexContentImpl.java:1124) at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTFontsImpl.setFontArray(Unknown Source) at org.apache.poi.xssf.model.StylesTable.writeTo(StylesTable.java:319) at org.apache.poi.xssf.model.StylesTable.commit(StylesTable.java:377) at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:177) at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:181) at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:214) at SR$4.actionPerformed(SR.java:298) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6527) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6292) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4883) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4705) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) at java.awt.Container.dispatchEventImpl(Container.java:2278) at java.awt.Window.dispatchEventImpl(Window.java:2739) at java.awt.Component.dispatchEvent(Component.java:4705) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) at java.awt.EventQueue.access$400(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:697) at java.awt.EventQueue$3.run(EventQueue.java:691) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:719) at java.awt.EventQueue$4.run(EventQueue.java:717) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:716) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Upvotes: 0

Views: 5389

Answers (1)

IvanaM
IvanaM

Reputation: 11

I know it's an old question, but it can help someone.

The XmlValueDisconnectedException occurs when you try writing into a same file twice. This happened to me when I tried to convert Word into a Pdf. Here's what helped me:

  1. First I had to update versions of all Opensagres and Apache Poi dependencies.

  2. Then I reloaded a workbook from document. Here is my code:

        FileInputStream is = FileUtils.openInputStream( templateFiles.get( 
        ConstantManager.FILE_NAME ) );
        XWPFDocument document = new XWPFDocument( is );
        tmpDoc = File.createTempFile( "tmp", "docx" );
        //replace body elements here...
    
        //write this into docx
        tmpDocx = File.createTempFile( "tmpDocx", "docx" );
        FileOutputStream out = new FileOutputStream( tmpDocx );
        document.write( out );
        out.close();
        PdfOptions options = PdfOptions.create();
        //KEY PART!!!           
        //it is not allowed to write into a document two times - 
        //org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
        //that's why I have to reload a workbook from the file
        document = new XWPFDocument( new FileInputStream( tmpDocx ) );
    
        //and then convert to pdf
        FileOutputStream fos = new FileOutputStream( tmpDoc );
        PdfConverter.getInstance().convert( document, fos, options );
        fos.close();
        is.close();
        document.close();
    

Upvotes: 0

Related Questions