All Projects → kmood → officeexport-java

kmood / officeexport-java

Licence: Apache-2.0 license
三行代码导出自定义样式word

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to officeexport-java

Poi Tl
Generate awesome word(docx) with template
Stars: ✭ 2,306 (+3291.18%)
Mutual labels:  export, word, docx
Sharpdocx
C# based template engine for generating Word documents
Stars: ✭ 100 (+47.06%)
Mutual labels:  word, docx
Js Word
✒️ Word Processing Document Library
Stars: ✭ 1,203 (+1669.12%)
Mutual labels:  word, docx
Net Core Docx Html To Pdf Converter
.NET Core library to create custom reports based on Word docx or HTML documents and convert to PDF
Stars: ✭ 133 (+95.59%)
Mutual labels:  word, docx
Desktopeditors
An office suite that combines text, spreadsheet and presentation editors allowing to create, view and edit local documents
Stars: ✭ 1,008 (+1382.35%)
Mutual labels:  word, docx
Documentbuilder
ONLYOFFICE Document Builder is powerful text, spreadsheet, presentation and PDF generating tool
Stars: ✭ 61 (-10.29%)
Mutual labels:  word, docx
Etherpad Lite
Etherpad: A modern really-real-time collaborative document editor.
Stars: ✭ 11,937 (+17454.41%)
Mutual labels:  word, docx
Unioffice
Pure go library for creating and processing Office Word (.docx), Excel (.xlsx) and Powerpoint (.pptx) documents
Stars: ✭ 3,111 (+4475%)
Mutual labels:  word, docx
Phpstamp
The XSL-way templating library for MS Office Word DOCX documents.
Stars: ✭ 150 (+120.59%)
Mutual labels:  word, docx
Documentserver
ONLYOFFICE Document Server is an online office suite comprising viewers and editors for texts, spreadsheets and presentations, fully compatible with Office Open XML formats: .docx, .xlsx, .pptx and enabling collaborative editing in real time.
Stars: ✭ 2,335 (+3333.82%)
Mutual labels:  word, docx
filefy
A javascript library to produce downloadable files such as in CSV, PDF, XLSX, DOCX formats
Stars: ✭ 39 (-42.65%)
Mutual labels:  export, word
Docconv
Converts PDF, DOC, DOCX, XML, HTML, RTF, etc to plain text
Stars: ✭ 735 (+980.88%)
Mutual labels:  word, docx
Fiduswriter
Fidus Writer is an online collaborative editor for academics.
Stars: ✭ 405 (+495.59%)
Mutual labels:  word, docx
Superfileview
基于腾讯浏览服务Tbs,使用X5Webkit内核,实现文件的展示功能,支持多种文件格式
Stars: ✭ 1,115 (+1539.71%)
Mutual labels:  word, docx
Docx
a ruby library/gem for interacting with .docx files
Stars: ✭ 288 (+323.53%)
Mutual labels:  word, docx
Docx
Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
Stars: ✭ 2,150 (+3061.76%)
Mutual labels:  word, docx
GemBox.Document.Examples
Read, write, convert and print document files (DOCX, DOC, PDF, HTML, XPS, RTF, and TXT) in a simple and efficient way.
Stars: ✭ 53 (-22.06%)
Mutual labels:  word, docx
Gotenberg
A Docker-powered stateless API for PDF files.
Stars: ✭ 3,272 (+4711.76%)
Mutual labels:  word, docx
Docxtemplater
Generate docx pptx and xlsx (Microsoft Word, Powerpoint, Excel documents) from templates, from Node.js, the Browser and the command line / Demo: https://www.docxtemplater.com/demo
Stars: ✭ 1,990 (+2826.47%)
Mutual labels:  word, docx
Quip Export
Export all folders and documents from Quip
Stars: ✭ 28 (-58.82%)
Mutual labels:  export, docx

officeexport-java

jdk1.6+ freemarker-2.3.28+

解决哪些问题

  • 开发中偶尔会有固定模板导出word的需求,常见的导出通常通过直接修改xml或者通过工具库代码调整样式输出,这些方式开发困难并且不利于后期维护。
  • 一些有规则的文档需要人工填写,费时费力,此时配置数据库做模板渲染导出,如数据库设计文档等

简单介绍

officeexport-java基于Apache FreeMarker,遵从模板 + 数据模型 = 输出的理念, 通过极简API实现javaBean即数据源,模板即样式的Word导出,提供以下功能:

  • 基本文本的输出,文本占位符样式即输出文本样式。
  • 文本行、表格行单行或多行的遍历输出,并能够进行循环嵌套。
  • 提供数据处理的插件,通过添加处理器可定制任意输出值,例如:特定项的日期、数字等文本格式转换
  • 图片保留样式的输出。

现有实现方案的对比

方案 跨平台 易用性
Freemarker 跨平台 需要掌握freemarker的语法,配置xml结构,复杂模型导出非常繁琐
Apache POI Java组件,跨平台 需要掌握POIApi,对于复杂样式配置起来很复杂
officeexport Java组件,跨平台 拥有极简API,极简的模板语法,模板样式即输出样式(模板样式直接通过word调整)

快速开始

代码示例

1、直接调整word模板,添加占位符,在word2007 版本前需要导出word2003xml, word2007 版本之后直接调整word 即可。

