使用Free Spire.XLS插入图表
前言
最近在研究Office中间件,上网搜索了下,比较出名的有两个:Aspose和Spire,两者功能齐全,对Office的支持趋近完善,但售价不菲。仔细搜索下,发现Spire系列还有免费版的产品,相对商业版本,在性能和文件转换功能上有缩水。通过一段时间的使用后,我发现免费版本能够满足我的大部分需求。所以写下这篇文章,和大家分享一下我的使用心得,这里我使用的是Free Spire.XLS,根据文中中的数据插入图表并保存。
准备
首先从CodePlex(https://freenetexcel.codeplex.com/)上下载Free Spire.XLS,然后引用Spire.License.dll和Spire.XLS.dll到工程中,如下图所示:
步骤
1.载入一个现有的Excel文档,里面已经包含生成图表所需的数据。
Workbook wb =
new Workbook();
wb.LoadFromFile( " Test.xlsx ", ExcelVersion.Version2010);
Worksheet ws = wb.Worksheets[ 0];
wb.LoadFromFile( " Test.xlsx ", ExcelVersion.Version2010);
Worksheet ws = wb.Worksheets[ 0];
2.创建一个条形图表,并设置图表的数据区域和位置。
//
Create a coulumn clustered chart
Chart chart = ws.Charts.Add(ExcelChartType.ColumnClustered);
// Set region of chart data
chart.DataRange = ws.Range[ " A1:C5 "];
chart.SeriesDataFromRange = false;
// Set position of chart
chart.LeftColumn = 1;
chart.TopRow = 7;
chart.RightColumn = 11;
chart.BottomRow = 30;
Chart chart = ws.Charts.Add(ExcelChartType.ColumnClustered);
// Set region of chart data
chart.DataRange = ws.Range[ " A1:C5 "];
chart.SeriesDataFromRange = false;
// Set position of chart
chart.LeftColumn = 1;
chart.TopRow = 7;
chart.RightColumn = 11;
chart.BottomRow = 30;
3. 设置图表标题格式以及两条坐标轴的格式。
//
Chart title
chart.ChartTitle = " Sales market by country ";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;
// Chart primary category axis
chart.PrimaryCategoryAxis.Title = " Country ";
chart.PrimaryCategoryAxis.Font.IsBold = true;
chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
// Chart primary value axis
chart.PrimaryValueAxis.Title = " Sales(in Dollars) ";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.MinValue = 1000;
chart.PrimaryValueAxis.TitleArea.IsBold = true;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = - 90;
chart.ChartTitle = " Sales market by country ";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;
// Chart primary category axis
chart.PrimaryCategoryAxis.Title = " Country ";
chart.PrimaryCategoryAxis.Font.IsBold = true;
chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
// Chart primary value axis
chart.PrimaryValueAxis.Title = " Sales(in Dollars) ";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.MinValue = 1000;
chart.PrimaryValueAxis.TitleArea.IsBold = true;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = - 90;
4. 显示数据标签,并将位置设定为“居中”。
//
Format data labels
foreach (ChartSerie cs in chart.Series)
{
cs.Format.Options.IsVaryColor = true;
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
cs.DataPoints.DefaultDataPoint.DataLabels.Position = DataLabelPositionType.Center;
}
foreach (ChartSerie cs in chart.Series)
{
cs.Format.Options.IsVaryColor = true;
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
cs.DataPoints.DefaultDataPoint.DataLabels.Position = DataLabelPositionType.Center;
}
5. 设置图例位置为“底部”,然后保存文件到磁盘。
chart.Legend.Position = LegendPositionType.Bottom;
wb.SaveToFile( " Sample.xlsx ",ExcelVersion.Version2010);
wb.SaveToFile( " Sample.xlsx ",ExcelVersion.Version2010);
生成的Excel文档如下图所示:
总结
网上不乏第三方操作excel的组件,Free Spire.XLS的优势在于免费易用,功能齐全,但最多只能处理5个工作簿,每个工作簿最多150行数据,这些是免费版本的限制。不过对于我来说已经足够了。