我在VB中进行导出。我能够将datagridview的值导出为excel,但是当我打开文件时,它会显示此错误。
我正在使用以下代码导出datagridview的值。
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 sPath As String = String.Empty
Dim dlgSave As New SaveFileDialog
Dim i As Integer
Dim j As Integer
dlgSave.DefaultExt = "xls"
dlgSave.Filter = "Microsoft Excel|*.xls"
If dlgSave.ShowDialog = System.Windows.Forms.DialogResult.OK Then
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To dgvFile.RowCount - 1
For j = 0 To dgvFile.ColumnCount - 1
For k As Integer = 1 To dgvFile.Columns.Count
xlWorkSheet.Cells(1, k) = dgvFile.Columns(k - 1).HeaderText
xlWorkSheet.Cells(i + 2, j + 1) = dgvFile(j, i).Value.ToString()
Next
Next
Next
Dim sFileName As String = Replace(dlgSave.FileName, ".xlsx", ".xlx")
xlWorkSheet.SaveAs(sFilename)
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
Dim res As MsgBoxResult
res = MsgBox("Process completed, Would you like to open file?", MsgBoxStyle.YesNo)
If (res = MsgBoxResult.Yes) Then
Process.Start(sFileName)
End If
End If
注意 这段代码工作正常。它会毫无问题地导出值。它只是在我打开excel文件时显示错误。 单击“是”时导出的文件没有问题。它显示正确的值。我只是想在打开文件时停止显示此错误。
任何人都可以帮我解决这个问题吗?我将非常感谢任何能够领导我的人。谢谢。 :)
答案 0 :(得分:0)
我明白了。感谢akhil kumar。
我的Excel使用.xlsx扩展程序,因此我更改了
dlgSave.DefaultExt = "xls"
到
dlgSave.DefaultExt = "xlsx"
并删除了Replace函数,使其看起来像这样。
Dim sFileName As String = dlgSave.FileName
这完美无缺。这个问题已经排序。 :)