1、获取hightcharts的SVG图
var charData = $("#chart").highcharts().getSVG();
将获取到的值传到后台
var title=$('#title').html().replace("/", "\/");
var url = '';//后台地址
//虚拟form
var form = $("<form>");
form.attr('style', 'display:none');
form.attr('target', '');
form.attr('method', 'post');
form.attr('action', url);
var input = $('<input type="text" name="ids"/>');
input.attr('value', charData);
form.append(input);
$('body').append(form);
form.submit();
2、后端获取到前端传来来的值’
title = URLDecoder.decode(title, "UTF-8");
HSSFWorkbook wb = ExcelUtil.getExcel3(title,ids, list, cols);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd(HH-mm-ss)");
String fileName = sdf.format(new Date()) + ".xls";//为避免处理起来麻烦,不要使用中文 //
ExcelUtil.responseExcel(wb, fileName, response);
3、
public class ExcelUtil {
public ExcelUtil() {
}
/**
* 导出图片到excel中
* @return
* @throws TranscoderException
*/
public static HSSFWorkbook getExcel3(String title,String ids, List<String> list, int colNums) throws TranscoderException {
HSSFWorkbook wb = new HSSFWorkbook();
String bracket = title.substring(title.indexOf("("),title.indexOf(")")+1);
String title1 = title.replace(bracket, "");
title1=title1.replaceAll(" ", "");
HSSFSheet sheet = wb.createSheet(title1);
HSSFRow row;
HSSFCell cell;
// 标题样式,居中,14,红色
HSSFCellStyle styleTitle = wb.createCellStyle();
styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont fontTitle = wb.createFont();
fontTitle.setFontHeightInPoints((short) 20);
fontTitle.setColor(HSSFColor.RED.index);
styleTitle.setFont(fontTitle);
// 表头样式,居中,11,蓝色
HSSFCellStyle styleHead = wb.createCellStyle();
styleHead.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleHead.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont fontHead = wb.createFont();
fontHead.setFontHeightInPoints((short) 11);
fontHead.setColor(HSSFColor.BLUE.index);
styleHead.setFont(fontHead);
// 表格样式,居中,10
HSSFCellStyle styleBody = wb.createCellStyle();
styleBody.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleBody.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont fontBody = wb.createFont();
fontBody.setFontHeightInPoints((short) 10);
styleBody.setFont(fontBody);
// 尾注样式,靠右,12,深绿色,黄色填充
HSSFCellStyle styleFoot = wb.createCellStyle();
styleFoot.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
styleFoot.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont fontFoot = wb.createFont();
fontFoot.setFontHeightInPoints((short) 12);
fontFoot.setColor(HSSFColor.DARK_GREEN.index);
styleFoot.setFont(fontFoot);
styleFoot.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
styleFoot.setFillForegroundColor(HSSFColor.YELLOW.index);
// 设置标题行
row = sheet.createRow(0);
cell = row.createCell(0);
// 行高
row.setHeightInPoints((float) (12.75 * 3));
// 内容
cell.setCellValue(title);
// 样式
cell.setCellStyle(styleTitle);
// 合并单元格 (始行,终行,始列,终列)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, colNums - 1));
// 设置表头
row = sheet.createRow(1);
// 行高
row.setHeightInPoints(15);
// 内容
String str = list.get(0);
String[] ary = str.split(",");
for (int j = 0; j < ary.length; j++) {
cell = row.createCell(j);
cell.setCellValue(ary[j]);
cell.setCellStyle(styleHead);
}
// 设置表格内容
for (int i = 2; i <= list.size(); i++) {
// 序号列
row = sheet.createRow(i);
cell = row.createCell(0);
cell.setCellValue(i - 1);
cell.setCellStyle(styleBody);
// 内容列
str = list.get(i - 1);
ary = str.split(",");
for (int j = 1; j <= ary.length; j++) {
cell = row.createCell(j);
cell.setCellValue(ary[j - 1]);
cell.setCellStyle(styleBody);
}
}
// 设置脚注
int n = sheet.getLastRowNum();
row = sheet.createRow(++n);
row.setHeightInPoints((float) (12.75 * 2));
cell = row.createCell(0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cell.setCellValue("数据生成时间:" + sdf.format(new Date()));
cell.setCellStyle(styleFoot);
sheet.addMergedRegion(new CellRangeAddress(n, n, 0, colNums - 1));
// 自动调整列宽(对中文不完美)
for (int k = 0; k < colNums; k++) {
sheet.autoSizeColumn((short) k, true);
}
if(!StringUtil.isEmptyStr(ids)){
/
byte[] bytes;
ByteArrayOutputStream byteArrayOut = null;
//System.out.println(ids.getBytes());
try {
bytes = ids.getBytes ("UTF-8");
byteArrayOut = new ByteArrayOutputStream();
PNGTranscoder t = new PNGTranscoder ();
TranscoderInput input = new TranscoderInput (new ByteArrayInputStream(bytes));
TranscoderOutput output = new TranscoderOutput (byteArrayOut);
t.transcode (input, output);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, n+2, (short) --colNums, colNums-1);
anchor.setAnchorType(3);
//插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
}
return wb;
}
}
4、核心代码
if(!StringUtil.isEmptyStr(ids)){
/
byte[] bytes;
ByteArrayOutputStream byteArrayOut = null;
//System.out.println(ids.getBytes());
try {
bytes = ids.getBytes ("UTF-8");
byteArrayOut = new ByteArrayOutputStream();
PNGTranscoder t = new PNGTranscoder ();
TranscoderInput input = new TranscoderInput (new ByteArrayInputStream(bytes));
TranscoderOutput output = new TranscoderOutput (byteArrayOut);
t.transcode (input, output);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, n+2, (short) --colNums, colNums-1);
anchor.setAnchorType(3);
//插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
}