范围在.Select()不工作在xlApp.ActiveWindow.FreezePanes

我只想selectA1到A3来冻结窗格。 但是xlApp.ActiveWindow.FreezePanes冻结A1到A10。 我正在试验3个小时这段代码是3个小时:

xlApp.Range("A1:A3").Select() 

但A1至A10仍然是冻结的细胞。 我只是想冻结A1到A3。 这可能吗? 我使用Visual Studio 2012和Excel 2013

这是我的代码:

 Imports System.Data.OleDb Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'AccessdbtestDataSet.country' table. You can move, or remove it, as needed. Me.CountryTableAdapter.Fill(Me.AccessdbtestDataSet.country) Dim con As New OleDbConnection Dim query As String = "SELECT * FROM country" con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=accessdbtest.accdb" con.Open() Dim dt As New DataTable Dim ds As New DataSet ds.Tables.Add(dt) Dim da As New OleDbDataAdapter(query, con) da.Fill(dt) DataGridView1.DataSource = ds.Tables(0) con.Close() End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Dim xlApp As Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value Dim i As Integer Dim j As Integer Dim xlCenter As String = "center" xlApp = New Excel.ApplicationClass xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = xlWorkBook.Sheets("sheet1") //Merging cells xlWorkSheet.Range("A1:D1").MergeCells = True xlWorkSheet.Range("A2:D2").MergeCells = True xlWorkSheet.Range("A3:D3").MergeCells = True //Assigning text to the merge cells xlWorkSheet.Cells(1, 1) = "Republic of the Philippines" xlWorkSheet.Cells(2, 1) = "NCR" xlWorkSheet.Cells(3, 1) = "Manila" //Put the text in the center of the merge cells xlWorkSheet.Range("A1:A1").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter xlWorkSheet.Range("A2:A2").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter xlWorkSheet.Range("A3:A3").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter For i = 5 To DataGridView1.RowCount - 2 For j = 0 To DataGridView1.ColumnCount - 1 xlWorkSheet.Cells(i + 1, j + 1) = _ DataGridView1(j, i - 3).Value.ToString() Next Next //This line of code is not working <-------------------------<| xlApp.Range("A1:A3").Select() xlApp.ActiveWindow.FreezePanes = True xlWorkSheet.SaveAs("C:\excel\vbexcel.xlsx") xlWorkBook.Close() xlApp.Quit() releaseObject(xlApp) releaseObject(xlWorkBook) releaseObject(xlWorkSheet) MsgBox("You can find the file C:\vbexcel.xlsx") End Sub Private Sub releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub End Class 

 xlApp.Range("A4").Select 

然后应用冻结窗格。