一、思路: 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 / , height / , radius, radius);
Random random = new Random();
float currentdegree = 0.0f;
float Total = data.Sum();
List<Color> listColor =new List<Color>();
for (int i = ; i < data.Length; i++)
{
Color temC = Color.FromArgb(random.Next(), random.Next(), random.Next());
listColor.Add(temC);
SolidBrush brush = new SolidBrush(temC);
objgraphics.FillPie(brush, rect, currentdegree, Convert.ToSingle(data[i] / Total * ));
currentdegree += Convert.ToSingle(data[i] / Total * );
}
//放置图例
PointF basePoint = new PointF(, );
//色块的大小
SizeF theSize = new SizeF(, );
//第一个色块的说明文字的位置 PointF textPoint = new PointF(basePoint.X + , basePoint.Y);
for (int j = ; 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 * ).ToString("#0.00"));
objgraphics.DrawString(legends[j] + " " + temstr, new Font(familyName, ), Brushes.Black, textPoint);
basePoint.Y += ;
textPoint.Y += ;
}
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(, SeekOrigin.Begin);
stream.Read(data, , Convert.ToInt32(stream.Length));
return data;
}
}
#endregion 生成饼图图例
三、在处理表格数据出插入下面代码:
//legends和data可以是动态数据
String[] legends = {"A满意","B基本满意","C不满意" };
float[] data = {,, };
Bitmap bitmap = GetPieGraphic(,,,data,legends);
var row101 = sheet.CreateRow();
row101.Height = * ;
//将图片文件读入一个字符串
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(, , , , , , , );
//把图片插到相应的位置
HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);