1
我正在複製gridview值,該值超過20列,在剪貼板中有超過500行,並將該值複製到excel文件中。在vb.net的現有excel文件中追加數據
Dim excelApp As New Excel.Application
Dim aWorkbook As Excel.Workbook
aWorkbook = excelApp.Workbooks.Add()
excelApp.Visible = False
Dim strWorkSheetName As String = "MyData"
Dim aWorkSheet As Excel.Worksheet
aWorkSheet = aWorkbook.Sheets.Add()
aWorkSheet.name = TextBox1.Text
aWorkSheet.Activate()
Dim intRows As Integer = DataGridView1.SelectedRows.Count
Dim strRangeString As String = "C4:G" & intRows.ToString
'Open the existing Template
Dim _Tpath As String = Application.StartupPath & "\Template.xlsx"
aWorkbook = excelApp.Workbooks.Add(_Tpath)
Dim alpha As String
Dim num As Integer
Dim cell As String
Dim count As Integer = 0
For Each col As DataGridViewColumn In DataGridView1.Columns
alpha = Convert.ToChar(65 + count)
num = Convert.ToInt32(7)
cell = Convert.ToString(alpha & "7")
aWorkSheet.Range(cell).Value = col.HeaderText.ToString
count = count + 1
Next
For row As Integer = 0 To dt2.Rows.Count - 1
For col As Integer = 0 To dt2.Columns.Count - 1
alpha = Convert.ToChar(65 + col)
num = Convert.ToInt32(8 + row)
cell = Convert.ToString(alpha & num)
aWorkSheet.Range(cell).Value = dt2(row)(col).ToString
Next
Next
,但現在我想的是剪貼板粘貼值到現有的Excel文件(作爲模板)上的特定位置從第7行和上述假設。我已經寫了一些代碼,其中值逐個粘貼,並且花費很多時間。有沒有其他的方法來將gridview值粘貼到現有的excel文件中?