有人可以帮我解决如何在vb.net中打开excel表而不保存它吗? 我想将DataGridView的数据导出到excel进行编辑,然后将其返回到DataGridView,在这种情况下,我不想将这个excel表保存到计算机,只是打开它进行编辑,然后在数据导入DataGridView后关闭
编辑 - 下面是dode
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles enter code hereButton2.Click
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
For i = 0 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
For k As Integer = 1 To DataGridView1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value
Next
Next
Next
Dim xlpath As String = Path.GetTempPath()
xlWorkSheet.SaveAs(xlpath & "checkfile.xlsx")
Process.Start(xlpath & "checkfile.xlsx")
答案 0 :(得分:0)
您可以将文件保存在Path.GetTempPath
文件夹中,然后使用Process.Start
方法将其打开。
要在DataGridView
中重新加载数据,您可以使用FileSystemWatcher
课程。