溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么通過(guò)java實(shí)現(xiàn)highcharts導(dǎo)出圖片至excel

發(fā)布時(shí)間:2021-09-26 16:30:08 來(lái)源:億速云 閱讀:179 作者:小新 欄目:編程語(yǔ)言

這篇文章主要為大家展示了“怎么通過(guò)java實(shí)現(xiàn)highcharts導(dǎo)出圖片至excel”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“怎么通過(guò)java實(shí)現(xiàn)highcharts導(dǎo)出圖片至excel”這篇文章吧。

1. 目的

通過(guò)java后臺(tái)實(shí)現(xiàn)將前端頁(yè)面的highcharts圖表導(dǎo)出至生成的excel文件中。使用于報(bào)表頁(yè)面導(dǎo)出類功能。

2. 說(shuō)明

前端頁(yè)面將圖表的svg信息字符串作為參數(shù)傳遞后臺(tái)使用batik工具包生成圖片使用poi工具包操作excel

3. 使用jar包

1.batik-all-1.7.jar

2.poi-3.12.jar

3.commons-codec-1.12.jar

不導(dǎo)入不會(huì)報(bào)錯(cuò),但使用時(shí)異常,提示信息:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/digest/DigestUtils at org.apache.poi.hssf.usermodel.HSSFWorkbook.addPicture(HSSFWorkbook.java:1575)

4. 實(shí)現(xiàn)

第一部分實(shí)現(xiàn)svg生成圖片到磁盤;第二部分實(shí)現(xiàn)將磁盤上的圖片讀取到創(chuàng)建的excel;第三部分直接將svg信息讀取并輸出到excel,是上面二者的合并,省略了存至磁盤的過(guò)程。

4.1 將svg導(dǎo)出圖片

代碼:

import org.apache.batik.transcoder.TranscoderException;import org.apache.batik.transcoder.TranscoderInput;import org.apache.batik.transcoder.TranscoderOutput;import org.apache.batik.transcoder.image.PNGTranscoder;import java.io.*;/***@Description: 將svg轉(zhuǎn)換為png格式的圖片*/public class SvgPngConverter {/***@Description: 將svg字符串轉(zhuǎn)換為png*@Author:*@param svgCode svg代碼*@param pngFilePath 保存的路徑*@throws IOException io異常*@throws TranscoderException svg代碼異常*/public static void convertToPng(String svgCode,String pngFilePath) throws IOException,TranscoderException {File file = new File (pngFilePath);FileOutputStream outputStream = null;try {file.createNewFile ();outputStream = new FileOutputStream (file);convertToPng (svgCode, outputStream);} finally {if (outputStream != null) {try {outputStream.close ();} catch (IOException e) {e.printStackTrace ();}}}}/***@Description: 將svgCode轉(zhuǎn)換成png文件,直接輸出到流中*@param svgCode svg代碼*@param outputStream 輸出流*@throws TranscoderException 異常*@throws IOException io異常*/public static void convertToPng(String svgCode,OutputStream outputStream) throws TranscoderException,IOException{try {byte[] bytes = svgCode.getBytes ("UTF-8");PNGTranscoder t = new PNGTranscoder ();ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);TranscoderInput input = new TranscoderInput (byteArrayInputStream);TranscoderOutput output = new TranscoderOutput (outputStream);t.transcode (input, output);outputStream.flush ();} finally {if (outputStream != null) {try {outputStream.close ();} catch (IOException e) {e.printStackTrace ();}}}}public static void main(String[] args) {StringBuilder svgStr = ""; // 圖表的svg字符串,一般較長(zhǎng),此處省略String path = "H:\\svg1.png";try {convertToPng(svgStr, path);} catch (IOException e) {e.printStackTrace();} catch (TranscoderException e) {e.printStackTrace();}}}

效果:

圖片內(nèi)容:

4.2 讀取圖片輸出至excel

代碼:

import org.apache.poi.hssf.usermodel.HSSFClientAnchor;import org.apache.poi.hssf.usermodel.HSSFPatriarch;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;/***@Description: 讀取磁盤中圖片到excel*/public class ExcelImageTest {public static void main(String[] args) {FileOutputStream fileOut = null;BufferedImage bufferImg = null;//先把讀進(jìn)來(lái)的圖片放到一個(gè)ByteArrayOutputStream中,以便產(chǎn)生ByteArraytry {ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();bufferImg = ImageIO.read(new File("H:/svg1.png"));ImageIO.write(bufferImg, "png", byteArrayOut);HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet1 = wb.createSheet("test picture");//畫圖的頂級(jí)管理器,一個(gè)sheet只能獲取一個(gè)(一定要注意這點(diǎn))HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();//anchor主要用于設(shè)置圖片的屬性HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 7, 10);anchor.setAnchorType(3);//插入圖片patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));fileOut = new FileOutputStream("H:/測(cè)試Excel.xls");// 寫入excel文件wb.write(fileOut);System.out.println("----Excle文件已生成------");} catch (Exception e) {e.printStackTrace();}finally{if(fileOut != null){try {fileOut.close();} catch (IOException e) {e.printStackTrace();}}}}}

說(shuō)明:

方法中HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 7, 10)表示要將圖片插入至excel時(shí)的位置。

方法為:public HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2) {},

參數(shù)如下:

int row 1:圖片左上角所在單元格行數(shù)(值為1,即2行);short col1:圖片左上角所在單元格列數(shù)(值為1,即B列);int row2:圖片右下角所在單元格行數(shù)(值為10,即11行);short col2:圖片右下角所在單元格列數(shù)(值為7,即H列);int dx1:圖片左上角距(B,2)單元格左上角行向距離;int dy1:圖片左上角距(B,2)單元格左上角列向距離;int dx2:圖片右下角距(H,11)單元格左上角的行向距離;int dy2:圖片右下角距(H,11)單元格左上角的列向距離;

4.3 直接將圖表導(dǎo)出至excel

代碼:

import org.apache.batik.transcoder.TranscoderInput;import org.apache.batik.transcoder.TranscoderOutput;import org.apache.batik.transcoder.image.PNGTranscoder;import org.apache.poi.hssf.usermodel.HSSFClientAnchor;import org.apache.poi.hssf.usermodel.HSSFPatriarch;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import java.io.*;/***@Description: 直接將svg信息轉(zhuǎn)為圖片添加至excel*/public class SvgToExcelPng {public static void main(String[] args) {FileOutputStream fileOut;try {String svgStr = ""; // 圖表的svg字符串,一般較長(zhǎng),此處省略byte[] bytes = svgStr.getBytes ("UTF-8");ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();PNGTranscoder t = new PNGTranscoder ();TranscoderInput input = new TranscoderInput (new ByteArrayInputStream(bytes));TranscoderOutput output = new TranscoderOutput (byteArrayOut);t.transcode (input, output);HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet1 = wb.createSheet("test picture");//畫圖的頂級(jí)管理器,一個(gè)sheet只能獲取一個(gè)(一定要注意這點(diǎn))HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();//anchor主要用于設(shè)置圖片的屬性HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 7, 10);anchor.setAnchorType(3);//插入圖片patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));fileOut = new FileOutputStream("H:/測(cè)試Excel2.xls");// 寫入excel文件wb.write(fileOut);System.out.println("----Excle文件已生成------");} catch (Exception e) {e.printStackTrace();}}}

以上是“怎么通過(guò)java實(shí)現(xiàn)highcharts導(dǎo)出圖片至excel”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI