java中excel转jpg_Java中excel转换为jpg/png图片 采用aspose-cells-18.6.jar

一  Java中excel转换为jpg/png图片

packagecom.thinkgem.jeesite.modules.task.util;importcom.aspose.cells.ImageFormat;importcom.aspose.cells.ImageOrPrintOptions;importcom.aspose.cells.SheetRender;importcom.aspose.cells.Workbook;importcom.aspose.cells.Worksheet;importjava.io.File;public classConvertToImage {public static voidConvertToImage (){

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 = newImageOrPrintOptions();//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 = newSheetRender(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();

}

}

/**

* @paramfilepath .xls或者.xlsx文件的路径* @parampicpath jpg或者png图片的路径*/public static voidConvertToImage (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 = newWorkbook(filepath);//Get the first worksheet//Worksheet sheet = book.getWorksheets().get(0);

Worksheet sheet = book.getWorksheets().get(0);//Define ImageOrPrintOptions

ImageOrPrintOptions imgOptions = newImageOrPrintOptions();//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 = newSheetRender(sheet, imgOptions);//Render the image for the sheet//render.toImage(0, dataDir + "SheetImage.jpg");

render.toImage(0, picpath);

}catch(Exception e) {

e.printStackTrace();

}

}public staticString 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= newFile(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 voidmain (String[] args ){

ConvertToImage();

}

}

二 linux系统中采用aspose-cells-18.6.jar包生成图片中文乱码

具体解决方案:参考帖子

在Windows中一切良好,中英文都能很好的展示,但是在我将程序部署到Linux中后,却出现生成的图片中中文全部乱码(显示成一个个的方框)。

20170501135716543.png-flyat

1)查看服务器字体列表

[root@iZ2zez5rp1bmsZ share]# cd fc-list

bash: cd: fc-list: No such file or directory

悲哀,连字体库都没有

09f70ac89fa21edc118fa1a7daf466be.gif

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之前是没有这两个目录的)

20170502141501411.png-flyat

接下来就可以添加字体库了

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

如下添加配置

20170502142922835.png-flyat

最后保存文件并执行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)重新测试生成文件

20170502143344236.png-flyat

三  官网下载地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值