使用 vb.net 在 Excel 表中选择范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15280939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:43:26 来源:igfitidea点击:
Range selection in excel sheet using vb.net
提问by coder
This is my code:
这是我的代码:
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Excel files (*.xls)|*.xls"
saveFileDialog1.Title = "Save File"
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Try
Dim ExcelApp As New Microsoft.Office.Interop.Excel.Application()
ExcelApp.Application.Workbooks.Add(Type.Missing)
ExcelApp.Cells.HorizontalAlignment = XlHAlign.xlHAlignLeft
' Change properties of the Workbook
ExcelApp.Columns.ColumnWidth = 15
' Storing header part in Excel
For i As Integer = 1 To DataGridView1.Columns.Count
ExcelApp.Cells(1, i) = DataGridView1.Columns(i - 1).HeaderText
Next
' Storing Each row and column value to excel sheet
For i As Integer = 0 To DataGridView1.Rows.Count - 2
For j As Integer = 0 To DataGridView1.Columns.Count - 1
ExcelApp.Cells(i + 2, j + 1) = DataGridView1.Rows(i).Cells(j).Value.ToString()
Next
Next
Dim Destinationpath As String = saveFileDialog1.FileName
ExcelApp.ActiveWorkbook.SaveAs(Destinationpath)
ExcelApp.ActiveWorkbook.Saved = True
ExcelApp.Quit()
MsgBox("Record exported successfully", MsgBoxStyle.Information)
Catch
MsgBox("It is being used by another process.Please close it and retry.", MsgBoxStyle.Critical)
End Try
Else
End If
Now how do I select the range using the above code which is populated in an excel sheet.
现在如何使用上面填充在 Excel 工作表中的代码来选择范围。
回答by coder
Found the answer:
找到答案:
Dim lastrow As Range = ExcelApp.Rows.End(XlDirection.xlDown)
Dim findme As Range = ExcelApp.Range("A1:E" & lastrow.Row)
MsgBox("A1:E" & lastrow.Row)