C#应用NPOI实现导出EXcel表格中插入饼状图(可实现动态数据生成)
一、思路: 1、excel是可以通过NPOI插入图片的;
2、C#通过NPOI生成饼状图;
3、把生成的饼状图以字节流的形式插入到表格
二、看代码:
#region 生成饼图图例 /// <summary> /// 生成饼图饼图的颜色随机产生 /// </summary> /// <param name="width">图片宽度</param> /// <param name="height">图片高度</param> /// <param name="radius">饼图半径</param> /// <param name="data">数据源</param> /// <param name="legends">图例</param> /// <returns></returns> public static Bitmap GetPieGraphic(int width, int height, int radius, float[] data, string[] legends) { if (data != null) { string familyName = "Arial"; Bitmap objbitmap = new Bitmap(width, height); Graphics objgraphics = Graphics.FromImage(objbitmap); objgraphics.Clear(Color.White); //抗锯齿 objgraphics.SmoothingMode = SmoothingMode.HighQuality; //高质量的文字 objgraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; //像素均偏移0.5个单位,以消除锯齿 objgraphics.PixelOffsetMode = PixelOffsetMode.Half; //定位饼图绘制的位置 Rectangle rect = new Rectangle(width / 2, height / 6, radius, radius); Random random = new Random(); float currentdegree = 0.0f; float Total = data.Sum(); List<Color> listColor =new List<Color>(); for (int i = 0; i < data.Length; i++) { Color temC = Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)); listColor.Add(temC); SolidBrush brush = new SolidBrush(temC); objgraphics.FillPie(brush, rect, currentdegree, Convert.ToSingle(data[i] / Total * 360)); currentdegree += Convert.ToSingle(data[i] / Total * 360); } //放置图例 PointF basePoint = new PointF(10, 20); //色块的大小 SizeF theSize = new SizeF(45, 16); //第一个色块的说明文字的位置 PointF textPoint = new PointF(basePoint.X + 50, basePoint.Y); for (int j = 0; j < listColor.Count; j++) { RectangleF baseRectangle = new RectangleF(basePoint, theSize); //画代表色块 objgraphics.FillRectangle(new SolidBrush(listColor[j]), baseRectangle); string temstr = String.Format("{0}%", (data[j] / Total * 100).ToString("#0.00")); objgraphics.DrawString(legends[j] + " " + temstr, new Font(familyName, 11), Brushes.Black, textPoint); basePoint.Y += 30; textPoint.Y += 30; } return objbitmap; } else { return null; } } /// <summary> /// 将BitMap类型的图像数据转换成byte[] /// </summary> /// <param name="bitmap">图像数据</param> /// <returns></returns> public static byte[] Bitmap2Byte(Bitmap bitmap) { using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Jpeg); byte[] data = new byte[stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(data, 0, Convert.ToInt32(stream.Length)); return data; } } #endregion 生成饼图图例
三、在处理表格数据出插入下面代码:
//legends和data可以是动态数据 String[] legends = {"A满意","B基本满意","C不满意" }; float[] data = {1,3,1 }; Bitmap bitmap = GetPieGraphic(200,200,100,data,legends); var row101 = sheet.CreateRow(82); row101.Height = 80 * 20; //将图片文件读入一个字符串 byte[] bytes = Bitmap2Byte(bitmap); int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG); HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); // 插图片的位置 HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2) 后面再作解释 HSSFClientAnchor anchor = new HSSFClientAnchor(100, 100, 0, 0, 1, 82, 6, 94); //把图片插到相应的位置 HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);