从智能标记aspose生成Excel图表
我有一个excel模板,包含以下两列周期和点,trend.period和trend.points是代表数据的aspose smart marker。
Period Points &=trend.period &=trend.points
下面是生成的表格。
Period Points 4Q'14 27 1Q'15 0 2Q'15 0 3Q'15 0 4Q'15 200
为表格2生成柱形图很容易,如何使用智能标记为表格1生成相同的图表?
您可以使用Aspose.Cells的智能标记function完成任务。 所有你需要做的是创build基于你的智能标记插入单元格的命名范围。 我已经使用Aspose.Cells API为您的需求编写了示例代码。 请参阅它,您可以根据您的需要添加/更新代码段。
例如
示例代码:
DataTable dt = new DataTable("Trend"); dt.Columns.Add("Period", typeof(string)); dt.Columns.Add("Points", typeof(int)); DataRow row = dt.NewRow(); row[0] = "4Q'14"; row[1] = 27; dt.Rows.Add(row); row = dt.NewRow(); row[0] = "1Q'15"; row[1] = 0; dt.Rows.Add(row); row = dt.NewRow(); row[0] = "2Q'15"; row[1] = 0; dt.Rows.Add(row); row = dt.NewRow(); row[0] = "3Q'15"; row[1] = 0; dt.Rows.Add(row); row = dt.NewRow(); row[0] = "4Q'15"; row[1] = 200; dt.Rows.Add(row); WorkbookDesigner wd = new WorkbookDesigner(); //Create a designer workbook Workbook workbook = new Workbook(); Worksheet worksheet = workbook.Worksheets[0]; worksheet.Cells["A1"].PutValue("Period"); worksheet.Cells["A2"].PutValue("&=Trend.Period"); worksheet.Cells["B1"].PutValue("Points"); worksheet.Cells["B2"].PutValue("&=Trend.Points"); Range range1 = worksheet.Cells.CreateRange("A2:A2"); range1.Name = "RANGE1"; Range range2 = worksheet.Cells.CreateRange("B2:B2"); range2.Name = "RANGE2"; wd.Workbook = workbook; wd.SetDataSource(dt); wd.Process(); //Create chart int chartIndex = worksheet.Charts.Add(ChartType.Column, 5, 2, 29, 10); Chart chart = worksheet.Charts[chartIndex]; //Obtain the updated ranges after processing smart markers var r1 = workbook.Worksheets.GetRangeByName("RANGE1"); var r2 = workbook.Worksheets.GetRangeByName("RANGE2"); MessageBox.Show(r2.RefersTo); //Add the nseries collection to a chart chart.NSeries.Add(r2.RefersTo, true); //Get or set the range of category axis values chart.NSeries.CategoryData = r1.RefersTo; chart.NSeries.IsColorVaried = true; wd.Workbook.Save("e:\\test2\\out1chart1.xlsx");
我在Aspose担任Support developer / Evangelist。