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文件的路徑
* @param
picpath 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中后,卻出現生成的圖片中中文全部亂碼(顯示成一個個的方框)。

Linux中ImageIO生成圖片中文亂碼文章的圖片

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之前是沒有這兩個目錄的

Linux中ImageIO生成圖片中文亂碼文章的圖片

接下來就可以添加字體庫了

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

如下添加配置

Linux中ImageIO生成圖片中文亂碼文章的圖片

<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)重新測試生成文件

Linux中ImageIO生成圖片中文亂碼文章的圖片

 

三  官網下載地址

https://downloads.aspose.com/cells/java


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM