文章目录
HWPF
HWPF的基本概念
HWPF是Apache POI项目中的一个组件,它提供了一套API,用于操作Microsoft Word文档(.doc格式)。HWPF代表“Horrible Word Processor Format”,这个名字反映了Microsoft Word文档格式的复杂性和难度。HWPF允许用户在Java中读取、创建、修改Word文档。下面是基础的内容:
- Range:Range是HWPF中的核心概念,它代表文档中的一个连续区域。你可以通过Range来访问和修改文本内容。
- Paragraph:Paragraph代表文档中的一个段落。每个Range都由多个Paragraph组成。
- CharacterRun:CharacterRun代表文档中的文本字符序列,它包含文本的格式信息,如字体、大小、颜色等。
- Table:Table代表文档中的一个表格。每个Table由多个TableRow组成。
- TableRow:TableRow代表表格中的一行,它由多个TableCell组成。
- TableCell:TableCell代表表格中的一个单元格。
基本操作
引入的依赖
<dependencies>
<!-- Apache POI HWPF for handling .doc files -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>5.2.3</version>
</dependency>
<!-- Apache POI XWPF for handling .docx files -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<!-- Other dependencies if needed for your project -->
</dependencies>
如果你需要处理.docx文件,那么poi-ooxml依赖是必需的,因为它包含了XWPF,这是Apache POI中用于处理Open XML格式(.docx)的库。
读取Word文档
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadWordExample {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("example.doc");
POIFSFileSystem fs = new POIFSFileSystem(fis);
HWPFDocument doc = new HWPFDocument(fs);
// 获取文档文本
String text = doc.getText();
System.out.println(text);
fis.close();
}
}
创建Word文档
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class CreateWordExample {
public static void main(String[] args) throws IOException {
HWPFDocument doc = new HWPFDocument();
// 添加文本
Range range = doc.Range();
range.insertAfter("Hello, World!");
// 保存文档
try (OutputStream out = new FileOutputStream("created.doc")) {
doc.write(out);
}
}
}
修改Word文档
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ModifyWordExample {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("example.doc");
HWPFDocument doc = new HWPFDocument(fis);
// 修改文本
Range range = doc.Range();
range.replaceText("old text", "new text");
// 保存修改后的文档
try (FileOutputStream out = new FileOutputStream("modified.doc")) {
doc.write(out);
}
fis.close();
}
}
高级操作
操作表格
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableRow;
import java.io.FileInputStream;
public class TableExample {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("example.doc");
HWPFDocument doc = new HWPFDocument(fis);
Range range = doc.Range();
Table table = range.insertTableAfter(1, 3);
TableRow row = table.getRow(0);
TableCell cell = row.getCell(0);
cell.setText("Row 1, Cell 1");
fis.close();
}
}
格式化文本
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Range;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FormatTextExample {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("example.doc");
HWPFDocument doc = new HWPFDocument(fis);
Range range = doc.Range();
CharacterRun run = range.insertAfter("Bold and italic text").getCharacterRun(0);
run.setBold(true);
run.setItalic(true);
// 保存文档
try (FileOutputStream out = new FileOutputStream("formatted.doc")) {
doc.write(out);
}
fis.close();
}
}
XWPF
HWPF的基本概念
XWPF是Apache POI项目中的一个组件,用于操作Microsoft Word文档的新格式(.docx)。XWPF代表“XML Word Processor Format”,它允许用户在Java中读取、创建、修改Word文档。下面是一些基础的内容:
- XWPFDocument:这是XWPF的主要类,代表一个Word文档。你可以使用它来创建新的文档或加载现有的文档。
- XWPFParagraph:代表文档中的一个段落。每个XWPFDocument包含多个XWPFParagraph对象。
- XWPFRun:代表文档中的一个文本运行。每个XWPFParagraph包含多个XWPFRun对象。XWPFRun用于设置文本的格式,如字体、大小、颜色等。
- XWPFTable:代表文档中的一个表格。每个XWPFTable包含多个XWPFTableRow对象。
- XWPFTableRow:代表表格中的一行。每个XWPFTableRow包含多个XWPFTableCell对象。
- XWPFTableCell:代表表格中的一个单元格。
基本操作
读取Word文档
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
public class ReadWordExample {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream(new File("example.docx"));
XWPFDocument document = new XWPFDocument(fis);
// 获取所有段落
List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
System.out.println(paragraph.getText());
}
fis.close();
}
}
创建Word文档
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import java.io.FileOutputStream;
public class CreateWordExample {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
// 创建一个段落
XWPFParagraph paragraph = document.createParagraph();
paragraph.createRun().setText("Hello, World!");
// 保存文档
FileOutputStream out = new FileOutputStream(new File("created.docx"));
document.write(out);
out.close();
document.close();
}
}
修改Word文档
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class ModifyWordExample {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream(new File("example.docx"));
XWPFDocument document = new XWPFDocument(fis);
// 修改第一个段落的文本
XWPFParagraph paragraph = document.getParagraphs().get(0);
paragraph.createRun().setText("This is a modified paragraph.");
// 保存修改后的文档
FileOutputStream out = new FileOutputStream(new File("modified.docx"));
document.write(out);
out.close();
document.close();
fis.close();
}
}
高级操作
操作表格
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import java.io.File;
import java.io.FileInputStream;
public class TableExample {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream(new File("example.docx"));
XWPFDocument document = new XWPFDocument(fis);
// 创建一个表格
XWPFTable table = document.createTable();
// 创建一个行
XWPFTableRow row = table.getRow(0);
// 创建单元格并设置文本
XWPFTableCell cell = row.getCell(0);
cell.setText("Row 1, Cell 1");
fis.close();
}
}
格式化文本
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FormatTextExample {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream(new File("example.docx"));
XWPFDocument document = new XWPFDocument(fis);
// 创建一个段落
XWPFParagraph paragraph = document.createParagraph();
// 创建一个文本运行并设置格式
XWPFRun run = paragraph.createRun();
run.setText("Bold and italic text");
run.setBold(true);
run.setItalic(true);
// 保存文档
FileOutputStream out = new FileOutputStream(new File("formatted.docx"));
document.write(out);
out.close();
document.close();
fis.close();
}
}