可以试试用 Free Spire.Office for .NET (https://www.e-iceblue.cn/Introduce/Free-Spire-Office-NET.html )的方法,思路:在Excel中生成图表( https://www.e-iceblue.cn/spirexls/explode-excel-pie-chart-and-doughnut-chart.html ),然后将图表保存为图片(https://www.e-iceblue.cn/spirexls/convert-excel-worksheet-and-chart-to-image.html ),再将图片插入到Word(https://www.e-iceblue.cn/spiredoc/insert-or-extrat-image-in-word.html ),然后打印(https://www.e-iceblue.cn/spiredoc/print-word-document-in-c.html ) 。 图表的设置细节需要你自己在Excel中生成图表时进行调整,以下代码供参考:
using System;
using Spire.Xls;
using Spire.Doc;
using System.Drawing.Imaging;
using System.Drawing;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace ExplodePieChart
{
class Program
{
static void Main(string[] args)
{
//加载Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
//获取工作表
Worksheet ws = workbook.Worksheets[0];
//创建饼状图表
Chart chart = ws.Charts.Add(ExcelChartType.Pie);
chart.DataRange = ws.Range["B2:B6"];
chart.SeriesDataFromRange = false;
chart.LeftColumn = 1;
chart.TopRow = 7;
chart.RightColumn = 9;
chart.BottomRow = 26;
chart.ChartTitle = "年销售额";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;
Spire.Xls.Charts.ChartSerie cs = chart.Series[0];
cs.CategoryLabels = ws.Range["A2:A6"];
cs.Values = ws.Range["B2:B6"];
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
chart.PlotArea.Fill.Visible = false;
//设置饼图整体分离程度
for (int i = 0; i < chart.Series.Count; i++)
{
chart.Series[i].DataFormat.Percent = 20;
}
//保存生成的excel文档
workbook.SaveToFile("ExplodePieChart.xlsx",ExcelVersion.Version2013);
//获取指定表格中的图表,保存为图片
Worksheet sheet = workbook.Worksheets[0];
Image[] imgs = workbook.SaveChartAsImage(sheet);
for (int i = 0; i < imgs.Length; i++)
{
imgs[i].Save(string.Format("img-{0}.png", i), ImageFormat.Png);
}
//创建Word文档,添加段落
Document doc = new Document();
Section section = doc.AddSection();
Paragraph para = section.AddParagraph();
//加载图片到System.Drawing.Image对象, 使用AppendPicture方法将图片插入到段落
Image image = Image.FromFile(@"img-0.png");
DocPicture picture = doc.Sections[0].Paragraphs[0].AppendPicture(image);
//设置文字环绕方式
picture.TextWrappingStyle = TextWrappingStyle.Square;
//指定图片位置
picture.HorizontalPosition = 50.0f;
picture.VerticalPosition = 50.0f;
//设置图片大小
picture.Width = 400;
picture.Height = 200;
//保存Word文档
doc.SaveToFile("AddChartImage.docx",Spire.Doc.FileFormat.Docx2013);
System.Diagnostics.Process.Start("AddChartImage.docx");
}
}
}
最后的Word文档效果:
"把"也不算难画:已知了扇形的位置和大小,取这个扇形弧中间点A,沿着过圆心和A的的直线延向外延迟至B点,然后再画一小段横线。不过如果扇形太多了,得考虑文字是否重叠,有些复杂。
请问,有直接的类库,可以用吗
最简单的方法,直接利用微软自带的rdlc就可以轻松实现