2

希望这不是太棘手,但是,我一直在使用 C# 和 Visual Studio 2012 开发一个应用程序,它可以解析文本并创建一系列List<T>'s. 有了这个,我已经成功地将这些数据导出到 Excel 电子表格中。但是,我已经在新工作表上创建图表,但是,我无法弄清楚如何从列表之一或其他工作表之一中获取数据并绘制图表。

这是我一直用于创建图表的片段:

const string topLeft = "A1";
const string bottomRight = "A4";
const string graphTitle = "License use";
const string xAxis = "Time";
const string yAxis = "Number of licenses";

var charts = worksheet.ChartObjects() as
Microsoft.Office.Interop.Excel.ChartObjects;
var chartObject = charts.Add(60, 10, 300, 300) as
Microsoft.Office.Interop.Excel.ChartObject;
var chart = chartObject.Chart;

// Set chart range.
range = worksheet.get_Range(topLeft, bottomRight);
chart.SetSourceData(range);

// Set chart properties.
chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;
chart.ChartWizard(Source: range,
Title: graphTitle,
CategoryTitle: xAxis,
ValueTitle: yAxis);

如果需要,我可以发布 Excel 文档创建的其余部分。

4

2 回答 2

0

刚刚创建了一个简单的示例,其中您拥有公司 ABC 以及他们拥有 5 年的许可证数量。

您可以看到您可以按名称选择工作表,然后向其中添加数据并为其添加图表。

    Sheets excelSheets = workbook.Worksheets;
    string currentSheet = "Sheet1";
    Worksheet worksheet1 = (Worksheet)excelSheets.get_Item(currentSheet);

在选定的工作表上,您可以添加图表,并告诉它您想要工作表 1 中的范围,您可以使用任何工作表的范围并使用 SetSourceData 将其推送到该图表,希望这有助于回答您关于不知道如何“到”的问题从其他工作表中获取数据并绘制图表。”

        var app = new Application();
        app.Visible = true;
        var workbook = app.Workbooks.Add(1);

        Sheets excelSheets = workbook.Worksheets;
        string currentSheet = "Sheet1";
        Worksheet worksheet1 = (Worksheet)excelSheets.get_Item(currentSheet);


        worksheet1.Cells[1, 1] = "";
        worksheet1.Cells[1, 2] = "Year 1";
        worksheet1.Cells[1, 3] = "Year 2";
        worksheet1.Cells[1, 4] = "Year 3";
        worksheet1.Cells[1, 5] = "Year 4";
        worksheet1.Cells[1, 6] = "Year 5";

        worksheet1.Cells[2, 1] = "Company A";
        worksheet1.Cells[2, 2] = "10";
        worksheet1.Cells[2, 3] = "50";
        worksheet1.Cells[2, 4] = "70";
        worksheet1.Cells[2, 5] = "70";
        worksheet1.Cells[2, 6] = "70";

        worksheet1.Cells[3, 1] = "Company B";
        worksheet1.Cells[3, 2] = "30";
        worksheet1.Cells[3, 3] = "70";
        worksheet1.Cells[3, 4] = "80";
        worksheet1.Cells[3, 5] = "80";
        worksheet1.Cells[3, 6] = "80";

        worksheet1.Cells[4, 1] = "Company C";
        worksheet1.Cells[4, 2] = "55";
        worksheet1.Cells[4, 3] = "65";
        worksheet1.Cells[4, 4] = "75";
        worksheet1.Cells[4, 5] = "75";
        worksheet1.Cells[4, 6] = "75";

        ChartObjects xlCharts = (ChartObjects)worksheet1.ChartObjects(Type.Missing);
        ChartObject myChart = (ChartObject) xlCharts.Add(60, 10, 300, 300);
        Range chartRange = worksheet1.get_Range("A1", "F4");

        Chart chartPage = myChart.Chart;
        chartPage.SetSourceData(chartRange, System.Reflection.Missing.Value);
        chartPage.ChartType = XlChartType.xlLine;
于 2013-06-13T22:00:21.880 回答
0

如果不需要互操作,请尝试使用 OpenXML SDK,您可以创建要对其建模的文档的模板,然后使用 OpenXML SDK 工具检查文档并查看用于生成它的代码,然后将其纳入您尝试创建的对象。有很多关于它的更多信息

http://openXMLDeveloper.org

DLL 和工具可以在以下位置找到

http://www.microsoft.com/en-us/download/details.aspx?id=30425

有几个 YouTube 视频也证明了这一点。此解决方案仅适用于基于 OpenXML 的文档(即 Office 2007 及更高版本)。

于 2013-06-13T21:09:10.477 回答