1、处理方案
private static Set<String> richTxtDateSet = new HashSet<String>();
public static byte[] exportWord07(byte[] content, Map<String, Object> map) {
byte[] result = resolveDocx(content, map);
return resolveRichTxt(result, map, SaveFormat.DOCX);
}
public static byte[] exportWord03(byte[] content, Map<String, Object> param){
content = AsposeWordUtils.docToDocx(content);
byte[] result = resolveDocx(content, param);
return resolveRichTxt(result, param, SaveFormat.DOC);
}
private static byte[] resolveRichTxt(byte[] content, Map<String, Object> map, int saveFormat) {
String htmlStr = byteToHtmlStr(content);
for (String placeHolder : richTxtDateSet) {
try {
htmlStr = htmlStr.replace(placeHolder, getRealValue(placeHolder, map).toString());
} catch (Exception e) {
logger.error("富文本处理异常", e);
}
}
richTxtDateSet.clear();
return AsposeWordUtils.htmlToWord(htmlStr.getBytes(StandardCharsets.UTF_8), saveFormat);
}
private static String byteToHtmlStr(byte[] content) {
String result = "";
try {
byte[] htmlContent = AsposeWordUtils.wordToHtml(content);
InputStream is = new ByteArrayInputStream(htmlContent);
InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(streamReader);
String line;
StringBuilder html = new StringBuilder();
while ((line = reader.readLine()) != null) {
html.append(line);
}
reader.close();
result = String.valueOf(html);
} catch (IOException e) {
logger.error("html转字符串异常", e);
}
return result;
}
2、AsposeWordUtils工具类
public static byte[] htmlToWord(byte[] content, Integer toType) {
byte[] result = new byte[1];
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream is = new ByteArrayInputStream(content);
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(streamReader);
String line;
StringBuilder html = new StringBuilder();
while ((line = reader.readLine()) != null) {
html.append(line);
}
reader.close();
builder.insertHtml(String.valueOf(html));
doc.save(os, toType);
log.info("html转word成功!");
result = os.toByteArray();
} catch (Exception e) {
log.error("html转word失败!", e);
}
return result;
}
public static byte[] wordToHtml(byte[] content) {
byte[] result = new byte[1] ;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream sbs = new ByteArrayInputStream(content);
Document document = new Document(sbs);
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.HTML);
options.setExportImagesAsBase64(true);
document.save(os, options);
log.info("html转word成功!");
result = os.toByteArray();
} catch (Exception e) {
log.error("word转html失败!", e);
}
return result;
}
public static byte[] docToDocx(byte[] content) {
byte[] result = new byte[1];
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream sbs = new ByteArrayInputStream(content);
com.aspose.words.Document doc = new com.aspose.words.Document(sbs);
doc.save(os, SaveFormat.DOCX);
result = os.toByteArray();
} catch (Exception e) {
log.error("doc转Docx失败!", e);
}
return result;
}
Java使用aspose.word完美实现docx转doc
|