zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    廖雪峰python 摘录1
    python web 测试CGI脚本
    AttributeError: 'Athlete' object has no attribute 'top3'
    python类定义小问题
    def函数安装调用的疑惑
    print_lol函数调用
    实现键盘输入信息,按enter键调用摄像头自动拍照
    EditText不能输入表情
    使用videoview播放资源文件夹下的视频
    使用videoview连续自动播放网络视屏
  • 原文地址:https://www.cnblogs.com/lijl/p/10718741.html
Copyright © 2011-2022 走看看