Java利用POI生成Word文件

简介

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();

参考资料

POI操作word模板并生成新的word.docx
https://www.jianshu.com/p/6603b1ea3ad1

POI生成WORD文档
https://www.cnblogs.com/yfrs/p/wordpoi.html

 

 

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文件——正文、标题

https://blog.csdn.net/xtfge0915/article/details/83479922

from docx import Document
doc=Document("./a.docx")
for p in doc.paragraphs:
&nbsp; &nbsp; print(p.text)

 

NodeJS 

officegen 插件

https://www.npmjs.com/package/officegen

 

Java 

poi: 需要建立模板文件,然后去替换模板上的标签来实现

http://poi.apache.org/index.html

 

 

 

 

参考资料

POI操作word模板并生成新的word.docx

https://www.jianshu.com/p/6603b1ea3ad1

 

POI生成WORD文档

https://www.cnblogs.com/yfrs/p/wordpoi.html

 

遇到的问题及解决

如何消除word标题前面的竖线 ||Word中标题编号变为竖线处理

https://blog.csdn.net/yangnianjinxin/article/details/78688594?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

 

word标题编号消失解决

https://blog.csdn.net/gzn00417/article/details/104570913/

 

posted @ 2019-05-19 10:38  lvye1221  阅读(177)  评论(0编辑  收藏  举报