java生成word报告echart_echarts 图形 导出到后台 word 图片加文字

用freemarker

先做一个word模板

public class ReportExporter extends AbstractServletHandler {

private static final DebugPrn log = new DebugPrn(ReportExporter.class.getName());

@Override

public String response(RequestParameters requestParameters) throws IPEGException {

initParameters(requestParameters);

String imgData = requestParameters.getRequest().getParameter("imgData").split(",")[1];

String exportedWord = createWord(WordData.getDataMap(imgData));

if(!"".equals(exportedWord)){

if(!ExportWord(exportedWord)){

return JsonHandlerUtils.faliureMessage;

}

}

return JsonHandlerUtils.successMessage;

}

private boolean ExportWord(String exportedWord) {

OutputStream toClient = null;

InputStream fis = null;

File wordFile = null;

try {

wordFile = new File(exportedWord);

String fileName = URLEncoder.encode(wordFile.getName(),"UTF-8");

if(fileName.length() > 150){

fileName = new String(fileName.getBytes("GBK"),"ISO-8859-1");

}

fis = new BufferedInputStream(new FileInputStream(wordFile));

byte[] buffer = new byte[fis.available()];

fis.read(buffer);

toClient = new BufferedOutputStream(response.getOutputStream());

response.reset();

response.setContentType("application/msword");

response.addHeader("Content-Disposition",

"attachment; filename=" + fileName);

response.addHeader("Content-Length",String.valueOf(wordFile.length()));

toClient.write(buffer);

toClient.flush();

} catch (Exception e) {

log.error("export word failed"+e.getMessage(), e);

return false;

}finally {

IOCloseUtil.close(fis);

IOCloseUtil.close(toClient);

wordFile.delete();

}

return true;

}

private String createWord(Map dataMap){

Configuration configuration = null;

configuration = new Configuration();

configuration.setDefaultEncoding("utf-8");

Template t = null;

try {

configuration.setDirectoryForTemplateLoading(new File(getTemplatePath()));

t = configuration.getTemplate("raplace_word.ftl");

} catch (IOException e) {

log.error("load template failed!"+e.getMessage(), e);

return "";

}

File outFile = new File(getWordPath());

Writer out = null;

FileOutputStream fos=null;

try {

fos = new FileOutputStream(outFile);

OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8");

out = new BufferedWriter(oWriter);

t.process(dataMap, out);

out.flush();

} catch (Exception e) {

log.error("report write word stream failed"+ e.getMessage(), e);

return "";

}finally {

IOCloseUtil.close(out);

IOCloseUtil.close(fos);

}

return getWordPath();

}

private String getTemplatePath(){

SystemSupportService smService = ServiceAccess.getSystemSupportService();

Par par = null;

try {

par = smService.getDeployedPar(ReportExporter.class);

} catch (SystemSupportException e) {

log.error("get Par‘s path failed!"+e.getMessage(), e);

}

return par.getBaseDir() +File.separator+ "conf"+File.separator+"template";

}

private String getWordPath(){

return getTemplatePath()+File.separator+"replaceWord.doc";

}

}

public class WordData {

public static Map getDataMap(String imgData) {

String img_data = imgData.replace(‘ ‘,‘+‘);

Map dataMap = new HashMap();

dataMap.put("city_feature_average", "dfdfdfdf");

dataMap.put("city_feature_level", "2222");

dataMap.put("city_feature_YoY_up_down", "33333");

dataMap.put("city_feature_MoM_up_down", "4444");

dataMap.put("city_feature_image", img_data);

return dataMap;

}

}

原文:http://www.cnblogs.com/hyp5490-/p/5541278.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Apache POI 库来实现将 Framer 生成Word 文档中的 FrameMarker 转换为图片,并将其插入到新的 Word 文档中。 下面是一个简单的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.poi.util.IOUtils; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFFrame; import org.apache.poi.xwpf.usermodel.XWPFPictureData; import org.apache.poi.xwpf.usermodel.XWPFRun; public class FramerToWordExporter { public static void main(String[] args) throws Exception { // 读取 Framer 生成Word 文档 InputStream framerDocInput = new FileInputStream("framer.docx"); XWPFDocument framerDoc = new XWPFDocument(framerDocInput); // 创建新的 Word 文档 XWPFDocument newDoc = new XWPFDocument(); // 遍历 Framer 生成Word 文档中的所有 FrameMarker for (XWPFFrame frame : framerDoc.getFrames()) { XWPFPictureData pictureData = frame.getPictureData(); if (pictureData != null) { // 将 FrameMarker 转换为图片并插入到新的 Word 文档中 byte[] pictureBytes = pictureData.getData(); XWPFRun run = newDoc.createParagraph().createRun(); run.addPicture(new ByteArrayInputStream(pictureBytes), XWPFDocument.PICTURE_TYPE_JPEG, "picture.jpg", Units.toEMU(200), Units.toEMU(200)); } } // 将新的 Word 文档保存到文件中 OutputStream newDocOutput = new FileOutputStream("new.docx"); newDoc.write(newDocOutput); // 关闭流 newDocOutput.close(); framerDocInput.close(); } } ``` 需要注意的是,上述示例代码中的 `Units.toEMU(200)` 表示图片的宽度和高度都为 200 EMU(English Metric Units)。你需要根据实际情况调整图片的大小。另外,由于涉及到文件读写操作,你还需要处理可能抛出的异常。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值