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();
}
}
}
});
错误
线程“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) 在org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTFontsImpl.setFontArray(未知来源) 在org.apache.poi.xssf.model.StylesTable.writeTo(StylesTable.java:319) 在org.apache.poi.xssf.model.StylesTable.commit(StylesTable.java:377) 在org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:177) 在org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:181) 在org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:214) 在SR $ 4.actionPerformed(SR.java:298) 在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) 在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2346) 在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6527) 在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) 在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) 在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:0)
我知道这是一个老问题,但是它可以帮助某人。
当您尝试两次写入同一文件时,会发生XmlValueDisconnectedException。当我尝试将Word转换为Pdf时,这发生在我身上。 这对我有帮助:
首先,我必须更新所有Opensagres和Apache Poi依赖项的版本。
然后,我从文档中重新加载了工作簿。这是我的代码:
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();