I am creating a button that will export all the data in my datagridview into excel
But I want to put it in a specific cell. How can I do that? I am still a beginner in programming.
This code will export all the data in my Datagridview to excel but not in a specific cells, that I want to do.
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(xlPath)
'xlWorkBook = xlApp.Workbooks.Add(misValue)
'xlWorkSheet = xlWorkBook.Sheets("sheet2")
xlWorkSheet = CType(xlWorkBook.Sheets("sheet2"), Microsoft.Office.Interop.Excel.Worksheet)
For i = 0 To DataGridView1.RowCount - 1
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
xlWorkSheet.SaveAs(CType(xlPath, String))
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("You can find the file C:\Users\Alex\Desktop\print2")
Dim res As MsgBoxResult
res = MsgBox("Process completed, Would you like to open file?", MsgBoxStyle.YesNo)
If (res = MsgBoxResult.Yes) Then
Process.Start(xlPath)
End If
What I have tried:
this code is only for one row that will place the data in specific cell in my excel but I want to put all the data that i exported in my excel to a specific cell.
<pre>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 t As Integer
Dim s As Integer
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(xlPath)
xlWorkSheet = xlWorkBook.Sheets("Payment Printing")
xlWorkSheet.Cells(t + 6, s + 8) = DATE_OF_PAYMENTTextBox.Text()
xlWorkSheet.Cells(t + 7, s + 2) = PAID_TOTextBox.Text()
xlWorkSheet.Cells(t + 12, s + 8) = AMOUNTTextBox.Text()
xlWorkSheet.Cells(t + 12, s + 7) = TOTextBox.Text()
xlWorkSheet.Cells(t + 12, s + 6) = FROMTextBox.Text()
xlWorkSheet.Cells(t + 12, s + 3) = ACTIVITYTextBox.Text()
xlWorkSheet.Cells(t + 17, s + 3) = TYPE_OF_EXPENSETextBox.Text()
xlWorkSheet.Cells(t + 20, s + 3) = ACCOUNT_CODETextBox.Text()
xlWorkSheet.Cells(t + 23, s + 3) = CHECKTextBox.Text()
xlWorkSheet.Cells(t + 24, s + 3) = BANKTextBox.Text()
xlWorkSheet.Cells(t + 25, s + 3) = DATE1TextBox.Text()
xlWorkSheet.Cells(t + 23, s + 6) = VATABLETextBox.Text()
xlWorkSheet.Cells(t + 24, s + 6) = SUBJECT_TO_W_TAXTextBox.Text()
xlWorkSheet.Cells(t + 25, s + 6) = W_TAX_RATETextBox.Text()
xlWorkSheet.Cells(t + 23, s + 8) = INPUT_VATTextBox.Text()
xlWorkSheet.Cells(t + 24, s + 8) = W_TAXTextBox.Text()
xlWorkSheet.Cells(t + 25, s + 8) = BALANCETextBox.Text()
xlWorkSheet.Cells(t + 21, s + 8) = TOTAL_PHPTextBox.Text()
Try
If MsgBox("WOULD YOU LIKE TO SEE THE DATA IN EXCEL?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
xlWorkSheet.SaveAs(xlPath)
Process.Start(xlPath)
Else
Exit Sub
End If
xlWorkBook.Close()
xlApp.Quit()
Catch ex As Exception
End Try
ReleaseObject(xlApp)
ReleaseObject(xlWorkBook)
ReleaseObject(xlWorkSheet)
End Sub