gpt4 book ai didi

vb.net - DataGridView需要导出为ex​​cel文件

转载 作者:行者123 更新时间:2023-12-02 11:37:56 25 4
gpt4 key购买 nike

我的程序包含一个datagridview,用户在运行时手动填充数据。当用户单击“保存”时,我希望将 datagridview 导出到他们稍后可以访问的 Excel 文件。

我目前的代码出现错误类型“空引用异常”

Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()

If xlApp Is Nothing Then
MessageBox.Show("Excel is not properly installed!!")
Return
End If


Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer

xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
'xlWorkSheet.Cells(1, 1) = "Sheet 1 content"
'xlWorkSheet.Cells(3, 3) = "Sheet 1 content"

If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
Exit Sub
End If



For k As Integer = 0 To DataGridView1.Columns.Count - 1
xlWorkSheet.Cells(1, k + 1) = DataGridView1.Columns(k).HeaderText
Next
For i = 0 To DataGridView1.RowCount - 1
For j = 0 To DataGridView1.ColumnCount - 1

xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()

Next
Next



xlWorkBook.SaveAs("C:\test\DATAGRID.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
xlWorkBook.Close(True, misValue, misValue)
xlApp.Quit()

releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)

MessageBox.Show("Excel file created , you can find the file C:\test\DATAGRID.xls")

最佳答案

谢谢大家的帮助

我发现这段代码实际上可以实现我想要的功能

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
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
Dim misValue As Object = System.Reflection.Missing.Value


Try
Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)

rowsTotal = DataGridView1.RowCount - 1
colsTotal = DataGridView1.Columns.Count - 1
With excelWorksheet
.Cells.Select()
.Cells.Delete()
For iC = 0 To colsTotal
.Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
Next
For I = 0 To rowsTotal - 1
For j = 0 To colsTotal - 1
.Cells(I + 2, j + 1).value = DataGridView1.Rows(I).Cells(j).Value
Next j
Next I
.Rows("1:1").Font.FontStyle = "Bold"
.Rows("1:1").Font.Size = 10
.Cells.Columns.AutoFit()
.Cells.Select()
.Cells.EntireColumn.AutoFit()
.Cells(1, 1).Select()
End With
excelBook.SaveAs("C:\test\DATAGRID.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
MessageBox.Show("Excel file created , you can find the file C:\test\DATAGRID.xls")




Catch ex As Exception
MsgBox("Export Excel Error " & ex.Message)
Finally
'RELEASE ALLOACTED RESOURCES
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
xlApp = Nothing
End Try
End Sub

关于vb.net - DataGridView需要导出为ex​​cel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35712033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com