Exception when writing to the xlsx document several times using apache poi 3.7

后端 未结 7 1719
夕颜
夕颜 2020-11-29 08:44

I am getting the following exception while trying to write an .xlsx file using Apache POI: org.apache.xmlbeans.impl.values.XmlValueDisconnectedException

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 08:52

    This seems to be indeed a bug in XSSFSheet.createRow(int index). As long as the bug is not fixed, using this class as a workaround should do the trick :

    import java.util.Iterator;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    
    public class PoiHacks
    {
        // Fix of XSSFSheet.createRow(int index)
        public static Row createRow(Sheet sheet, int index) {
            Row row = sheet.getRow(index);
            if(row==null) return sheet.createRow(index);
    
            Iterator it = row.iterator();
            while(it.hasNext()) {
                it.next();
                it.remove();
            }
            return row;
        }
    }
    

    Use :

    PoiHacks.createRow(sheet, 0);
    

提交回复
热议问题