word文档中内容如下:
渠道编码:________________232254344________
要求获取渠道编码。
代码如下:
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class demo {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream("D:/Backup/Downloads/demo.docx");
XWPFDocument document = new XWPFDocument(file);
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
String text = extractor.getText();
System.out.println(text);
Pattern pattern = Pattern.compile("渠道编码:_+(\\d+)_+");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
// 提取渠道编码的值
String channelCode = matcher.group(1);
System.out.println("渠道编码的值是: " + channelCode);
} else {
System.out.println("未找到渠道编码");
}
extractor.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}