phpword读取doc文件_java读取带图片的doc文件

适用场景:java读取带图片或不带图片的doc文件

1.带图片的doc文件格式

99d4cd2d01aca53c7a3468038cb15716.png

2.运行代码

5644c1905de5528e9ce456832d457408.png

生成的html格式文件:

a1a7be97c47a76b69577f28471b3dd30.png

生成到目录的图片:

68a8a50cb903d2209c07bf22acff8285.png

双击打开csDoc.html:

de4ef891bd6a9fb430df74b7caafb6d1.png

代码:


package com.test;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Random;

import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hwpf.model.PicturesTable;

import org.apache.poi.hwpf.usermodel.CharacterRun;

import org.apache.poi.hwpf.usermodel.Picture;

import org.apache.poi.hwpf.usermodel.Range;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)

@SpringBootTest

public class TestDoc {

@Test

public void testReadImgDoc() {

System.out.println(analyzeDoc());

}

private String analyzeDoc(){

//对应桌面地址的doc格式文件

String path="C:甥敳獲AdministratorDesktopcs.doc";

StringBuffer tempBuffer = new StringBuffer();

InputStream in = null;

try

{

in = new FileInputStream(path);

HWPFDocument doc=new HWPFDocument(in);

// 取得文档中字符的总数

int length = doc.characterLength();

// 创建图片容器

PicturesTable pTable = doc.getPicturesTable();

//存放图片路径

String fileName = "D:甥汰慯dcs";

for (int i = 0; i < length - 1; i++) {

// 整篇文章的字符通过一个个字符的来判断,range为得到文档的范围

Range range = new Range(i, i + 1, doc);

CharacterRun cr = range.getCharacterRun(0);

if (pTable.hasPicture(cr)) {

String pic = readPicture(pTable, cr,i,fileName);

// 读写图片

tempBuffer.append("");

}else {

char c = cr.text().charAt(0);

// 判断为回车符

if (c == 13) {

tempBuffer.append("
");

}else {

tempBuffer.append(cr.text());

}

}

}

}catch (FileNotFoundException e) {

//异常按业务场景处理

}catch (IOException e) {

//异常按业务场景处理

}

finally {

if(in != null) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

String docxContent = tempBuffer.toString();

PrintWriter pw = null;

try {

//对应桌面地址

pw = new PrintWriter("C:甥敳獲AdministratorDesktopcsDoc.html");

//输出到html,然后桌面找到csDoc.html打开看效果

pw.write(docxContent);

} catch (FileNotFoundException e) {

}finally {

if(pw != null) {

pw.close();

}

}

return docxContent;

}

private String readPicture(PicturesTable pTable, CharacterRun cr,int i,String imgPath) {

// 提取图片

Picture pic = pTable.extractPicture(cr, false);

// 返回POI建议的图片文件名

String img = pic.suggestFullFileName();

img = createNumber() +i+ img;

String fileName = imgPath + img;

File file = new File(fileName);

//如果路径不存在,则创建

if (!file.getParentFile().exists()) {

file.getParentFile().mkdirs();

}

OutputStream out=null;

try {

out = new FileOutputStream(new File(fileName));

pic.writeImageContent(out);

}catch(Exception e) {

//异常按业务需求处理

return "";

}finally {

if(out != null) {

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return fileName;

}

/**

* 生成18位编号,yyyyMMddHHmmss+4位随机数(共18位)

*/

private String createNumber(){

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

Random r = new Random();

return sdf.format(new Date())+(r.nextInt(9000)+1000);

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值