使用NPOI,如何在excel中绘制图表轴?

时间:2017-03-03 02:51:13

标签: vb.net npoi

NPOI版本2.2.1

以下是使用折线图生成Excel的示例代码,但生成图表时没有轴。

bottomAxis的属性(IsVisible)已经是真的,但仍然无法看到它。

我的问题是如何让这些轴可见?

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim wb As IWorkbook = New XSSFWorkbook()
    Dim ws As ISheet = wb.createSheet("linechart")
    Dim NUM_OF_ROWS = 3
    Dim NUM_OF_COLUMNS = 10


    For rowIndex As Integer = 0 To NUM_OF_ROWS
        Dim row = ws.CreateRow(rowIndex + 1)
        For colIndex As Integer = 0 To NUM_OF_COLUMNS
            Dim cell = ws.GetRow(rowIndex + 1).CreateCell(colIndex)
            cell.setCellValue(colIndex * (rowIndex + 1))
        Next

    Next
    Dim drawing = ws.createDrawingPatriarch()
    Dim anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15)
    Dim chart As IChart = drawing.createChart(anchor)
    Dim legend = chart.getOrCreateLegend()

    Dim dataFactory = chart.ChartDataFactory
    Dim chartAxisFactory = chart.ChartAxisFactory

    Dim lineChartData = dataFactory.createLineChartData(Of Double, Double)()
    Dim bottomAxis = chartAxisFactory.createCategoryAxis(NPOI.SS.UserModel.Charts.AxisPosition.BOTTOM)
    Dim leftAxis = chartAxisFactory.createValueAxis(NPOI.SS.UserModel.Charts.AxisPosition.RIGHT)


    Dim xs = NPOI.SS.UserModel.charts.DataSources.fromNumericCellRange(ws, New NPOI.SS.Util.CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1))
    Dim ys1 = NPOI.SS.UserModel.charts.DataSources.fromNumericCellRange(ws, New NPOI.SS.Util.CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1))
    Dim ys2 = NPOI.SS.UserModel.charts.DataSources.fromNumericCellRange(ws, New NPOI.SS.Util.CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1))


    lineChartData.addSeries(xs, ys1)
    lineChartData.addSeries(xs, ys2)

    chart.plot(lineChartData, bottomAxis, leftAxis)

    Dim file As FileStream = New FileStream("D:/feed//Xiaohongshu/" & "test" & ".xlsx", FileMode.Create)
    wb.Write(file)
    file.Close()


End Sub

1 个答案:

答案 0 :(得分:1)

这可能是NPOI 2.2.1的错误 当deafult为True时,我们需要将IsVisible设置为False

bottomAxis.IsVisible = False
leftAxis.IsVisible = False