Android development uses POI to generate and export report files according to the word report template (hands-on tutorial)

There is a function that needs to be used to export reports in the project. I searched for a long time on the Internet, and the few blogs I found were not specific enough. Finally, after continuous attempts, I finally succeeded. Here is a record of how to create a new report based on the word template. The word document is filled with content, and the word document is exported to the specified path. (docx format)

1. First, we need to import the following jar package:
insert image description here
Download link: https://pan.baidu.com/s/1S88PX1NxXtSsafbm2tW1fw
Extraction code: l4z0

2. We add these contents in build.gradle:
(1) Add in android{}:

	packagingOptions{
    
    
        exclude 'META-INF/INDEX.LIST'
    }
    defaultConfig{
    
    
        multiDexEnabled true
    }

(2) Add in dependencies{}

	implementation files('libs\\dom4j-1.6.1.jar')
    implementation files('libs\\poi-3.9-20121203.jar')
    implementation files('libs\\poi-ooxml-3.9-20121203.jar')
    implementation files('libs\\poi-ooxml-schemas-3.9-20121203.jar')
    implementation files('libs\\stax-api-1.0.1.jar')
    implementation files('libs\\xmlbeans-2.3.0.jar')

(3) Click Sync Project with Gradle Files in File in the menu bar.

3. We need to prepare a docx file as a template (it can contain tables), enter a string in the form of $name$ in the position you need to replace, and then we will replace it with the content you want to fill. When ready, create a folder under src/main/ called assets, and put the docx template file in it.
insert image description here

4. After that, we can start to create N groups of HashMap key-value pairs, and write in the content you want to fill in, for example:

	Map<String, Object> map = new HashMap<String, Object>();
	map.put("$name$", "张三");
	map.put("$sex$", "男");

5. Then we get the template file, traverse the map to fill the corresponding content into the document, and select the path to export the new file:

	InputStream is = context.getAssets().open("模板文件.docx");
	XWPFDocument document = new XWPFDocument(is);
	//读取段落(一般段落,页眉页脚没办法读取)
	List<XWPFParagraph> listParagraphs = document.getParagraphs();
	processParagraphs(listParagraphs, map);

	//读取页脚
	List<XWPFFooter> footerList = document.getFooterList();
	processParagraph(footerList, map);

	//处理表格
	Iterator<XWPFTable> it = document.getTablesIterator();
	while (it.hasNext()) {
    
    //循环操作表格
		XWPFTable table = it.next();
        List<XWPFTableRow> rows = table.getRows();
        for (XWPFTableRow row : rows) {
    
    //取得表格的行
        	List<XWPFTableCell> cells = row.getTableCells();
            for (XWPFTableCell cell : cells) {
    
    //取得单元格
				List<XWPFParagraph> paragraphListTable = cell.getParagraphs();
                processParagraphs(paragraphListTable, map);
			}
		}
	}
	FileOutputStream fopts = new FileOutputStream("/storage/emulated/0/poiTest/Report/测试.docx");
	document.write(fopts);
	if (fopts != null) {
    
    
		fopts.close();
	}

6. In this way, the docs file can be exported to the specified path.

Guess you like

Origin blog.csdn.net/qq_35761934/article/details/119781249