• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#应用NPOI实现导出EXcel表格中插入饼状图(可实现动态数据生成) ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、思路:   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);

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
如何创建C# Closure ?发布时间:2022-07-14
下一篇:
[C#学习笔记02]理解值和引用发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap