文章目录
效果展示:实现过程:前提准备代码部分添加图片到word添加本地图片到word添加网络图片到word效果展示:
1.模板
2.实现效果:
实现过程:
前提准备
准备一个word文档,按照需求写导出模板。把写好的模板另存为一个xml文件修改xml文件的后缀为.ftl,例如test.xml—>test.ftl打开.ftl文件,超级混乱的文件,可以使用 在线格式化工具格式话,然后找到文档对应{id}等自定义部分,最后删除这一部分如图一,最后删除完格式如图二:
代码部分
1. 导入依赖
<!--导出word--><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId><version>2.0.2</version></dependency>
2.代码实现
注意不要导错jar包。
import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.Version;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.text.SimpleDateFormat;import java.util.HashMap;import java.util.Map;/*** @author lenovo*/@RestController@RequestMapping("/word")public class WordController {@GetMapping("/export1")public void export1(HttpServletRequest request, HttpServletResponse response) throws Exception {try {Map<String, Object> dataMap = new HashMap<String, Object>();//编号dataMap.put("id", "123456");//日期dataMap.put("date", new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse("-09-19")));//附件张数dataMap.put("number", 1);//受款人dataMap.put("payee", "张三");//付款用途dataMap.put("use_of_payment", "test");//大写金额dataMap.put("capitalization_amount", "200.00");//小写金额dataMap.put("lowercase_amount", "100");//Configuration 用于读取ftl文件Configuration configuration = new Configuration(new Version("2.3.0"));configuration.setDefaultEncoding("utf-8");//指定路径的第二种方式,我的路径是F:/test.ftlconfiguration.setDirectoryForTemplateLoading(new File("F:/"));//ftl文件目录//输出文档路径及名称File outFile = new File("F:\\受益人信息.doc");//以utf-8的编码读取ftl文件Template template = configuration.getTemplate("test.ftl", "utf-8");Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);template.process(dataMap, out);out.close();} catch (Exception e) {e.printStackTrace();}}}
添加图片到word
添加本地图片到word
新建一个word文档, 在要插入图片的地方随便插入一张图片将word另存为xml将xml扩展名改为ftl打开ftl文件, 搜索w:binData 或者 png可以快速定位图片的位置,图片 已经编码成0-Z的字符串了,修改成如下:
<w:binData w:name=“wordml://1.png”>${img}</w:binData>记得在代码中添加:
Map<String,String> dataMap = new HashMap<String,String>(); dataMap.put("img", getImageStr("F:\\Desktop\\图库\\linux.png"));
getImageStr(String imgFile)方法。
/*** 本地图片地址* @param imgFile 本地图片地址* @return*/public String getImageStr(String imgFile) {InputStream in = null;byte[] data = null;try {in = new FileInputStream(imgFile);data = new byte[in.available()];in.read(data);in.close();} catch (Exception e) {e.printStackTrace();}BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);}
添加网络图片到word
其他不变,修改
dataMap.put("img", getImageStr("网络图片url"));
添加方法
/*** 网络图片地址* @param imgFile* @return*/public String getImageStr(String imgFile) {InputStream in = null;byte[] data = null;try {in = getInput(imgFile);data = new byte[in.available()];in.read(data);in.close();} catch (Exception e) {e.printStackTrace();}BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);}/*** 网络图片转inputStream* @param picUrl* @return* @throws IOException*/public static InputStream getInput(String picUrl) {InputStream inputStream=null;HttpURLConnection connection=null;try {URL url = new URL(picUrl);if (url!=null){connection= (HttpURLConnection) url.openConnection();connection.setConnectTimeout(10000);connection.setDoInput(true);connection.setRequestMethod("GET");int responseCode = connection.getResponseCode();if (responseCode==200){inputStream = connection.getInputStream();}}} catch (Exception e) {e.printStackTrace();}return inputStream;}