1 pom.xml(Maven依赖文件)
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.17</version></dependency>
2 调试代码
/*** 删除行*/@GetMapping("/removeRow")public void removeRow(HttpServletResponse response) {String templateFileName = "D:/test/模板.docx";try {InputStream inputStream = new FileInputStream(templateFileName);XWPFDocument document = new XWPFDocument(inputStream);XWPFTable table = document.getTableArray(0);//删除第二行table.removeRow(1);//保存文件String fileName = "模板.docx";response.setHeader("content-type", "application/octet-stream");response.setContentType("application/octet-stream;charset=UTF-8");response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));OutputStream outputStream = response.getOutputStream();document.write(outputStream);outputStream.flush();outputStream.close();} catch (Exception e) {e.printStackTrace();}}
3 调试结果
原始模板:
最终效果图: