重構機房之“匯出EXCEL異常”

來源:互聯網
上載者:User

標籤:異常   重構   excel   原始碼   對象   

    在重構機房,完成EXCEL匯出功能時出現異常。

    當運行到匯出EXCEL的語句:

<span style="font-family:KaiTi_GB2312;font-size:24px;">xlApp.Cells(rowindex, colindex) = row(col.ColumnName)</span>
時,顯示異常:


    對此,特尋找原因與解決方案,在此與大家分享,也請高手多指教呀!

    原因:程式在運行調用傳遞地址時起衝突,導致出現此異常。

    解決方案:將原始碼替換成一下代碼即可。

<span style="font-family:KaiTi_GB2312;font-size:24px;">Dim MyExcel As NewMicrosoft.Office.Interop.Excel.Application()       MyExcel.Application.Workbooks.Add(True)       MyExcel.Visible = True        '將datagridview中的欄位名匯出        Dim m As Integer       For m = 0 To DataGridView1.ColumnCount - 1           MyExcel.Cells(1, m + 1) = Me.DataGridView1.Columns(m).HeaderText       Next m        '往excel表裡添加資料        Dim i As Integer       For i = 0 To DataGridView1.RowCount - 1           Dim j As Integer           For j = 0 To DataGridView1.ColumnCount - 1                If Me.DataGridView1(j, i).ValueIs System.DBNull.Value Then                     MyExcel.Cells(i + 2, j + 1)= ""                Else                    MyExcel.Cells(i + 2, j + 1)= DataGridView1(j, i).Value.ToString                 End If           Next j       Next i</span>


 

重構機房之“匯出EXCEL異常”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.