Java中excel转换为jpg/png图片 采用aspose-cells-18.6.jar
一 Java中excel转换为jpg/png图片
package com.thinkgem.jeesite.modules.task.util; import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOrPrintOptions;
import com.aspose.cells.SheetRender;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import java.io.File; public class ConvertToImage { public static void ConvertToImage (){ String dataDir = getDataDir(ConvertToImage.class);
// Create a new Workbook object
// Open a template excel file
Workbook book = null;
try {
//book = new Workbook(dataDir + "2018各项目情况.xlsx");
book = new Workbook("D:\\20180702_Game10002_DataReport.xls");
// Get the first worksheet
//Worksheet sheet = book.getWorksheets().get(0);
Worksheet sheet = book.getWorksheets().get(0);
sheet.getPageSetup().setLeftMargin(-20);
sheet.getPageSetup().setRightMargin(0);
sheet.getPageSetup().setBottomMargin(0);
sheet.getPageSetup().setTopMargin(0); // Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Specify the image format
imgOptions.setImageFormat(ImageFormat.getJpeg());
imgOptions.setCellAutoFit(true);
imgOptions.setOnePagePerSheet(true);
//imgOptions.setDesiredSize(1000,800);
// Render the sheet with respect to specified image/print options
SheetRender render = new SheetRender(sheet, imgOptions); // Render the image for the sheet
//render.toImage(0, dataDir + "SheetImage.jpg");
render.toImage(0, "D:\\SheetImage.jpg");
} catch (Exception e) {
e.printStackTrace();
} } /**
* @param filepath .xls或者.xlsx文件的路径
* @parampicpath jpg或者png图片的路径
*/
public static void ConvertToImage (String filepath ,String picpath){ String dataDir = getDataDir(ConvertToImage.class);
// Create a new Workbook object
// Open a template excel file
Workbook book = null;
try {
//book = new Workbook(dataDir + "2018各项目情况.xlsx");
book = new Workbook(filepath);
// Get the first worksheet
//Worksheet sheet = book.getWorksheets().get(0);
Worksheet sheet = book.getWorksheets().get(0); // Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Specify the image format
imgOptions.setImageFormat(ImageFormat.getJpeg());
imgOptions.setCellAutoFit(true);
imgOptions.setOnePagePerSheet(true);
imgOptions.setDefaultFont("200");
// Render the sheet with respect to specified image/print options
SheetRender render = new SheetRender(sheet, imgOptions); // Render the image for the sheet
//render.toImage(0, dataDir + "SheetImage.jpg");
render.toImage(0, picpath);
} catch (Exception e) {
e.printStackTrace();
} } public static String getDataDir(Class c) {
File dir = new File(System.getProperty("user.dir")); System.out.println("shake" + dir.getAbsolutePath()); dir = new File(dir, "src");
dir = new File(dir, "main");
dir = new File(dir, "resources"); for (String s : c.getName().split("\\.")) {
dir = new File(dir, s);
} if (dir.exists()) {
System.out.println("Using data directory: " + dir.toString());
} else {
dir.mkdirs();
System.out.println("Creating data directory: " + dir.toString());
} return dir.toString() + File.separator;
} public static void main (String[] args ){
ConvertToImage();
}
}
二 linux系统中采用aspose-cells-18.6.jar包生成图片中文乱码
具体解决方案:参考帖子
在Windows中一切良好,中英文都能很好的展示,但是在我将程序部署到Linux中后,却出现生成的图片中中文全部乱码(显示成一个个的方框)。

1)查看服务器字体列表
[root@iZ2zez5rp1bmsZ share]# cd fc-list
bash: cd: fc-list: No such file or directory
悲哀,连字体库都没有![]()
2)安装字体库
# 先安装fontconfig,用fontconfig来安装字体库
[root@iZ2zebjvdi1bmsZ share]# yum -y install fontconfig
Loaded plugins: fastestmirror
...
Installed:
fontconfig.x86_64 0:2.10.95-10.el7
Dependency Installed:
fontpackages-filesystem.noarch 0:1.44-8.el7
Complete!
fontconfig安装成功后在/usr/share目录中就会看到fonts和fontconfig两个目录(没装fontconfig之前是没有这两个目录的)

接下来就可以添加字体库了
3)添加字体
添加字体之前需要先下载相应的字体文件,博主用的是simsun.ttf(宋体)字体库,可以直接点击下载:下载simsun.zip
当然也可以去windows系统下的C:/windows/fonts目录下寻找合适的字体
字体文件准备好后,下边就正式开发操作
首先在/usr/share/fonts/目录下创建目录(名称随意)
mkdir chinese
然后将上方提供的
zip包中的两个文件全部解压并放到新建的目录(chinese)中,
然后修改chinese目录的权限
chmod -R 755 /usr/share/fonts/chinese
接下来需要安装ttmkfdir,这个命令的作用是搜索目录中所有的字体信息,汇总生成fonts.scale文件。
[root@iZ2zebjvditp1bmsZ fonts]# yum -y install ttmkfdir
Loaded plugins: fastestmirror
...
Installed:
ttmkfdir.x86_64 0:3.0.9-42.el7
Complete!
然后执行ttmkfdir命令:
[root@iZ2zz5rp1bmsZ fonts]# ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
最后修改字体配置文件
vi /etc/fonts/fonts.conf
如下添加配置

