Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have Tried a code to export datagridview data to excl file, it is working perfectly but
it actually just exports it to an existing excel file. is it possible to have a code that can create a new excel file.

i have tried the code below please check

What I have tried:

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 TAPDataGridView.RowCount - 2
For j = 0 To TAPDataGridView.ColumnCount - 1
For k As Integer = 1 To TAPDataGridView.Columns.Count
xlWorkSheet.Cells(1, k) = TAPDataGridView.Columns(k - 1).HeaderText
xlWorkSheet.Cells(i + 2, j + 1) = TAPDataGridView(j, i).Value.ToString()
Next
Next
Next


xlWorkSheet.SaveAs("C:\Users\P A R\Documents\?.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)

MsgBox("You can find the file D:\Todays_record_excel.xlsx")
End Sub
Posted
Updated 17-Dec-20 19:23pm
Comments
Richard Deeming 18-Dec-20 9:18am    
?.xlsx is not a valid file name.

Why are you telling the user to look for D:\Todays_record_excel.xlsx when you've saved the file in C:\Users\P A R\Documents\ instead?

1 solution

Copy a blank workbook and update that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900