vb Code:
  1. Private Sub exportToExcel_new(ByVal dgv As DataGridView)
  2.         If Not File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then
  3.             Dim xlApp As excel.Application = New excel.Application
  4.             Dim xlWorkBook As excel.Workbook
  5.             Dim xlWorkSheet As excel.Worksheet
  6.             Dim misValue As Object = System.Reflection.Missing.Value
  7.  
  8.             xlWorkBook = xlApp.Workbooks.Add(misValue)
  9.             xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet)
  10.  
  11.             Dim headers = (From ch In dgv.Columns _
  12.                             Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
  13.                             Select header.Value).ToArray()
  14.             Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString)
  15.  
  16.             Dim items() = (From r In dgv.Rows _
  17.                     Let row = DirectCast(r, DataGridViewRow) _
  18.                     Where Not row.IsNewRow _
  19.                     Select (From cell In row.Cells _
  20.                         Let c = DirectCast(cell, DataGridViewCell) _
  21.                         Select c.Value).ToArray()).ToArray()
  22.  
  23.             Dim table As String = String.Join(vbTab, headerText) & Environment.NewLine
  24.  
  25.             For Each a In items
  26.                 Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
  27.                 table &= String.Join(vbTab, t) & Environment.NewLine
  28.             Next
  29.  
  30.             table = table.TrimEnd(CChar(Environment.NewLine))
  31.             Clipboard.SetText(table)
  32.  
  33.             Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
  34.  
  35.             Dim range As excel.Range = xlWorkSheet.Range("B2:" & alphabet(headerText.Length) & (items.Length + 2).ToString)
  36.  
  37.             range.Select()
  38.             xlWorkSheet.Paste()
  39.  
  40.             range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone
  41.             range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone
  42.             With range.Borders(excel.XlBordersIndex.xlEdgeLeft)
  43.                 .LineStyle = excel.XlLineStyle.xlContinuous
  44.                 .ColorIndex = 1 'black
  45.                 .TintAndShade = 0
  46.                 .Weight = excel.XlBorderWeight.xlMedium
  47.             End With
  48.             With range.Borders(excel.XlBordersIndex.xlEdgeTop)
  49.                 .LineStyle = excel.XlLineStyle.xlContinuous
  50.                 .ColorIndex = 1 'black
  51.                 .TintAndShade = 0
  52.                 .Weight = excel.XlBorderWeight.xlMedium
  53.             End With
  54.             With range.Borders(excel.XlBordersIndex.xlEdgeBottom)
  55.                 .LineStyle = excel.XlLineStyle.xlContinuous
  56.                 .ColorIndex = 1 'black
  57.                 .TintAndShade = 0
  58.                 .Weight = excel.XlBorderWeight.xlMedium
  59.             End With
  60.             With range.Borders(excel.XlBordersIndex.xlEdgeRight)
  61.                 .LineStyle = excel.XlLineStyle.xlContinuous
  62.                 .ColorIndex = 1 'black
  63.                 .TintAndShade = 0
  64.                 .Weight = excel.XlBorderWeight.xlMedium
  65.             End With
  66.             With range.Borders(excel.XlBordersIndex.xlInsideVertical)
  67.                 .LineStyle = excel.XlLineStyle.xlContinuous
  68.                 .ColorIndex = 1 'black
  69.                 .TintAndShade = 0
  70.                 .Weight = excel.XlBorderWeight.xlThin
  71.             End With
  72.             With range.Borders(excel.XlBordersIndex.xlInsideHorizontal)
  73.                 .LineStyle = excel.XlLineStyle.xlContinuous
  74.                 .ColorIndex = 1 'black
  75.                 .TintAndShade = 0
  76.                 .Weight = excel.XlBorderWeight.xlThin
  77.             End With
  78.  
  79.             range.Columns.AutoFit() 'auto-size the columns so the columns look nice
  80.             xlWorkBook.SaveAs(Application.StartupPath & "\" & "TEST" & ".xlsx") 'save our workbook
  81.  
  82.             'releasing object references
  83.             xlWorkBook = Nothing
  84.             xlWorkBook = Nothing
  85.             xlApp.Quit()
  86.             xlApp = Nothing
  87.  
  88.             DataGridView1.Rows.Clear() 'clear the log
  89.  
  90.         ElseIf File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then
  91.             MsgBox("bump, it exists")
  92.             Dim xlApp As excel.Application = New excel.Application
  93.             Dim xlWorkBook As excel.Workbook = xlApp.Workbooks.Open(Application.StartupPath & "\" & "TEST" & ".xlsx")
  94.             Dim xlWorkSheet As excel.Worksheet
  95.             'Dim misValue As Object = System.Reflection.Missing.Value
  96.  
  97.             'xlWorkBook = xlApp.Workbooks.Add(misValue)
  98.             xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet)
  99.  
  100.             Dim headers = (From ch In dgv.Columns _
  101.                            Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
  102.                            Select header.Value).ToArray()
  103.             Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString)
  104.  
  105.             Dim items() = (From r In dgv.Rows _
  106.                     Let row = DirectCast(r, DataGridViewRow) _
  107.                     Where Not row.IsNewRow _
  108.                     Select (From cell In row.Cells _
  109.                         Let c = DirectCast(cell, DataGridViewCell) _
  110.                         Select c.Value).ToArray()).ToArray()
  111.  
  112.             Dim table As String = String.Empty
  113.  
  114.             For Each a In items
  115.                 Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
  116.                 table &= String.Join(vbTab, t) & Environment.NewLine
  117.             Next
  118.  
  119.             table = table.TrimEnd(CChar(Environment.NewLine))
  120.             Clipboard.SetText(table)
  121.  
  122.             Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
  123.             Dim range As excel.Range = xlWorkSheet.Range("B" & (xlWorkSheet.UsedRange.Rows.Count + 1).ToString & ":" & alphabet(headerText.Length) & (xlWorkSheet.UsedRange.Rows.Count + items.Length).ToString)
  124.  
  125.             range.Select() '? should have this or not? is it over-writing?
  126.             xlWorkSheet.Paste()
  127.  
  128.             range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone
  129.             range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone
  130.  
  131.             With range.Borders(excel.XlBordersIndex.xlEdgeLeft)
  132.                 .LineStyle = excel.XlLineStyle.xlContinuous
  133.                 .ColorIndex = 1 'black
  134.                 .TintAndShade = 0
  135.                 .Weight = excel.XlBorderWeight.xlMedium
  136.             End With
  137.             With range.Borders(excel.XlBordersIndex.xlEdgeTop)
  138.                 .LineStyle = excel.XlLineStyle.xlContinuous
  139.                 .ColorIndex = 1 'black
  140.                 .TintAndShade = 0
  141.                 .Weight = excel.XlBorderWeight.xlMedium
  142.             End With
  143.             With range.Borders(excel.XlBordersIndex.xlEdgeBottom)
  144.                 .LineStyle = excel.XlLineStyle.xlContinuous
  145.                 .ColorIndex = 1 'black
  146.                 .TintAndShade = 0
  147.                 .Weight = excel.XlBorderWeight.xlMedium
  148.             End With
  149.             With range.Borders(excel.XlBordersIndex.xlEdgeRight)
  150.                 .LineStyle = excel.XlLineStyle.xlContinuous
  151.                 .ColorIndex = 1 'black
  152.                 .TintAndShade = 0
  153.                 .Weight = excel.XlBorderWeight.xlMedium
  154.             End With
  155.             With range.Borders(excel.XlBordersIndex.xlInsideVertical)
  156.                 .LineStyle = excel.XlLineStyle.xlContinuous
  157.                 .ColorIndex = 1 'black
  158.                 .TintAndShade = 0
  159.                 .Weight = excel.XlBorderWeight.xlThin
  160.             End With
  161.             With range.Borders(excel.XlBordersIndex.xlInsideHorizontal)
  162.                 .LineStyle = excel.XlLineStyle.xlContinuous
  163.                 .ColorIndex = 1 'black
  164.                 .TintAndShade = 0
  165.                 .Weight = excel.XlBorderWeight.xlThin
  166.             End With
  167.  
  168.             range.Columns.AutoFit() 'auto-size the columns so the excel columns look nice
  169.             xlWorkBook.Save() 'save our workbook
  170.  
  171.             'releasing object references
  172.             xlWorkBook = Nothing
  173.             xlWorkBook = Nothing
  174.             xlApp.Quit()
  175.             xlApp = Nothing
  176.  
  177.             DataGridView1.Rows.Clear() 'clear the log
  178.         Else
  179.             Debug.Print("What the hell? Either exists or not lol")
  180.         End If
  181.     End Sub