ECharts图表导入Excel
一、获取Echarts图片数据
//myChart为全局变量,图表对象;getDataURL方法用来获取图片数据,默认图片格式为"png",可以根据需要设置
var imgData = myChart.getDataURL();
二、后台处理
2.1 引用NPOI
2.2 获取数据流
//get the image data and trim "data:image/png;base64,". string imgData = Request.Form["imgData"]; imgData = imgData.Split(',')[1]; Byte[] bytes = Convert.FromBase64String(imgData);
2.3 写入excle文件
2.2.1 xls文件
//create a hssworkbok HSSFWorkbook sheets = new HSSFWorkbook(); //create a sheet HSSFSheet sheet1 = (HSSFSheet)sheets.CreateSheet("your sheet's name"); //create a patriarch HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch(); //create a anchor HSSFClientAnchor anchor = new HSSFClientAnchor(); //load the picture and get the picture index in the workbook int pictIndx = sheets.AddPicture(bytes, PictureType.PNG); //create a picture HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictIndx); //Reset the image to the original size. pict.Resize(); //set the file path string filePath = "your file path";//relative file path //write to file using (FileStream fs = new FileStream(System.Web.HttpContext.Current.Server.MapPath(filePath), FileMode.Create)) { sheets.Write(fs); }
2.2.2 xlsx文件
//create a workbook. XSSFWorkbook sheets = new XSSFWorkbook(); //create a sheet. ISheet sheet1 = sheets.CreateSheet("your sheet's name"); //create a patriarch. XSSFDrawing patriarch = (XSSFDrawing)sheet1.CreateDrawingPatriarch(); //create a anchor. XSSFClientAnchor anchor = new XSSFClientAnchor(); //load the picture and get the picture index in the workbook. int pictIdx = sheets.AddPicture(bytes, XSSFWorkbook.PICTURE_TYPE_PNG); //create a picture. IPicture pict = patriarch.CreatePicture(anchor, pictIdx); //reset the image to the original size. pict.Resize(); //set the file path string filePath = "your file path";//relative file path //write to file using (FileStream fs = new FileStream(System.Web.HttpContext.Current.Server.MapPath(filePath), FileMode.Create)) { sheets.Write(fs); }
2.4 下载
将文件路径传到前端href或src,触发浏览器下载。
效果图如下:
本文来自博客园,作者:yscit,转载请注明原文链接:https://www.cnblogs.com/yscit/p/15095865.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?