<dir>/usr/share/fonts/chinese</dir>
最后保存文件并执行fc-cache进行刷新字体缓存
OK,到此字体就安装完成,在看一下字体列表:
[root@iZ2zebrp1bmsZ fonts]# fc-list
/usr/share/fonts/chinese/simsun.ttc: SimSun\-PUA,宋体\-PUA:style=Regular
/usr/share/fonts/chinese/simsun.ttc: NSimSun,新宋体:style=Regular
/usr/share/fonts/chinese/simsun.ttf: SimSun,宋体:style=Regular
/usr/share/fonts/chinese/simsun.ttc: SimSun,宋体:style=Regular
4)重新测试生成文件

三 官网下载地址
https://downloads.aspose.com/cells/java
Java中excel转换为jpg/png图片 采用aspose-cells-18.6.jar的更多相关文章
- 将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)
方法一:使用Arrays.asList()方法 1 2 String[] asset = {"equity", "stocks", "gold&q ...
- java中excel导入\导出工具类
1.导入工具 package com.linrain.jcs.test; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import ...
- Java中List转换为数组,数组转List
今天写代码遇到一个奇怪的问题,具体代码不贴出了,写一个简化的版本.如下: ArrayList<String> list=new ArrayList<String>(); ...
- Java 中数组转换为 List
目录 1 - int 型数组转换为 List 2 - List 转换为 int 型数组 3 - String 型数组转换为 List 4 - List 转换为 String 型数组 版权声明 开发中经 ...
- C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll]
[csharp] view plain copy 1.添加引用: Aspose.Cells.dll(我们就叫工具包吧,可以从网上下载.关于它的操作我在“Aspose.Cells操作说明 中文版 下载 ...
- java中getBytes方法可能使图片文件产生的问题
InputStream is = new FileInputStream(fl); ImageInputStream iis = ImageIO.createImageInputStream(is); ...
- Java中Excel导入功能实现、excel导入公共方法_POI -
这是一个思路希望能帮助到大家:如果大家有更好的解决方法希望分享出来 公司导入是这样做的 每个到导入的地方 @Override public List<DataImportMessage> ...
- 关于Java中excel表格导出的总结(Java程序导出模板和Java根据模板导出表格两种实现方式)
导出excel通用模板(程序定义模板导出) 转载原文:https://www.jianshu.com/p/5c7b359a159c 如下代码,本方法主要用于程序定义模板格式,并导出文件.该方法将定义和 ...
- java 中Excel的导入导出
部分转发原作者https://www.cnblogs.com/qdhxhz/p/8137282.html雨点的名字 的内容 java代码中的导入导出 首先在d盘创建一个xlsx文件,然后再进行一系列 ...
随机推荐
- IE内核浏览器的404页面问题和IE自动缓存引发的问题
本站404页面被IE替换成IE自己的404页面 在权限设置正确的情况下,自定义的404页面文件大小如果小于512字节,那么IE内核的浏览器会认为你自定义的404页面不够权威,从而使用其自带的404页面 ...
- 浅析js中2个等号与3个等号的区别(转)
首先,== equality 等同,=== identity 恒等. ==, 两边值类型不同的时候,要先进行类型转换,再比较. ===,不做类型转换,类型不同的一定不等. 下面分别说明: 先说 === ...
- flask内容
Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后 ...
- pygame-KidsCanCode系列jumpy-part11-角色动画(下)
接上节继续,上节并没有处理向左走.向右走的动画效果,这节补上,看似很简单,但是有一些细节还是要注意: def jump(self): hits = pg.sprite.spritecollide(se ...
- arcgis 获得工具箱工具的个数
import arcgisscripting import string; gp = arcgisscripting.create(9.3); ##多少个工具箱 toolboxes = gp.list ...
- lua的性能优化
Roberto Ierusalimschy写过经典的Lua 性能提示的文章,链接地址>> 我通过实际的代码来验证,发现一个问题.当我使用 LuaStudio 运行时,发现结果反而与提示相反 ...
- dlib的编译和安装
之前写过python dlib依赖的安装,安装过程还算比较复杂,还需要安装boost.Python依赖等,但是如果纯粹的编译C++的dlib库,则要简单得多,基本上不需要其他外部的依赖,这里简单叙述一 ...
- bcrypt 加密
关于 bcrypt:1.bcrypt是不可逆的加密算法,无法通过解密密文得到明文.2.bcrypt和其他对称或非对称加密方式不同的是,不是直接解密得到明文,也不是二次加密比较密文,而是把明文和存储的密 ...
- CSS3制作图形大全——碉堡了
为方便观看效果图,请移步原文:https://www.jqhtml.com/8045.html Square #square { width: 100px; height: 100 ...
- 关于docker 意外停止,重新快速启动措施
1. 我们要重启这个镜像,需要知道这个镜像ID,类似这个: 7079ff99e10ac326726a364348853c0e508cad8ce00ae970f3c800f172a40252 那么你可以 ...