java使用xwpf 导入word为xhtml中文乱码问题记录

6 篇文章 0 订阅

我使用xdocreport的xwpf来读取docs文档,并转换成html, 代码如下

核心代码如下:

public static String word2htmlString(XWPFDocument document, FileImageExtractorImpl imageProcessor) throws IOException {

String result = null;

ByteArrayOutputStream htmlOutputStream = null;

try {

XHTMLOptions options = XHTMLOptions.create();

options.setExtractor(imageProcessor);

options.setIgnoreStylesIfUnused(true);

options.setFragment(true);

htmlOutputStream = new ByteArrayOutputStream();

XHTMLConverter.getInstance().convert(document, htmlOutputStream, options);

result = imageProcessor.convertLocalUrlToRemoteUrl(htmlOutputStream.toString("utf-8"));

} catch (Exception e) {

throw new AppBizException(HttpResult.ERROR_TEMPLATE_PARSE_ERROR, e.getMessage());

} finally {

if (null != htmlOutputStream)

htmlOutputStream.close();

}

return result;

}

依赖是

 

<dependency>

<groupId>fr.opensagres.xdocreport</groupId>

<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>

<version>1.0.5</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>

<version>3.10</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-scratchpad</artifactId>

<version>3.12</version>

</dependency>



<dependency>

<groupId>com.alibaba</groupId>

<artifactId>easyexcel</artifactId>

<version>2.2.6</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.12</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>3.12</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml-schemas</artifactId>

<version>3.12</version>

</dependency>

 

 

 

后面为了集成eaxyexcel, poi版本必须升级成3.17, 依赖变成了

 

<dependency>

<groupId>fr.opensagres.xdocreport</groupId>

<artifactId>fr.opensagres.poi.xwpf.converter.xhtml</artifactId>

<version>2.0.1</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>

<version>3.10</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-scratchpad</artifactId>

<version>3.17</version>

</dependency>



<dependency>

<groupId>com.alibaba</groupId>

<artifactId>easyexcel</artifactId>

<version>2.2.6</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.17</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>3.17</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml-schemas</artifactId>

<version>3.17</version>

</dependency>

 

 

结果在导入word 的时候,window和ubuntu都没问题,到了docker里头就乱码,中文变成问号????,这样,刚开始怀疑是字体的问题,导入了中文字体也没用,最后只能使用idea 连接docker进行远程调试,发现这个问题代码

private void write( String content )

throws SAXException

{

try

{

if ( out != null )

{

out.write( content.getBytes() );

}

else

{

writer.write( content );

}

}

catch ( IOException e )

{

throw new SAXException( e );

}

}

 

 

注意 content.getBytes() );,这里头有这个代码

static byte[] encode(byte coder, byte[] val) {

Charset cs = Charset.defaultCharset();

 

一打印这个值 ,发现是US-ASCII,问题找到,赶紧百度,去设置docker的默认字符集

往Dockerfile里头设置

ENV LANG C.UTF-8

参考链接

https://blog.csdn.net/u011077672/article/details/70569849

 

再重新制作镜像,果然就行了,没有乱码了

再调试,Charset.defaultCharset() 就成utf-8了

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,将 PDF 转为 Word 有很多种方法,这里介绍使用 Apache POI 和 iText 来实现。POI 提供了对 Word 文档的操作,而 iText 可以解析 PDF 文件,并提供了一些字体处理的功能。下面是一个简单的示例代码,可以将 PDF 转为 Word 并解决中文问题: ``` import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.parser.PdfTextExtractor; import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy; public class PdfToWordConverter { public static void main(String[] args) throws Exception { // 1. 读取 PDF 文件 PdfReader reader = new PdfReader(new FileInputStream(new File("input.pdf"))); // 2. 获取 PDF 文件中的文本 StringBuilder sb = new StringBuilder(); for (int i = 1; i <= reader.getNumberOfPages(); i++) { String text = PdfTextExtractor.getTextFromPage(reader, i, new SimpleTextExtractionStrategy()); sb.append(text); } reader.close(); // 3. 将文本写入 Word 文件 XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(sb.toString()); run.setColor(Color.BLACK); // 4. 保存 Word 文件 FileOutputStream out = new FileOutputStream(new File("output.docx")); document.write(out); out.close(); document.close(); } } ``` 在上面的示例代码中,我们首先使用 iText 读取 PDF 文件,并获取 PDF 文件中的文本。然后使用 POI 创建一个新的 Word 文档,将文本写入 Word 文件中,并设置文本颜色。最后将 Word 文件保存到本地磁盘上。 需要注意的是,上面的代码只能处理纯文本的 PDF 文件,并且对于一些特殊的字体或字符,可能仍然会出现问题。如果需要处理 PDF 中的图片或表格,可以使用其他的 PDF 转 Word 工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值