1

我正在使用 Interop.Excel 从数据表中导出数据并生成折线图。我有一个代码,可以将数据从数据表导出到 excel 并在那里创建图表,但是代码在这一行给出错误

Excel.Axis yAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlSeriesAxis,
Excel.XlAxisGroup.xlPrimary);

并且我收到的错误消息Value 不在预期范围内

我的折线图生成的完整代码。

private void button1_Click(object sender, EventArgs e)
    {
        System.Data.DataTable dt = GetGraphData();

        Excel.Application xla = new Excel.Application();
        xla.Visible = true;
        Excel.Workbook wb = xla.Workbooks.Add(Excel.XlSheetType.xlWorksheet);
        Excel.Worksheet ws = (Excel.Worksheet)wb.ActiveSheet;
        //********************** Now create the chart. *****************************
        Excel.ChartObjects chartObjs = (Excel.ChartObjects)ws.ChartObjects(Type.Missing);
        Excel.ChartObject chartObj = chartObjs.Add(250, 60, 300, 300);
        Excel.Chart xlChart = chartObj.Chart;

        int nRows = 2;
        int nColumns = dt.Rows.Count;
        string upperLeftCell = "B2";
        int endRowNumber = System.Int32.Parse(upperLeftCell.Substring(1))
            + nRows - 1;
        char endColumnLetter = System.Convert.ToChar(
            Convert.ToInt32(upperLeftCell[0]) + nColumns - 1);
        string upperRightCell = System.String.Format("{0}{1}",
            endColumnLetter, System.Int32.Parse(upperLeftCell.Substring(1)));
        string lowerRightCell = System.String.Format("{0}{1}",
            endColumnLetter, endRowNumber);

        Excel.Range rg = ws.get_Range(upperLeftCell, lowerRightCell);
        for (int i = 1; i <= dt.Rows.Count; i++)
        {
            rg[1, i] = dt.Rows[i - 1][0].ToString();          //For Adding Header Text
            rg[2, i] = int.Parse(dt.Rows[i - 1][1].ToString());  //For Adding Datarow Value
        }


        Excel.Range chartRange = ws.get_Range(upperLeftCell, lowerRightCell);
        xlChart.SetSourceData(chartRange, Type.Missing);
        xlChart.ChartType = Excel.XlChartType.xlLine;

        // *******************Customize axes: ***********************
        Excel.Axis xAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlCategory,
             Excel.XlAxisGroup.xlPrimary);
        //xAxis.HasTitle = true;
        // xAxis.AxisTitle.Text = "X Axis";

        Excel.Axis yAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlSeriesAxis,
             Excel.XlAxisGroup.xlPrimary);
        //yAxis.HasTitle = true;
        //yAxis.AxisTitle.Text = "Y Axis";

        Excel.Axis zAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlValue,
             Excel.XlAxisGroup.xlPrimary);
        //zAxis.HasTitle = true;
        //zAxis.AxisTitle.Text = "Z Axis";

        // *********************Add title: *******************************
        xlChart.HasTitle = true;
        xlChart.ChartTitle.Text = "Project Status Graph";

        // *****************Set legend:***************************
        xlChart.HasLegend = true;

        FileStream file = new FileStream(@"c:\pop.xls", FileMode.Create);
        file.Close();

        wb.SaveCopyAs(@"c:\pop.xls");

        // ****************For Quiting The Excel Aplication ***********************
        if (xla != null)
        {
            xla.DisplayAlerts = false;
            wb.Close();
            wb = null;
            xla.Quit();
            xla = null;
        }
    }

    private System.Data.DataTable GetGraphData()
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("ProjectStatus"), new DataColumn("per") });

        DataRow dr1 = dt.NewRow();
        dr1[0] = "Compleet";
        dr1[1] = 20;
        dt.Rows.Add(dr1);

        DataRow dr2 = dt.NewRow();
        dr2[0] = "Pending";
        dr2[1] = 20;
        dt.Rows.Add(dr2);

        DataRow dr3 = dt.NewRow();
        dr3[0] = "UnCompleet";
        dr3[1] = 20;
        dt.Rows.Add(dr3);

        return dt;

    }

寻求帮助。谢谢

4

1 回答 1

1

我的同事告诉我,根据智能感知,第一个参数 (Excel.XlAxisType.xlSeriesAxis) 的值仅对 3D 图表有效。

您正在创建什么样的图表?

于 2013-10-24T14:56:21.540 回答