Java 中hightcharts导出图到excel文件中

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));
			
		      }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Java 代码示例,演示如何将图片导出Excel 使用 POI 库: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.util.IOUtils; import org.apache.poi.ss.util.ImageUtils; public class PoiExportImageToExcelExample { public static void main(String[] args) throws IOException { String imagePath = "path_to_image.png"; String excelPath = "path_to_excel.xlsx"; int rowIndex = 0; int columnIndex = 0; // 创建 Excel 工作簿 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 加载图片文件 byte[] imageData = IOUtils.toByteArray(new FileInputStream(imagePath)); // 插入图片到单元格 Drawing<?> drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, columnIndex, rowIndex, columnIndex + 1, rowIndex + 1); Picture picture = drawing.createPicture(anchor, workbook.addPicture(imageData, Workbook.PICTURE_TYPE_PNG)); // 输出到 Excel 文件 FileOutputStream fileOut = new FileOutputStream(excelPath); workbook.write(fileOut); fileOut.close(); workbook.close(); System.out.println("图片已经成功导出Excel 文件!"); } } ``` 这个示例假设图片已经存在于本地文件系统,你需要将 `path_to_image.png` 替换为你的图片路径。它还假设你想将图片插入到单元格的第一行第一列,如果你想插入到其他位置,你需要修改 `rowIndex` 和 `columnIndex` 变量的值。 这个示例使用 POI 的 `Workbook` 和 `Sheet` 类创建了一个 Excel 文件,然后使用 `Drawing` 和 `ClientAnchor` 类将图片插入到单元格。最后,使用 `FileOutputStream` 将工作簿写入到本地文件系统
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值