Java读取Excel并生成WordPDF
- 最近需要用Java处理Excel中的数据,并生成word和pdf格式的文档(其中word和pdf需要带背景图),所以在这里做下总结。
- 使用技术:FreeMarker + easyexcel + jacob
①FreeMarker大家应该会比较熟悉,一个模板引擎。
②easyexcel 是阿里巴巴开源,用于快速、简单避免OOM的java处理Excel工具。
②JACOB是一个JAVA-COM Bridge,允许您从Java调用COM Automation组件。它使用JNI对COM和Win32库进行本机调用。
- 使用easyexcel处理excel
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/*** JavaBean与Excel之间的关系映射*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ERReport extends BaseRowModel {@ExcelProperty(value = "省", index = 0)private String province;@ExcelProperty(value = "市", index = 1)private String city;@ExcelProperty(value = "学校", index = 2)private String school;@ExcelProperty(value = "班级", index = 3)private String classs;@ExcelProperty(value = "老师", index = 4private String teacher;
}
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.BaseRowModel;
import com.google.common.collect.Lists;
/*** 自定义Listener继承AnalysisEventListener,重写invoke方法,为了方便获取值,添加了一个getData的方法*/
public class ExcelListener<T extends BaseRowModel> extends AnalysisEventListener<T> {private final List<T> data = Lists.newArrayList();@Overridepublic void invoke(T t, AnalysisContext analysisContext) {data.add(t);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {}public List<T> getData(){return data;}
public class ExcelUtils {/*** 读取excel的方法*/public static <T extends BaseRowModel> List<T> readExcel(final InputStream inputStream, final Class<? extends BaseRowModel> clazz) {if (null == inputStream) {throw new NullPointerException("the inputStream is null!");}AnalysisEventListener listener = new ExcelListener();ExcelReader reader = new ExcelReader(inputStream, null, listener);reader.read(new com.alibaba.excel.metadata.Sheet(1, 1, clazz));return ((ExcelListener) listener).getData();}public static void main(String[] args) throws Exception {String filePath = "C:\\Users\\Administrator\\Desktop\\test.xlsx";List<ERReport> datas = ExcelUtils.readExcel(new FileInputStream(new File(filePath)), ERReport.class);datas.forEach(System.out::println);}
- 新建一个word文件,把固定不变的地方写好,将要替换的值改用${xxx}占位符的方式,然后把后缀名改为.ftl。(注:如果此条路行不通,可以先将word另存为xml文件,再将xml转为ftl格式),放到项目的resource/templates目录下,便于读取。
- 如果需要设置图片,打开ftl文件后,搜索
<w:binData></w:binData>
标签(我用的是wps,如果是office word,可能并不一定是这个标签),这里正常显示的会是一长串base64编码后的字符串。因为这里我需要替换图片,所以用了占位符。
- 转为word的代码如下:
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import sun.misc.BASE64Encoder;import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class WordUtils {public static void main(String[] args) throws Exception {String imgPath = "C:\\Users\\Administrator\\Desktop\\timg.jpg";BASE64Encoder encoder = new BASE64Encoder();String imgStr = encoder.encode(encode(imgPath));System.out.println(imgStr);// 这里只是一个示例,key=value的格式,这里的key对应的ftl文件占位符{}中的字符串。Map<String, Object> dataMap = new HashMap<String, Object>() {{put("imgStr",imgStr);put("province","北京市");put("city","海淀区");put("school","");put("class","二三班");}};//处理excel,将其中的数据填充至模板中,最后生成word。List<ERReport> datas = ExcelUtils.readExcel(new FileInputStream(new File(filePath)), ERReport.class);datas.forEach((ERReport e) -> {try {processTemplate(e, “Your File Name”);System.out.println("转换成功");} catch (Exception ex) {LOGGER.error("Excel转换Word错误.", ex);}});}private static byte[] encode(String imagePath) throws IOException {InputStream in = null;byte[] data = null;try {in = new FileInputStream(imagePath);data = new byte[in.available()];in.read(data);in.close();} catch (Exception e) {e.printStackTrace();} finally {if (in != null) {in.close();}return data;}}public static void processTemplate(Object dataMap, String fileName) throws IOException, TemplateException {Configuration configuration = new Configuration();configuration.setDefaultEncoding("utf-8");configuration.setClassForTemplateLoading(WordUtils.class, "/templates");Template template = configuration.getTemplate("template.ftl");File file = new File("C:\\Users\\Administrator\\Desktop\\" + fileName + ".docx");BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));template.process(dataMap, bufferedWriter);}
}
- word转pdf(使用jacob)
所需依赖我放在百度云盘上了,如果有用到的朋友遇到问题,可以来微博或者github上面找我: https://pan.baidu.com/s/1uV54gViWt9x3eDYB0OAM0w 提取码: yy95
解压后会是下图所示,将两个dll文件放在jdk的bin目录下, 将jacob.jar导入项目中。
- word转pdf的代码如下:
public class PDFUtils {public static void main(String[] args) {String wordFile = "C:\\Users\\Administrator\\Desktop\\二年级五班.docx";String pdfFile = "C:\\Users\\Administrator\\Desktop\\二年级五班.pdf";processWordToPdf(wordFile,pdfFile);}public static void processWordToPdf(String wordFile,String pdfFile){ActiveXComponent app = null;System.out.println("开始转换...");// 开始时间long start = System.currentTimeMillis();try {// 打开wordapp = new ActiveXComponent("Word.Application");// 获得word中所有打开的文档Dispatch documents = app.getProperty("Documents").toDispatch();System.out.println("打开文件: " + wordFile);// 打开文档Dispatch document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch();// 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在File target = new File(pdfFile);if (target.exists()) {target.delete();}System.out.println("另存为: " + pdfFile);// 另存为,将文档报错为pdf,其中word保存为pdf的格式宏的值是17Dispatch.call(document, "SaveAs", pdfFile, 17);// 关闭文档Dispatch.call(document, "Close", false);// 结束时间long end = System.currentTimeMillis();System.out.println("转换成功,用时:" + (end - start) + "ms");} catch (Exception e) {System.out.println("转换失败" + e.getMessage());} finally {// 关闭officeapp.invoke("Quit", 0);}}
}
- 总结
总体思路:利用easyexcel读取excel里面的数据,然后利用事先定义好的freemarker模板,往里面填充值,最后生成word,再利用jacob根据word生成pdf。(代码中用到了lombok和google guava,所以可能直接复制以上代码并不能直接使用)
Java读取Excel并生成WordPDF相关推荐
- JAVA读取Excel表格,建数据库建表,并生成java实体实例
转自:https://blog.csdn.net/wuyongde_0922/article/details/43233413 JAVA读取Excel表格,建数据库建表,并生成java实体实例,这一篇 ...
- Flex通过Java读取Excel(详细流程)----Excel在客户端(DataGrid动态根据Excel生成)
2011-03-06 23:57:00| 分类: flex/flash | 标签:excel flex java 客户端 as3xls |字号大中小 订阅 在网上查了很久的资料,就是 ...
- python生成表格文件_python 读取excel文件生成sql文件实例详解
python 读取excel文件生成sql文件实例详解 学了python这么久,总算是在工作中用到一次.这次是为了从excel文件中读取数据然后写入到数据库中.这个逻辑用java来写的话就太重了,所以 ...
- 读取excel批量生成二维码
昨天工作需要,让生成二维码,让用草料生成,就需要一个个的复制粘贴,有点麻烦.关键是量特别大,如果传统的复制粘贴要很长时间才可以. 后来想到用程序生成.于是百度了一下生成二维码的方法,别说还很简单,把代 ...
- 读取Excel内容生成PNG图片
读取Excel内容生成PNG图片 1.需要的jar包 2.需要的pojo类 3.核心代码 本文转载自 心淡人也懒 的博客 https://blog.csdn.net/u014730287/articl ...
- Java读取excel指定行列的数据(通过jxl的方式)
目录 Java读取Excel指定列的数据详细教程和注意事项(通过jxl的方式) 1.前言 2.注意事项 3.主要的API说明 4.示例代码 Java读取Excel指定列的数据详细教程和注意事项(通过j ...
- Java读取Excel文件数据并将记录写入到新的文件中--POI技术实现
1. apache poi包里有4个主要的类: Workbook----工作表,通过WorkbookFactory的create(FileInputStream fis)方法获取, Sheet---- ...
- java读取Excel 错误 jxl read biff BiffException Unable to recog
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! java ...
- java读取excel(java读取excel表格存入数据库)
怎么用java读取excel 里面的数据 * excel文件的读入和更新 * 使用的是jxl 技术. public static void main(String[] args) throws Row ...
最新文章
- android类名方法名不混淆,android – 如何告诉Proguard混淆类名
- 改正错误:tensorflow.python.framework.errors_impl.NotFoundError: FindFirstFile failed for: ...
- 放个手机在单位自动打卡_1秒识别打卡,无感知考勤系统重磅来袭!
- ASP.NET Core IHostEnvironment和IApplicationLifetime介绍
- TP框架如何绑定参数。目的进行ajax验证
- 使用Python获取Linux系统的各种信息
- 计算机应用基础自考,自考计算机应用基础
- 重磅综述:三万字长文读懂单细胞RNA测序分析的最佳实践教程 (原理、代码和评述)
- mysql连接池设计_数据库连接池(基于MySQL数据库)
- MyBitis(iBitis)系列随笔之四:多表(多对一查询操作)
- C#窗体无法接受Keydown事件
- 计算机科学论文生成器,数学论文生成器:从此一天一篇不再愁
- 精通javascript 代码总结
- 21天c语言 ppt,21天学通C语言 第1章 C语言与程序概述.ppt
- AutoLisp从入门到放弃(四)
- 金蝶K3安装环境检测缺少microsoft vm for java组件解决办法
- Unity中使用的四种存档和读档方式
- goinception审核规则
- C++封装zlib库
- 网络营销相关名词解释