为什么无法导出数据网格视图中的最后一行?

编程入门 行业动态 更新时间:2024-10-12 01:27:32
本文介绍了为什么无法导出数据网格视图中的最后一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click If HdInventoryDataGridView.RowCount = Nothing Then MessageBox.Show("Sorry nothing to export into excel sheet.." & vbCrLf & "Please retrieve data in datagridview", "", MessageBoxButtons.OK, MessageBoxIcon.Error) Exit Sub End If Dim rowsTotal, colsTotal As Short Dim I, j, iC As Short System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor Dim xlApp As New Excel.Application Try Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet) xlApp.Visible = True rowsTotal = HdInventoryDataGridView.RowCount - 1 colsTotal = HdInventoryDataGridView.Columns.Count - 1 With excelWorksheet .Cells.Select() .Cells.Delete() For iC = 0 To colsTotal .Cells(1, iC + 1).Value = HdInventoryDataGridView.Columns(iC).HeaderText Next For I = 0 To rowsTotal - 1 For j = 0 To colsTotal .Cells(I + 2, j + 1).value = HdInventoryDataGridView.Rows(I).Cells(j).Value Next j Next I .Rows("1:1").Font.FontStyle = "Bold" .Rows("1:1").Font.Size = 12 .Cells.Columns.AutoFit() .Cells.Select() .Cells.EntireColumn.AutoFit() .Cells(1, 1).Select() End With Catch ex As Exception MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Finally 'RELEASE ALLOACTED RESOURCES System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default xlApp = Nothing End Try End Sub

推荐答案

我记得,Cells集合索引从1开始。因此,我怀疑你错过了最后一行,因为你的限制是数 - 1。 As I recall, the Cells collection indexes start at 1. Hence, I suspect that you are missing the last row because your limit is count - 1.

更多推荐

为什么无法导出数据网格视图中的最后一行?

本文发布于:2023-05-26 02:24:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/335898.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   视图   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!