NPOI是一个很强大的第三方类库(自己是POI[java类库]改过来的.net版本),其余介绍本身去百度。这篇文章主要是最近有个需求,须要将饼图导入到Excel中,这里提供两种解决方案(其实应该有三种)。java
第一种:将图片写入到Excel中测试
这种方式,须要本身先在代码中生成饼图,而后将图片写入到Excel中。.net
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Drawing : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int[] data = { 100,200,300,460}; Color[] colors={Color.Green,Color.Blue,Color.Yellow,Color.Tomato}; Bitmap bm = new Bitmap(400,400); Graphics g = Graphics.FromImage(bm); g.Clear(Color.White); g.DrawString("饼图测试",new Font("宋体",16),Brushes.Red,new PointF(5,5)); float totalValue = 0; foreach (int i in data) { totalValue += i; } float sweepAngle = 0; float startAngle = 0; int index=0; float x = 50f; float y = 50f; float width = 200f; foreach (int i in data) { sweepAngle=i/totalValue*360; g.FillPie(new SolidBrush(colors[index++]),x,y,width,width,startAngle,sweepAngle); //g.DrawPie(Pens.Black,x,y,width,width,startAngle,sweepAngle); //加边线代码 startAngle += sweepAngle; } bm.Save(Response.OutputStream,ImageFormat.Jpeg); g.Dispose(); } }
//g.DrawPie(Pens.Black,x,y,width,width,startAngle,sweepAngle);给饼图加边线
第二种:用Excel模板导出orm
第二种方式须要本身先手动建立一个带Excel文档,里面插入一个图表,定义好数据源,根据传入的数据源,Excel自动生成饼图。blog
第三种:NPOI自己自带生成图表的类库,是能够自定义生成的,可是APi很差查,这种方法争取后续补上。图片