In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to export pictures through java highcharts to excel", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to export pictures to excel through java highcharts" this article.
1. Purpose
The highcharts chart of the front-end page is exported to the generated excel file through the java background implementation. Used in the report page export class function.
two。 Description
The front-end page passes the svg information string of the chart as a parameter. The background uses the batik toolkit to generate pictures and manipulate the excel using the poi toolkit.
3. Use the jar package
1.batik-all-1.7.jar
2.poi-3.12.jar
3.commons-codec-1.12.jar
No error will be reported if it is not imported, but an exception occurs when it is used. Prompt message: 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. Realize
The first part realizes the svg to generate pictures to the disk; the second part reads the pictures on the disk to the created excel;. The third part reads and outputs the svg information directly to the excel, which is the combination of the above two, omitting the process of saving to the disk.
4.1Exporting svg to pictures
Code:
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: convert svg to png format image * / public class SvgPngConverter {/ * @ Description: convert svg string to png*@Author:*@param svgCode svg code * @ param pngFilePath saved path * @ throws IOException io exception * @ throws TranscoderException svg code exception * / 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: convert svgCode into a png file and output it directly to the stream * @ param svgCode svg code * @ param outputStream output stream * @ throws TranscoderException exception * @ throws IOException io exception * / 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 string of the chart, which is generally long. String path = "H:\\ svg1.png" is omitted here; try {convertToPng (svgStr, path);} catch (IOException e) {e.printStackTrace ();} catch (TranscoderException e) {e.printStackTrace ();}
Effect:
Picture content:
4.2 Reading pictures and outputting them to excel
Code:
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: read pictures from disk to excel*/public class ExcelImageTest {public static void main (String [] args) {FileOutputStream fileOut = null;BufferedImage bufferImg = null;// first put the read images into a ByteArrayOutputStream to generate 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") / / for the top-level manager of drawing, only one sheet can be obtained (be sure to note this) HSSFPatriarch patriarch = sheet1.createDrawingPatriarch (); / / anchor is mainly used to set the properties HSSFClientAnchor anchor = new HSSFClientAnchor (0,0,255,255, (short) 1,1, (short) 7,10); anchor.setAnchorType (3); / insert patriarch.createPicture (anchor, wb.addPicture (byteArrayOut.toByteArray (), HSSFWorkbook.PICTURE_TYPE_JPEG)) FileOut = new FileOutputStream ("wb.write / test Excel.xls"); / / write to excel file wb.write (fileOut); System.out.println ("- Excle file generated -");} catch (Exception e) {e.printStackTrace ();} finally {if (fileOut! = null) {try {fileOut.close ();} catch (IOException e) {e.printStackTrace ();}
Description:
In the method, HSSFClientAnchor anchor = new HSSFClientAnchor (0,0,255,255, (short) 1,1, (short) 7,10) indicates where you want to insert the picture into the excel.
The method is public HSSFClientAnchor (int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2) {}
The parameters are as follows:
Int row 1: the number of cell rows in the upper left corner of the picture (value is 1, that is, 2 rows); short col1: the number of cell columns in the upper left corner of the picture (value 1, that is, column B); int row2: the number of cell rows in the lower right corner of the picture (value 10, that is, 11 rows); short col2: the number of cell columns in the lower right corner of the picture (value 7, that is, column H); int dx1: the distance between the upper left corner of the picture and the upper left corner of the cell Int dy1: the distance from the upper-left corner of the picture to the upper-left corner of the cell; int dx2: the row distance from the lower-right corner of the picture from the upper-left corner of the cell; int dy2: the distance from the lower-right corner of the picture from the upper-left corner of the cell
4.3 Export the chart directly to excel
Code:
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: directly add svg information to excel*/public class SvgToExcelPng {public static void main (String [] args) {FileOutputStream fileOut;try {String svgStr = ""; / / the svg string of the chart, which is generally long, omitting 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"); / / A top-level manager for drawing, and a sheet can only get one HSSFPatriarch patriarch = sheet1.createDrawingPatriarch (); / / anchor is mainly used to set the properties HSSFClientAnchor anchor = new HSSFClientAnchor (0,0,255,255, (short) 1,1, (short) 7,10); anchor.setAnchorType (3) / insert patriarch.createPicture (anchor, wb.addPicture (byteArrayOut.toByteArray (), HSSFWorkbook.PICTURE_TYPE_JPEG)); fileOut = new FileOutputStream ("wb.write / test Excel2.xls"); / / write excel file wb.write (fileOut); System.out.println ("- Excle file has been generated -");} catch (Exception e) {e.printStackTrace ();}
The above is all the contents of the article "how to export pictures to excel through highcharts through java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.