当前位置: 移动技术网 > IT编程>开发语言>Java > 如何通过java实现highcharts导出图片至excel

如何通过java实现highcharts导出图片至excel

2019年07月19日  | 移动技术网IT编程  | 我要评论

1. 目的

通过java后台实现将前端页面的highcharts图表导出至生成的excel文件中。使用于报表页面导出类功能。

2. 说明

前端页面将图表的svg信息字符串作为参数传递
后台使用batik工具包生成图片
使用poi工具包操作excel

3. 使用jar包

1.batik-all-1.7.jar

2.poi-3.12.jar

3.commons-codec-1.12.jar

不导入不会报错,但使用时异常,提示信息: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. 实现

第一部分实现svg生成图片到磁盘;
第二部分实现将磁盘上的图片读取到创建的excel;
第三部分直接将svg信息读取并输出到excel,是上面二者的合并,省略了存至磁盘的过程。

4.1 将svg导出图片

代码:

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转换为png格式的图片
*/
public class svgpngconverter {
/**
*@description: 将svg字符串转换为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转换成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字符串,一般较长,此处省略
string path = "h:\\svg1.png";
try {
converttopng(svgstr, path);
} catch (ioexception e) {
e.printstacktrace();
} catch (transcoderexception e) {
e.printstacktrace();
}
}
}

效果:

图片内容:

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;
//先把读进来的图片放到一个bytearrayoutputstream中,以便产生bytearray
try {
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");
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
hssfpatriarch patriarch = sheet1.createdrawingpatriarch();
//anchor主要用于设置图片的属性
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:/测试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();
}
}
}
}
}

效果:

内容:

说明:

方法中hssfclientanchor anchor = new hssfclientanchor(0, 0, 255, 255,(short) 1, 1, (short) 7, 10)表示要将图片插入至excel时的位置。

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

参数如下:

int row 1:图片左上角所在单元格行数(值为1,即2行);
short col1:图片左上角所在单元格列数(值为1,即b列);
int row2:图片右下角所在单元格行数(值为10,即11行);
short col2:图片右下角所在单元格列数(值为7,即h列);
int dx1:图片左上角距(b,2)单元格左上角行向距离;
int dy1:图片左上角距(b,2)单元格左上角列向距离;
int dx2:图片右下角距(h,11)单元格左上角的行向距离;
int dy2:图片右下角距(h,11)单元格左上角的列向距离;

4.3 直接将图表导出至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信息转为图片添加至excel
*/
public class svgtoexcelpng {
public static void main(string[] args) {
fileoutputstream fileout;
try {
string svgstr = ""; // 图表的svg字符串,一般较长,此处省略
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");
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
hssfpatriarch patriarch = sheet1.createdrawingpatriarch();
//anchor主要用于设置图片的属性
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:/测试excel2.xls");
// 写入excel文件
wb.write(fileout);
system.out.println("----excle文件已生成------");
} catch (exception e) {
e.printstacktrace();
}
}
}

效果同上。

5. 结语

实现将highcharts图表导出至excel,总体思想为使用svg字符串信息生成输入流,使用工具转出输出流至对应地址。

ps:上述实现中,由于svg字符串一般较长,考虑可读性将其省略,读者可自行百度或至highcharts官网下载图片示例复制粘贴到代码相应位置测试。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网