2、Maven引入jar包,通过api导出

  
   <dependency>
       <groupId>com.github.kmood</groupId>
       <artifactId>officeexport-java</artifactId>
       <version>1.0.3.0-RELEASE</version>
   </dependency>

  HashMap<String, Object> data = new HashMap<>();
  ...准备数据
  data.put("zxsm",zxsmList);
  data.put("sbsm","kmood-导出-商标说明");
  
  DocumentProducer dp = new DocumentProducer(ActualModelPath);
  dp.Complie(xmlPath, "filename.xml",true);
  dp.produce(map, ExportFilePath);

示例1-文本输出

代码实例

         HashMap<String, Object> map = new HashMap<>();
         map.put("text", "kmood-文本占位输出");
         map.put("text1", "kmood-文本占位输出2");
         DocumentProducer dp = new DocumentProducer(ActualModelPath);
         String complie = dp.Complie(xmlPath, "text.xml", true);
         dp.produce(map, ExportFilePath);

实现效果

示例2-文本循环输出

代码实例

        //准备数据
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<Object> tables = new ArrayList<>();
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("text", "kmood-文本占位输出-循环1");
        map1.put("text1", "kmood-文本占位输出2-循环1");
        tables.add(map1);
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("text", "kmood-文本占位输出-循环2");
        map2.put("text1", "kmood-文本占位输出2-循环2");
        tables.add(map2);
        map.put("tables",map);
        //编译输出
        DocumentProducer dp = new DocumentProducer(ActualModelPath);
        String complie = dp.Complie(xmlPath, "textf.xml", true);
        dp.produce(map, ExportFilePath);

实现效果

示例3-文本、表格循环输出

代码实例

        //准备数据
       HashMap<String, Object> map = new HashMap<>();
       ArrayList<Object> tables = new ArrayList<>();
       HashMap<String, Object> map1 = new HashMap<>();
       map1.put("text", "kmood-文本占位输出-循环1");
       map1.put("text1", "kmood-文本占位输出2-循环1");
       //表格数据
       ArrayList<Object> columns1= new ArrayList<>();
       HashMap<String, Object> row = new HashMap<>();
       row.put("column1", "kmood-table1-column1-row1");
       row.put("column2", "kmood-table1-column2-row1");
       columns1.add(row);
       HashMap<String, Object> row2 = new HashMap<>();
       row2.put("column1", "kmood-table1-column1-row2");
       row2.put("column2", "kmood-table1-column2-row2");
       columns1.add(row2);
       map1.put("columns", columns1);
       tables.add(map1);

       HashMap<String, Object> map2 = new HashMap<>();
       map2.put("text", "kmood-文本占位输出-循环2");
       map2.put("text1", "kmood-文本占位输出2-循环2");
       //表格数据
       ArrayList<Object> columns2= new ArrayList<>();
       HashMap<String, Object> row3 = new HashMap<>();
       row3.put("column1", "kmood-table2-column1-row1");
       row3.put("column2", "kmood-table2-column2-row1");
       columns2.add(row3);
       HashMap<String, Object> row4 = new HashMap<>();
       row4.put("column1", "kmood-table2-column1-row2");
       row4.put("column2", "kmood-table2-column2-row2");
       columns2.add(row4);
       map2.put("columns", columns2);

       tables.add(map2);
       map.put("tables",tables);
       //编译输出
       DocumentProducer dp = new DocumentProducer(ActualModelPath);
       String complie = dp.Complie(xmlPath, "table.xml", true);
       dp.produce(map, ExportFilePath);

实现效果

示例4-图片输出

docx文档中图片占位 : 文本占位: {^mypicture^} ;图片占位:(在图片的可选文字中配置占位符{^mypicture^})详见wiki

代码实例

     Class<? extends Class> aClass = Main.class.getClass();
     ClassLoader classLoader = aClass.getClassLoader();
     if (classLoader == null){
        classLoader = ClassLoader.getSystemClassLoader();
     }
     String ActualModelPath = classLoader.getResource("./model/").toURI().getPath();
     String xmlPath = classLoader.getResource("./model").toURI().getPath();
     String ExportFilePath = classLoader.getResource(".").toURI().getPath() + "/picture.doc";
     
     HashMap<String, Object> map = new HashMap<>();
     //读取输出图片
     URL introUrl = classLoader.getResource("./picture/exportTestPicture-intro.png");
     URL codeUrl = classLoader.getResource("./picture/exportTestPicture-code.png");
     URL titleUrl = classLoader.getResource("./picture/exportTestPicture-title.png");
     
     String intro = Base64.getEncoder().encodeToString(FileUtils.readToBytesByFilepath(introUrl.toURI().getPath()));
     map.put("intro", intro);
     String code = Base64.getEncoder().encodeToString(FileUtils.readToBytesByFilepath(codeUrl.toURI().getPath()));
     map.put("code", code);
     map.put("title", Base64.getEncoder().encodeToString(FileUtils.readToBytesByFilepath(titleUrl.toURI().getPath())));
     //编译输出
     DocumentProducer dp = new DocumentProducer(ActualModelPath);
     String complie = dp.Complie(xmlPath, "picture.xml", true);
     dp.produce(map, ExportFilePath);

实现效果

完整导出示例

中文文档:https://github.com/kmood/officeexport-java/wiki

注意!

模板配置注意


  变量命名只能使用:字母 数字 $ _ (下划线)
  
  中文也可以用来命名变量,但最好不这样写。
  
  变量第一个字符只能使用:字母 $ _ (下划线)
  
  变量第一个字符不能使用:数字

建议和完善

欢迎在GitHub Issue中提问和交流。有问题可通过微信咨询: 1761104164、18330019132

贡献者

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].