Home >Web Front-end >Front-end Q&A >poi html to word
With the popularity and use of electronic documents in work, different document formats have begun to appear. At work, we may encounter situations where we need to convert HTML to Word format. So, in this article, we will explore how to convert HTML to Word document via POI.
POI is an excellent Java API that provides a library that can read and write documents in Microsoft Office format (Word, Excel, PowerPoint, etc.). Through the API provided by POI, we can easily operate various types of Office documents. In this article, we will mainly use POI's XWPF module to read and write Word documents.
First, we need to prepare an HTML document. You can use any editor to edit the HTML document. In addition, we need to add POI-related dependency packages to the project. For specific dependency packages, please refer to the official documentation of POI.
Before converting HTML to Word document, we need to complete the following steps:
In this example, we Use XSSFWorkbook to create a Word document object. The sample code is as follows:
XWPFDocument document = new XWPFDocument();
Create a paragraph object through XWPFDocument. The sample code is as follows:
XWPFParagraph paragraph = document.createParagraph();
Next, we need to add the text and pictures from the HTML document to the Word document. Here we need to traverse the HTML document, read the HTML text line by line, and add it to the Word document. When we encounter a picture, we need to read the picture into memory and create an XWPFRun object to add the picture to the Word document.
The sample code is as follows:
File file = new File("test.html"); BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null; while ((line = reader.readLine()) != null) { if (line.contains("<img")) { Pattern p = Pattern.compile("<img.*?src=\"(.*?)\""); Matcher m = p.matcher(line); String imgPath = null; while (m.find()) { imgPath = m.group(1); } if (imgPath != null) { InputStream is = new FileInputStream(new File(imgPath)); paragraph.createRun().addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, "image.jpeg", Units.toEMU(200), Units.toEMU(200)); } } else { paragraph.createRun().setText(line); } }
In the process of reading the HTML text content, we use regular expressions to match the path of the image. If the HTML text contains the a1f02c36ba31691bcfe87b2722de723b tag, Then we use regular expressions to match the image path and read it into memory. Then, we use the XWPFRun object to add pictures to the Word document.
Finally, we need to save the Word document to the local disk. We can use Java's FileOutputStream class to output the Word document to the specified file path. The sample code is as follows:
FileOutputStream out = new FileOutputStream(new File("test.docx")); document.write(out); out.close(); document.close();
With the sample code in this article, we can convert an HTML document into a Word document and save it to the local disk. In addition to using POI to achieve conversion, we can also use third-party tools to implement the HTML to Word function, such as Docx4j, etc. However, the advantage of using POI to implement conversion is that it is an open source tool that can be easily integrated into our Java applications, and using POI can better control the conversion process.
To sum up, this article introduces how to use POI to convert HTML to Word documents. At the same time, we also explore how to add text and pictures to Word documents and save the documents to the local disk. This function is very common in actual work. I hope the content of this article can help you.
The above is the detailed content of poi html to word. For more information, please follow other related articles on the PHP Chinese website!