简介
POI是apache提供的可以操作word文档的第三方jar。POI能操作word是使用XWPFDocument对象
主要操作
pom 依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.15</version>
</dependency>
建立word模板
这里建立的是:d:/ttt/t.doc, 内容如下:
${content}
输出word文件
File tmpFile = new File("d:/ttt/t.doc");
if(!tmpFile.getParentFile().exists()){
tmpFile.getParentFile().mkdirs();
}
FileInputStream tempFileInputStream = new FileInputStream(tmpFile);
HWPFDocument document = new HWPFDocument(tempFileInputStream);
// 读取文本内容
Range bodyRange = document.getRange();
bodyRange.replaceText("${content}", sb.toString());
//导出到文件
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
document.write(byteArrayOutputStream);
String exportFile = "D:" + File.separator + "doc" + File.separator + "output.doc";
OutputStream outputStream = new FileOutputStream(exportFile);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.close();
参考资料
excel-java
用Java操作excel
XSSFWorkbook
能否web编辑 excel 模板,
如何在 excel 中添加程序标记
org.apache.poi.xssf
读取 excel 中的批注内容:
File file = new File("d:\\1.xlsx");
InputStream in = new FileInputStream(file);
Workbook wb = new XSSFWorkbook(in);
Assert.assertNotNull(wb);
Sheet sheet = wb.getSheetAt(0);
for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row row = sheet.getRow(rowNum);
for (int colNum = 0; colNum <= row.getLastCellNum(); colNum++) {
Cell cell = row.getCell(colNum);
if (cell != null) {
System.out.println("--------------");
System.out.println(rowNum);
System.out.println(colNum);
System.out.println(cell.getStringCellValue());
Comment comment = cell.getCellComment();
if (comment != null) {
System.out.println("comment 内容如下:");
System.out.println(comment.getString());
}
}
}
}
有了 批注 后,再解析
读写Word程序
Python
利用python批量处理Word文件——正文、标题
from docx import Document
doc=Document("./a.docx")
for p in doc.paragraphs:
print(p.text)
NodeJS
officegen 插件
https://www.npmjs.com/package/officegen
Java
poi: 需要建立模板文件,然后去替换模板上的标签来实现
http://poi.apache.org/index.html