从智能标记生成 excel 图表

Generate excel chart from smart marker aspose

我有一个 excel 模板,其中包含以下两列句点和点,trend.period 和 trend.points 是代表数据的智能标记。

Period          Points
&=trend.period  &=trend.points

生成的table如下。

Period  Points
4Q'14   27
1Q'15   0
2Q'15   0
3Q'15   0
4Q'15   200

table2生成柱形图很容易,如何用智能标记为table1生成相同的柱形图?

您可以使用 Aspose.Cells' 智能标记功能完成任务。您需要做的就是根据插入到单元格中的智能标记创建命名范围。我已经根据您的要求使用 Aspose.Cells API 编写了示例代码。请参考它,您可以根据需要add/update相应的代码段。

例如

示例代码:

  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 担任支持开发人员/传播者。