Aspose.Words for Java 体验

公司中要做一些导出word的工作,经别人推荐,使用了Aspose.Words for Java ,感觉很好用,美中不足的地方就是,它是收费软件。

原理吗?比较常规,模板+入参==》aspose引擎==》生成文档。


在里,给大家提供一个简单的DEMO:


1、Maven依赖:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <dependency>  
  2.         <groupId>com.aspose</groupId>  
  3.         <artifactId>aspose-words</artifactId>  
  4.         <version>14.9.0</version>  
  5.         <classifier>jdk16</classifier>  
  6.     </dependency>  

2、word模板:



3、把license文件放入classpath。如果没有license会有水印。如果不想购买,又想用,请自己想办法。


4、核心代码:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.io.InputStream;  
  2. import java.util.HashMap;  
  3. import java.util.Iterator;  
  4.   
  5. import org.slf4j.Logger;  
  6. import org.slf4j.LoggerFactory;  
  7.   
  8. import com.aspose.words.Document;  
  9. import com.aspose.words.License;  
  10. import com.ths.platform.custom.util.Tool;  
  11.   
  12. public class RepDocTemplate {  
  13.   
  14.     // 默认没有license,会有水印文字  
  15.     private boolean isLicense = false;  
  16.   
  17.     // 初始化日志  
  18.     private static final Logger log = LoggerFactory  
  19.             .getLogger(RepDocTemplate.class);  
  20.   
  21.     /** 
  22.      * 私有构造,用户初始化License 
  23.      */  
  24.     public RepDocTemplate() {  
  25.         InputStream is = RepDocTemplate.class.getClassLoader()  
  26.                 .getResourceAsStream("license.xml");  
  27.         License aposeLic = new License();  
  28.         try {  
  29.             aposeLic.setLicense(is);  
  30.             isLicense = true;  
  31.         } catch (Exception e) {  
  32.             log.error("word模板破解失败!", e);  
  33.         }  
  34.     }  
  35.   
  36.     /** 
  37.      * 替换内容的主要操作 
  38.      *  
  39.      * @param input 
  40.      * @param output 
  41.      * @param datas 
  42.      */  
  43.     public void replaceDocTem(String input, String output,  
  44.             HashMap<String, Object> datas) {  
  45.   
  46.         if (isLicense) {  
  47.             try {  
  48.                 Document doc = new Document(input);  
  49.                 // 遍历要替换的内容  
  50.                 Iterator<String> keys = datas.keySet().iterator();  
  51.                 while (keys.hasNext()) {  
  52.                     String key = keys.next();  
  53.                     String value = String.valueOf(datas.get(key));  
  54.   
  55.                     // 对显示值得修改  
  56.                     if (Tool.isNull(value)) {  
  57.                         value = "";  
  58.                     }  
  59.                     value = value.replace("\r\n"" ");  
  60.   
  61.                     // 要求替换的内容是完全匹配时的替换  
  62.                     doc.getRange().replace("$" + key + "$", value, truefalse);  
  63.                 }  
  64.   
  65.                 // 替换保存后的内容  
  66.                 doc.save(output);  
  67.             } catch (Exception e) {  
  68.                 log.error(e.getMessage(), e);  
  69.             }  
  70.         }  
  71.     }  
  72.   
  73. }  

测试方法:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public void export_word() {  
  2.           
  3.         //准备数据,用于替换模板中的占位符  
  4.         //可以根据业务,从数据库中读取  
  5.         HashMap<String, Object> datas = new HashMap<String, Object>();  
  6.         datas.put("property_a""helloworld_a");   
  7.         datas.put("property_b""helloworld_b");   
  8.         datas.put("property_c""helloworld_c");   
  9.         datas.put("property_d""helloworld_d");   
  10.         datas.put("property_e""helloworld_e");   
  11.         datas.put("property_f""helloworld_f");   
  12.         datas.put("property_g""helloworld_g");   
  13.         datas.put("property_h""helloworld_h");   
  14.         datas.put("property_i""helloworld_i");   
  15.         datas.put("property_j""helloworld_j");   
  16.         datas.put("property_k""helloworld_k");   
  17.         datas.put("property_l""helloworld_l");   
  18.         datas.put("property_m""helloworld_m");   
  19.         datas.put("property_n""helloworld_n");   
  20.         datas.put("property_o""helloworld_o");   
  21.         datas.put("property_p""helloworld_p");   
  22.         datas.put("property_q""helloworld_q");   
  23.         datas.put("property_r""helloworld_r");   
  24.           
  25.           
  26.         String contextPath = request.getSession().getServletContext()  
  27.                 .getRealPath("/");  
  28.           
  29.         //模板文件名  
  30.         String docName="word_template.doc";  
  31.         //模板路径,示例写死饿,可以自行改为可配置的方式  
  32.         //String inPath = (PathUtil.webAppPath() + "/WEB-INF/office/word/").replace("/", File.separator)+ docName;  
  33.         String inPath = (contextPath + "solution/office/word/").replace("/", File.separator)+ docName;  
  34.         //生成word的名称  
  35.         String fileName = "word"+String.valueOf(System.currentTimeMillis())+".doc";  
  36.         //word生成路径  
  37.         String outPath = (contextPath + "solution/office/word-export/").replace("/", File.separator)+ fileName;  
  38.   
  39.         InputStream in = null;  
  40.         OutputStream out = null;  
  41.         byte[] buf = new byte[1024];  
  42.         int len = 0;  
  43.         try {  
  44.               
  45.             //传入参数,根据模板生成word  
  46.             RepDocTemplate doc = new RepDocTemplate();  
  47.             doc.replaceDocTem(inPath, outPath, datas);  
  48.   
  49.             //下载生成的word  
  50.             in = new FileInputStream(new File(outPath));  
  51.             response.setContentType("application/x-download;charset=UTF-8");  
  52.             response.setHeader("Content-Disposition""attachment; filename="  
  53.                     + URLEncoder.encode(fileName, "utf-8"));  
  54.             out = response.getOutputStream();  
  55.   
  56.             while ((len = in.read(buf)) > 0) {  
  57.                 out.write(buf, 0, len);  
  58.             }  
  59.   
  60.             in.close();  
  61.             out.flush();  
  62.             out.close();  
  63.         } catch (Exception e) {  
  64.             log.error(e.getMessage(), e);  
  65.         } finally {  
  66.             if (null != in) {  
  67.                 try {  
  68.                     in.close();  
  69.                 } catch (IOException e) {  
  70.                     e.printStackTrace();  
  71.                 }  
  72.             }  
  73.             if (null != out) {  
  74.                 try {  
  75.                     out.close();  
  76.                 } catch (IOException e) {  
  77.                     e.printStackTrace();  
  78.                 }  
  79.             }  
  80.         }  
  81.   
  82.     }  

5、结果




很简单,很强大。简单的东西一般都很强大。

想生成和操作内容复杂的word文档,请去参看官网,导出word,推荐使用Aspose.Words。


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值