Generating Excel through .Net doing odd things

2019-08-02 20:00发布

Good Morning,
I have a rather odd thing happening with an Excel WorkSheet that I am creating through .Net. I load up an existing workbook

Dim _xlApp As New Excel.Application
Dim _xlWorkbook As Excel.WokBook = _xlApp.Workbooks.Open(_templateFileName)

set it to be visible before populating it (for debugging purposes)

_xlApp.Visible = True

I then go on to populate the existing template.

Private Sub GenerateOceanFreightGrid()

        Dim columnCount As Integer = 4 ' _standardColumnCount
        Dim columnInsertPosition As Integer = 5

        If MyBooleanValue() Then
            _xlSheet.Columns(4).Insert()
            _xlSheet.Cells(7, 4) = "My Header Value"
            columnInsertPosition += 1
            columnCount += 1
        End If

When i set a breakpoint on the "Private Sub Gen.." line, and step through the code line by line, it inserts a new column for each variable declared, so in this case it will insert two columns before it even hits the "If MyBooleanValue()..." line (I can watch it actually happen due to the Excel app being visible). However, if i dont set a breakpoint and just let the code run straight through this doesnt happen.
There is no multi threading happening or anything else that i suspect would have any impact on the code. My obvious work around is to not set the breakpoint, i was just wondering if anyone has seen this happen before and for what reason? Thanks

1条回答
祖国的老花朵
2楼-- · 2019-08-02 20:38

I suspect this is due to some property that the debugger is evaluating in order to be helpful - but it's an evil property with the side-effect of adding a column.

I assume you don't have any Watch variables which could be doing this explicitly?

查看更多
登录 后发表回答