VB.net格式化系列并删除1个图例条目

时间:2017-12-13 17:30:15

标签: excel vb.net charts legend series

我有一些VB.NET代码,它使用Excel从一些数据创建图表。该图表的工作原理应该如何,但我发现从VBA切换到VB格式化很困难。

我想要删除图例中的系列1,并且图表本身的数据要么将透明度设置为100,要么将填充设置为无填充。我曾与charpage.seriescollection(1).format.fill等人玩过,但我没有得到它。

我的代码:

Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
    Dim xlWorkBook As Excel.Workbook = Nothing
    Dim xlWorkbooks As Excel.Workbooks = Nothing
    Dim xlWorkSheet As Excel.Worksheet = Nothing

    Dim chartPage As Excel.Chart
    Dim xlCharts As Excel.ChartObjects
    Dim myChart As Excel.ChartObject
    Dim chartRange As Excel.Range
    Dim Chartarea As Excel.ChartArea

xlCharts = xlWorkSheet.ChartObjects
        myChart = xlCharts.Add(10, 80, 900, 500)
        chartPage = myChart.Chart
        chartRange = xlWorkSheet.Range("B1", "E2000")
        chartPage.SetSourceData(Source:=chartRange)
        chartPage.ChartType = Excel.XlChartType.xlBarStacked
        chartPage.ChartWizard(Title:="Gantt Chart of Offshore Operations")
        chartPage.Legend.Position = Excel.XlLegendPosition.xlLegendPositionTop

        'Y axis
        With chartPage.Axes(Excel.XlAxisType.xlCategory)
            .categorytype = Excel.XlCategoryType.xlTimeScale
            .BaseUnit = Excel.XlTimeUnit.xlDays
        End With

        'X axis
        With chartPage.Axes(Excel.XlAxisType.xlValue)
            .HasTitle = True
            .AxisTitle.Caption = "Time (Days)"
        End With

1 个答案:

答案 0 :(得分:1)

我在Excel中编写了一些数据,保存了它,并使用了以下代码:

Imports Microsoft.Office.Interop

Public Class Form1

    Sub X()
        Dim xlApp As Excel.Application = Nothing
        Dim xlWorkBook As Excel.Workbook = Nothing

        Try
            xlApp = New Excel.Application()
            xlWorkBook = xlApp.Workbooks.Open("C:\temp\Book1.xlsx")
            Dim xlWorkSheet As Excel.Worksheet = xlWorkBook.Worksheets(1)

            Dim xlCharts As Excel.ChartObjects = xlWorkSheet.ChartObjects
            Dim myChart As Excel.ChartObject = xlCharts.Add(10, 80, 900, 500)
            Dim chartPage As Excel.Chart = myChart.Chart
            Dim chartRange As Excel.Range = xlWorkSheet.Range("B1", "E20")

            chartPage.SetSourceData(Source:=chartRange)
            chartPage.ChartType = Excel.XlChartType.xlBarStacked
            chartPage.ChartWizard(Title:="Gantt Chart of Offshore Operations")

            ' *****************
            myChart.Chart.SeriesCollection(1).Name = ""
            myChart.Chart.SeriesCollection(1).Interior.ColorIndex = Excel.Constants.xlNone

            chartPage.Legend.Position = Excel.XlLegendPosition.xlLegendPositionTop

            'Y axis
            With chartPage.Axes(Excel.XlAxisType.xlCategory)
                .categorytype = Excel.XlCategoryType.xlTimeScale
                .BaseUnit = Excel.XlTimeUnit.xlDays
            End With

            'X axis
            With chartPage.Axes(Excel.XlAxisType.xlValue)
                .HasTitle = True
                .AxisTitle.Caption = "Time (Days)"
            End With

            xlWorkBook.SaveAs("C:\temp\Book2")

        Finally
            xlWorkBook.Close()
            xlApp.Quit()

        End Try

    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            X()
        Finally
            GC.Collect()
            GC.WaitForPendingFinalizers()
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End Try

    End Sub

End Class

产生了这个:

enter image description here

认为是你想要的。

现金: