本文介绍了Datagrid Last Column未导出为ex​​cel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将最后一栏中的数据导出到excel时没有显示



my data in the last column is not being displayed when it exports it to excel

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 = DataGridView1.RowCount - 1
           colsTotal = DataGridView1.Columns.Count - 1
           With excelWorksheet
               .Cells.Select()
               .Cells.Delete()
               For iC = 0 To colsTotal
                   .Cells(3, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
               Next
               For I = 0 To rowsTotal - 1
                   For j = 0 To colsTotal - 1
                       .Cells(I + 5, j + 1).value = DataGridView1.Rows(I).Cells(j).Value
                   Next j
               Next I
               .Rows("1:1").Font.FontStyle = "Bold"
               .Rows("1:1").Font.Size = 11
               .Rows("3:1").Font.FontStyle = "Bold"
               .Rows("3:1").Font.Size = 11
               .Cells.Columns.AutoFit()
               .Cells.Select()
               .Cells.EntireColumn.AutoFit()
               .Cells(1, 1).Select()
               For j = 0 To colsTotal
                   .Cells(I + 5, j + 1).value = DataGridView1.Rows(I).Cells(j).Value
               Next j
           End With
Catch ex As Exception
            MsgBox("Export Excel Error " & ex.Message)
        Finally
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            xlApp = Nothing
        End Try

推荐答案

For I = 0 To rowsTotal - 1 ' By including the -1 here you are actually skipping the last row
   For j = 0 To colsTotal - 1 ' By including the -1 here you are actually skipping the last column
      .Cells(I + 5, j + 1).value = DataGridView1.Rows(I).Cells(j).Value
      Next j
   Next I


这篇关于Datagrid Last Column未导出为ex​​cel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:42