What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.
Public Sub ExportToExcel(ByVal dgv As DataGridView, ByVal path As String) Dim xlApp As New Application 'create a new workbook Dim xlWb As Workbook = xlApp.ActiveWorkbook Dim xlWs As Worksheet Dim lastRow As Long Dim lastCol As Long 'Check for existing data With xlApp .Visible = True 'open workbook xlWb = .Workbooks.Open(path) 'set it to the relavant sheet xlWs = xlWb.ActiveSheet With xlWs Dim rowcount As Integer = 1 For Each gridRow As DataGridViewRow In dgv.Rows rowcount += 1 For i As Integer = 0 To dgv.Columns.Count - 1 Next Next 'Add the header the first time through If rowcount = 2 Then For i As Integer = 0 To dgv.Columns.Count - 1 .Cells(1, i + 1).Value = dgv.Columns(i).HeaderText .Cells(1, i + 1).font.bold = True 'export the values to a blank Spreadsheet For rowlength As Integer = 0 To dgv.Rows.Count - 1 For columnlength As Integer = 0 To dgv.Columns.Count - 1 .Cells(rowlength + 2, columnlength + 1).Value = dgv.Rows(rowlength).Cells(columnlength).Value Next Next Next End If 'append values to a previously populated Spreadsheet If rowcount > 2 Then Dim fileName As String = "c:\excelRTM2.xlxs" Dim sb As StringBuilder = New StringBuilder Dim dvgTraceability As dvgTraceability For rowlength As Integer = 0 To dgv.Rows.Count - 1 For columnlength As Integer = 0 To dgv.Columns.Count - 1 .Cells(rowlength + 2, columnlength + 1).Value = dgv.Rows(rowlength).Cells(columnlength).Value sb.Append(dvgTraceability.ToString) Next Next Using writer As StreamWriter = New StreamWriter(fileName, True) writer.Write(sb.ToString()) End Using End If End With End With 'save the workbook as the path specified xlWb.SaveAs(path) releaseObject(xlApp) End Sub
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
|Help with adding xml file as datasource in MS Report||8||19|
|Excel: Calculating during business hour time versus after business hour time||24||21|
|Help with viewing images in an MSReport using a Dataset using VB.NET||29||28|
|VBA to Import multiple excel files into a worksheet||5||13|
Join the community of 500,000 technology professionals and ask your questions.