Home Web Front-end Front-end Q&A poi html to word

poi html to word

May 15, 2023 pm 09:14 PM

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:

  1. Create Word document object

In this example, we Use XSSFWorkbook to create a Word document object. The sample code is as follows:

XWPFDocument document = new XWPFDocument();
Copy after login
  1. Create a paragraph object

Create a paragraph object through XWPFDocument. The sample code is as follows:

XWPFParagraph paragraph = document.createParagraph();
Copy after login
  1. Add text and pictures

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);
    }
}
Copy after login

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 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.

  1. Save 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();
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you connect React components to the Redux store using connect()? How do you connect React components to the Redux store using connect()? Mar 21, 2025 pm 06:23 PM

Article discusses connecting React components to Redux store using connect(), explaining mapStateToProps, mapDispatchToProps, and performance impacts.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

What are the limitations of Vue 2's reactivity system with regard to array and object changes? What are the limitations of Vue 2's reactivity system with regard to array and object changes? Mar 25, 2025 pm 02:07 PM

Vue 2's reactivity system struggles with direct array index setting, length modification, and object property addition/deletion. Developers can use Vue's mutation methods and Vue.set() to ensure reactivity.

How do you define routes using the <Route> component? How do you define routes using the <Route> component? Mar 21, 2025 am 11:47 AM

The article discusses defining routes in React Router using the &lt;Route&gt; component, covering props like path, component, render, children, exact, and nested routing.

What are Redux reducers? How do they update the state? What are Redux reducers? How do they update the state? Mar 21, 2025 pm 06:21 PM

Redux reducers are pure functions that update the application's state based on actions, ensuring predictability and immutability.

What are the benefits of using TypeScript with React? What are the benefits of using TypeScript with React? Mar 27, 2025 pm 05:43 PM

TypeScript enhances React development by providing type safety, improving code quality, and offering better IDE support, thus reducing errors and improving maintainability.

What are Redux actions? How do you dispatch them? What are Redux actions? How do you dispatch them? Mar 21, 2025 pm 06:21 PM

The article discusses Redux actions, their structure, and dispatching methods, including asynchronous actions using Redux Thunk. It emphasizes best practices for managing action types to maintain scalable and maintainable applications.

How can you use useReducer for complex state management? How can you use useReducer for complex state management? Mar 26, 2025 pm 06:29 PM

The article explains using useReducer for complex state management in React, detailing its benefits over useState and how to integrate it with useEffect for side effects.

See all articles