JAVA中使用openoffice将Excel转PDF再转图片功能的实现代码
需求
公司一个小项目要结尾了, 有非常多的表格, 而且非常复杂, 例如
例如:(这表格, 有想死的心…)
看到这样的表格,第一反应是n脸懵逼,这得做到啥时候啊,最后想到用一个方法,让客户自己玩!!
平台提供excel模版下载,客户下载填写完再提交excel,平台再将excel转pdf,顺便将pdf转成图片,给前端展示,想法简单,方案可行,开工!
方案:
- windows server下用 jacob, windows上可完美运行, 但是!!!linux上不支持jacob!!!(非常重要!!)
- poi + itext,貌似不错
- openoffice,跨平台
本文选用openoffice,各位看官可自选用itext + poi,自己判别!当然也可以用jacob,前提是你不需要在linux中运行项目。
安装软件:下载传送门
win:
下载完成后,一路next,最好默认路径安装,免得折腾
完成后 win + r 填入 dcomcnfg 打开组件服务
设置openoffice组件服务, 都设成system
再设置标识为:交互式用户
进入到安装目录,如果路径是默认的,则在 c:\program files (x86)\openoffice 4\program 下面,
进到这个路径下,按住 shift 再点击鼠标右键,在此处打开命令窗口,输入 soffice -headless-accept=“socket,host=127.0.0.1,port=8100;urp;”-nofirststartwizard, 查看是否启动成功, 黑窗口输入 netstat -ano
windows下安装完毕!
参考: https://blog.csdn.net/qq_30987095/article/details/78789103
linux:下载传送门
根据自己的linux系统选择rpm包或者deb,如果是ubuntu则选deb,其他就选rpm包,查看当前操作系统是ubuntu还是centos
[root@iz2zeg3etf08f6snef3lgrz ~]# lsb_release -a lsb version: :(略) distributor id: centos description: centos linux release (略) release: (略) codename: core
下载完毕后,利用ssh工具上传到服务器上!
> tar -zxvf apache_openoffice_4.1.3_linux_x86-64_install-rpm_zh-cn.tar.gz (略)...... > ls zh-cn > cd zh-cn/rpms > ls openoffice-core01-4.1.3-9783.x86_64.rpm openoffice-impress-4.1.3-9783.x86_64.rpm openoffice-zh-cn-4.1.3-9783.x86_64.rpm openoffice-4.1.3-9783.x86_64.rpm openoffice-core02-4.1.3-9783.x86_64.rpm openoffice-javafilter-4.1.3-9783.x86_64.rpm openoffice-zh-cn-base-4.1.3-9783.x86_64.rpm openoffice-base-4.1.3-9783.x86_64.rpm openoffice-core03-4.1.3-9783.x86_64.rpm openoffice-math-4.1.3-9783.x86_64.rpm openoffice-zh-cn-calc-4.1.3-9783.x86_64.rpm openoffice-brand-base-4.1.3-9783.x86_64.rpm openoffice-core04-4.1.3-9783.x86_64.rpm openoffice-ogltrans-4.1.3-9783.x86_64.rpm openoffice-zh-cn-draw-4.1.3-9783.x86_64.rpm openoffice-brand-calc-4.1.3-9783.x86_64.rpm openoffice-core05-4.1.3-9783.x86_64.rpm openoffice-onlineupdate-4.1.3-9783.x86_64.rpm openoffice-zh-cn-help-4.1.3-9783.x86_64.rpm openoffice-brand-draw-4.1.3-9783.x86_64.rpm openoffice-core06-4.1.3-9783.x86_64.rpm openoffice-ooofonts-4.1.3-9783.x86_64.rpm openoffice-zh-cn-impress-4.1.3-9783.x86_64.rpm openoffice-brand-impress-4.1.3-9783.x86_64.rpm openoffice-core07-4.1.3-9783.x86_64.rpm openoffice-ooolinguistic-4.1.3-9783.x86_64.rpm openoffice-zh-cn-math-4.1.3-9783.x86_64.rpm openoffice-brand-math-4.1.3-9783.x86_64.rpm openoffice-draw-4.1.3-9783.x86_64.rpm openoffice-pyuno-4.1.3-9783.x86_64.rpm openoffice-zh-cn-res-4.1.3-9783.x86_64.rpm openoffice-brand-writer-4.1.3-9783.x86_64.rpm openoffice-gnome-integration-4.1.3-9783.x86_64.rpm openoffice-ure-4.1.3-9783.x86_64.rpm openoffice-zh-cn-writer-4.1.3-9783.x86_64.rpm openoffice-brand-zh-cn-4.1.3-9783.x86_64.rpm openoffice-graphicfilter-4.1.3-9783.x86_64.rpm openoffice-writer-4.1.3-9783.x86_64.rpm openoffice-calc-4.1.3-9783.x86_64.rpm openoffice-images-4.1.3-9783.x86_64.rpm openoffice-xsltfilter-4.1.3-9783.x86_64.rpm [注释:运行下面命令, 生成 desktop-integration包] > yum localinstall *.rpm > (略...) > cd desktop-integration/ > ls > openoffice4.1.3-freedesktop-menus-4.1.3-9783.noarch.rpm openoffice4.1.3-mandriva-menus-4.1.3-9783.noarch.rpm openoffice4.1.3-redhat-menus-4.1.3-9783.noarch.rpm openoffice4.1.3-suse-menus-4.1.3-9783.noarch.rpm > yum localinstall openoffice4.1.3-redhat-menus-4.1.3-9783.noarch.rpm
ok, 安装完毕了! 启动服务
进入到默认安装目录下
> cd /opt/openoffice4/program/ [注释:临时启动] > /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard [注释:或后台启动] > /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & [注释:永久启动] > nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & [注释:查看是否启动成功] > ps -ef|grep openoffice root 7541 7527 0 10:19 pts/0 00:00:12 /opt/openoffice4/program/soffice.bin -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard root 8964 7066 0 18:18 pts/3 00:00:00 grep --color=auto openoffice 或者 > netstat -tunlp |grep tcp 0 0 127.0.0.1:8100 0.0.0.0:* listen 7541/soffice.bin
linux下安装成功!
开始撸码:
pom.xml
<!-- openoffice 相关依赖 --> <dependency> <groupid>commons-io</groupid> <artifactid>commons-io</artifactid> <version>1.4</version> </dependency> <dependency> <groupid>org.openoffice</groupid> <artifactid>juh</artifactid> <version>3.2.1</version> </dependency> <dependency> <groupid>org.openoffice</groupid> <artifactid>ridl</artifactid> <version>3.2.1</version> </dependency> <dependency> <groupid>org.openoffice</groupid> <artifactid>unoil</artifactid> <version>3.2.1</version> </dependency> <!-- pdf2img --> <dependency> <groupid>org.apache.pdfbox</groupid> <artifactid>pdfbox</artifactid> <version>2.0.6</version> </dependency> <dependency> <groupid>org.apache.pdfbox</groupid> <artifactid>pdfbox-tools</artifactid> <version>2.0.6</version> </dependency>
openofficeutils
import java.awt.*; import java.io.file; import java.io.inputstream; import java.io.outputstream; import java.net.connectexception; import org.apache.commons.lang.stringutils; import com.artofsolving.jodconverter.defaultdocumentformatregistry; import com.artofsolving.jodconverter.documentconverter; import com.artofsolving.jodconverter.documentformatregistry; import com.artofsolving.jodconverter.openoffice.connection.openofficeconnection; import com.artofsolving.jodconverter.openoffice.connection.socketopenofficeconnection; import com.artofsolving.jodconverter.openoffice.converter.openofficedocumentconverter; import com.artofsolving.jodconverter.openoffice.converter.streamopenofficedocumentconverter; import org.apache.pdfbox.pdmodel.pddocument; import org.apache.pdfbox.pdmodel.pdpage; import org.apache.pdfbox.pdmodel.pdpagetree; import org.apache.pdfbox.rendering.imagetype; import org.apache.pdfbox.rendering.pdfrenderer; import org.apache.pdfbox.tools.imageio.imageioutil; import java.awt.image.bufferedimage; import java.awt.image.renderedimage; import java.io.file; import java.io.ioexception; import java.util.arraylist; import java.util.list; import javax.imageio.imageio; /** * * @author josnow * @date 2017年5月9日 下午12:38:39 * @version 1.0.0 * @desc openoffice转换工具 */ public class openofficeutils { public static final string local_host = "localhost"; public static final int local_port = 8100; // format public static documentformatregistry formatfactory = new defaultdocumentformatregistry(); /** * * @desc * @auth josnow * @date 2017年6月9日 下午4:11:04 * @param inputfilepath * 待转换的文件路径 * @param outputfilepath * 输出文件路径 */ public static void convert(string inputfilepath, string outputfilepath) throws connectexception { convert(inputfilepath, outputfilepath, local_host, local_port); } /** * * @desc * @auth josnow * @date 2017年6月9日 下午4:12:29 * @param inputfilepath * 待转换的文件路径 * @param outputfilepath * 输出文件路径 * @param connectip * 远程调用ip * @param connectport * 远程调用端口 */ public static void convert(string inputfilepath, string outputfilepath, string connectip, int connectport) throws connectexception { if (stringutils.isempty(inputfilepath) || stringutils.isempty(outputfilepath) || stringutils.isempty(connectip)) { throw new illegalargumentexception("参数异常!!"); } openofficeconnection connection = new socketopenofficeconnection(connectip, connectport); connection.connect(); // todo excel转成pdf默认是a4纸 // documentconverter converter = getconverter(connectip, connection); // converter.convert(new file(inputfilepath), new file(outputfilepath)); // todo excel转成pdf默认是a4纸, 如果现实折行,则自定义converterdocument,改变纸张大小 converterdocument converterdocument = new converterdocument(connection); converterdocument.convert(new file(inputfilepath), new file(outputfilepath)); connection.disconnect(); } /** * * @desc * @auth josnow * @date 2017年6月9日 下午4:08:26 * @param inputstream * @param inputfileextension * 待转换文件的扩展名,例如: xls,doc * @param outputstream * @param outputfileextension * 输出文件扩展名,例如:pdf */ public static void convert(inputstream inputstream, string inputfileextension, outputstream outputstream, string outputfileextension) throws connectexception { convert(inputstream, inputfileextension, outputstream, outputfileextension, local_host, local_port); } /** * * @desc * @auth josnow * @date 2017年6月9日 下午4:10:21 * @param inputstream * @param inputfileextension * 待转换文件的扩展名,例如: xls,doc * @param outputstream * @param outputfileextension * 输出文件扩展名,例如:pdf * @param connectip * 远程调用ip * @param connectport * 远程调用端口 */ public static void convert(inputstream inputstream, string inputfileextension, outputstream outputstream, string outputfileextension, string connectip, int connectport) throws connectexception { if (inputstream == null || stringutils.isempty(inputfileextension) || outputstream == null || stringutils.isempty(outputfileextension) || stringutils.isempty(connectip)) { throw new illegalargumentexception("参数异常!!"); } openofficeconnection connection = new socketopenofficeconnection(connectip, connectport); connection.connect(); documentconverter converter = getconverter(connectip, connection); converter.convert(inputstream, formatfactory.getformatbyfileextension(inputfileextension), outputstream, formatfactory.getformatbyfileextension(outputfileextension)); connection.disconnect(); } private static documentconverter getconverter(string connectip, openofficeconnection connection) { documentconverter converter = "localhost".equals(connectip) || "127.0.0.1".equals(connectip) || "0:0:0:0:0:0:0:1".equals(connectip) ? new openofficedocumentconverter(connection) : new streamopenofficedocumentconverter(connection); return converter; } public static void main(string[] args) throws exception { openofficeutils.convert("d:\\test.xls", "d:\\test.pdf"); pdf2image(new file("d:\\test.pdf")); } /** * pdf to big image * github: https://github.com/mmtou/pdf2img * @param pdffile pdf文件路径 * @return big image file * @throws exception */ public static file pdf2image(file pdffile) throws exception { // 拼成图片后的宽度和高度 int w = 0; int h = 0; list<bufferedimage> images = new arraylist(); // 生成图片后的路径 string path = pdffile.getparent() + file.separator; string filename = pdffile.getname().replace(".pdf", ""); file destinationfile = new file(path); if (!destinationfile.exists()) { destinationfile.mkdir(); } pddocument document = pddocument.load(pdffile); pdpagetree list = document.getdocumentcatalog().getpages(); int pagecounter = 0; for (pdpage page : list) { pdfrenderer pdfrenderer = new pdfrenderer(document); bufferedimage image = pdfrenderer.renderimagewithdpi(pagecounter, 100, imagetype.rgb); string target = path + filename + "-" + (pagecounter++) + ".png"; imageioutil.writeimage(image, target, 100); w = image.getwidth(); h += image.getheight(); images.add(image); new file(target).delete(); } document.close(); bufferedimage combined = new bufferedimage(w, h, bufferedimage.type_int_argb); graphics g = combined.getgraphics(); int y = 0; for (bufferedimage image : images) { g.drawimage(image, 0, y, null); y += image.getheight(); } // save as new image file image = new file(path, filename + ".png"); imageio.write(combined, "png", image); return image; } }
自定义 converterdocument
import com.artofsolving.jodconverter.openoffice.connection.openofficeconnection; import com.artofsolving.jodconverter.openoffice.converter.openofficedocumentconverter; import com.artofsolving.jodconverter.openoffice.converter.streamopenofficedocumentconverter; import com.sun.star.awt.size; import com.sun.star.beans.propertyvalue; import com.sun.star.lang.xcomponent; import com.sun.star.uno.unoruntime; import com.sun.star.view.paperformat; import com.sun.star.view.xprintable; public class converterdocument extends streamopenofficedocumentconverter { public converterdocument(openofficeconnection connection) { super(connection); } public final static size a5, a4, a3; public final static size b4, b5, b6; public final static size kaoqinreport; static { a5 = new size(14800, 21000); a4 = new size(21000, 29700); a3 = new size(29700, 42000); b4 = new size(25000, 35300); b5 = new size(17600, 25000); b6 = new size(12500, 17600); // kaoqinreport = new size(29700, 27940); //最大限度 宽 1600000 kaoqinreport = new size(25000, 21000);; } @override protected void refreshdocument(xcomponent document) { super.refreshdocument(document); // the default paper format and orientation is a4 and portrait. to // change paper orientation // re set page size xprintable xprintable = (xprintable) unoruntime.queryinterface(xprintable.class, document); propertyvalue[] printerdesc = new propertyvalue[2]; // paper orientation // printerdesc[0] = new propertyvalue(); // printerdesc[0].name = "paperorientation"; // printerdesc[0].value = paperorientation.portrait; // paper format printerdesc[0] = new propertyvalue(); printerdesc[0].name = "paperformat"; printerdesc[0].value = paperformat.user; // paper size printerdesc[1] = new propertyvalue(); printerdesc[1].name = "papersize"; printerdesc[1].value = kaoqinreport; try { xprintable.setprinter(printerdesc); } catch (exception e) { e.printstacktrace(); } } }
使用:
public static void main(string[] args) throws exception { openofficeutils.convert("d:\\test.xls", "d:\\test.pdf"); pdf2image(new file("d:\\test.pdf")); }
参考: https://blog.csdn.net/liuhualiang/article/details/14094019?utm_source=blogxgwz6
到此这篇关于java中使用openoffice将excel转pdf再转图片功能实现的文章就介绍到这了,更多相关java openoffice excel转pdf内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 钉钉如何远程打卡签到 钉钉远程签到方法
下一篇: Python定时爬取微博热搜示例介绍