hcharts svg 导入到excel生成图片

Deaver_gwp 2018-05-27 01:13:19

$(function () {
var charts = Highcharts.chart('conter',{
chart: {
zoomType: 'xy'
},
credits: {
enabled: false
},
exporting: {
enabled:false
},
title: {
text: 'sasdasda'
},
subtitle: {
text: 'ddddddddd'
},
xAxis: [{
categories: <%=rq%>
}],
yAxis: [{ // Primary yAxis
title: {
text: '项数',
style: {
color: '#666666'
}
},
labels: {
format: '{value}项',
style: {
color: '#666666'
}
},
opposite: true
}, { // Secondary yAxis
title: {
text: '风险值',
style: {
color: '#666666'
}
},
labels: {
format: '{value}',
style: {
color: '#666666'
}
}
}],
tooltip: {
shared: true
},
plotOptions: {
column: {
dataLabels: {
enabled: true,
allowOverlap: true
}
}
},
series: [{
colorByPoint: true,
name: '风险值',
type: 'column',
yAxis: 1,
data: <%=mtfxz%>,
zones: [{
value: 150,
color: '#3CB371'
}, {
value: 300,
color: '#DAA520'
},{
color: '#DC143C'
}
],
dataLabels: {
enabled: true,
allowOverlap:true,
rotation: -90,
color: '#FFFFFF',
align: 'right',
format: '{point.y:.1f}', // :.1f 为保留 1 位小数
y: 15
}
}, {
name: '项数',
type: 'spline',
color: '#00BFFF',
data: <%=mtfxxs%>,
tooltip: {
valueSuffix: '次'
},
dataLabels: {
enabled: true,
allowOverlap:true,
rotation: -90,
color: '#666666',
format: '{y}',
y: -15
}
}]
});
var svg_column = charts.getSVG();
$.post("downExpExcelAction.jsp",{"svg_column":svg_column},function(){});
});


request.setCharacterEncoding("utf-8");
String svg_column= request.getParameter("svg_column");
if(svg_column!=null && !svg_column.equals("")){
System.out.println("****************"+svg_column);
ExportFXData.Export(request,svg_column);
}
public static String Export(HttpServletRequest request,String svg_column){
String serverRealPath = request.getSession().getServletContext().getRealPath("/");
String excelPath = serverRealPath+ "Mis/fxgl/fxztj/DCFXZXS.xls";
try {
PoiExcel excel = new PoiExcel(excelPath);
setDatas(excel, 0,request);
insertPicture(excel, 0,excelPath,svg_column, request);
return saveExcel(excel, request);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void insertPicture(PoiExcel excel,int sheetNum,String excelPath,String svg_column,HttpServletRequest request){
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
try {
Sheet sheet1 = excel.getSheet(sheetNum);
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = (HSSFPatriarch) sheet1.createDrawingPatriarch();
String savePathImg = "temp"+ (int) (Math.random() * 100000) +".jpg";
convertToPng(svg_column,savePathImg);
Image src = Toolkit.getDefaultToolkit().getImage(savePathImg);
bufferImg = toBufferedImage(src);
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(bufferImg, "jpg", byteArrayOut);
//anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,255,255,(short)10,1,(short)28,24);
anchor.setAnchorType(0);
//插入图片
patriarch.createPicture(anchor, excel.getWorkbook().addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

fileOut = new FileOutputStream(excelPath);
// 写入excel文件
excel.getWorkbook().write(fileOut);
System.out.println("----Excle文件已生成------");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fileOut != null){
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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();
JPEGTranscoder t = new JPEGTranscoder ();
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
TranscoderOutput output = new TranscoderOutput (outputStream);
t.transcode(input, output);
outputStream.flush ();
} finally {
if (outputStream != null) {
try {
outputStream.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
}
}

在 t.transcode(input, output); 提示转化失败!
...全文
1512 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dianbolan 2018-06-01
  • 打赏
  • 举报
回复
我觉的可能有两种可能 一个是项目里防xss注入的把 svg的xml破坏了 就是把那些 <,>什么的都给替换掉了,还有一种可能就是你用的是JPEGTranscoder,你换成PNGTranscoder 或 SVGAbstractTranscoder试下吧只能帮你到这了。
Deaver_gwp 2018-05-27
  • 打赏
  • 举报
回复
有大神在线吗?急等啊!那位大神在么帮帮忙!

87,954

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