一 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中后,却出现生成的图片中中文全部乱码(显示成一个个的方框)。
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
如下添加配置
最后保存文件并执行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)重新测试生成文件
三 官网下载地址