簡體   English   中英

如何使用VB.Net解鎖Excel中的列?

[英]How to unlock columns in Excel using VB.Net?

早上好

我在VB.Net中有一個程序,可以將Datagridview中的文件導出到Excel文件中,它看起來像這樣。

在此處輸入圖片說明

我的目標是如何鎖定一些列? 根據上面的圖片? 鎖定除黃色以外的所有列? 我的意思是除黃色外的所有列均不可編輯。

這是我導出Excel中的代碼

  Try
            If DataGridView1.Rows.Count = 0 Then
                MsgBox("Nothing to Export")
            Else
                Dim ExcelApp As Object, ExcelBook As Object
                Dim ExcelSheet As Object
                Dim i As Integer
                Dim J As Integer
                Dim rowIndex As Integer = 1
                Dim total As Double = 0
                Dim indexTotal As Integer

                ExcelApp = CreateObject("Excel.Application")
                ExcelBook = ExcelApp.WorkBooks.Add
                ExcelSheet = ExcelBook.WorkSheets(1)
                With ExcelSheet
                    rowIndex += 2
                    For Each column As DataGridViewColumn In DataGridView1.Columns
                        .cells(rowIndex, column.Index + 1) = column.HeaderText
                    Next
                    .Range(.Cells(rowIndex, 1), .Cells(rowIndex, DataGridView1.Columns.Count)).Font.Bold = True
                    rowIndex += 1
                    For i = 0 To Me.DataGridView1.RowCount - 1
                        .cells(rowIndex, 1) = Me.DataGridView1.Rows(i).Cells("ItemCode").Value
                        For J = 1 To DataGridView1.Columns.Count - 1
                            If IsNumeric(DataGridView1.Rows(i).Cells(J).Value) Then
                                .cells(rowIndex, J + 1).NumberFormat = "#,##0.00"
                                .cells(rowIndex, J + 1) = DataGridView1.Rows(i).Cells(J).Value
                            Else
                                .cells(rowIndex, J + 1) = DataGridView1.Rows(i).Cells(J).Value
                            End If
                            'You can test also by index for example : if J = indexofTotalColumn then
                            If DataGridView1.Columns(J).Name = "Total" Then
                                total += DataGridView1.Rows(i).Cells(J).Value
                                indexTotal = J
                            End If
                        Next
                        rowIndex += 1
                        .Columns("A:Z").EntireColumn.AutoFit()
                        .Columns("L").ColumnWidth = 0
                        .cells(5).Locked = False
                    Next
                    .Protect("fakepwd")


                End With


                ExcelApp.Visible = True
                ExcelSheet = Nothing
                ExcelBook = Nothing
                ExcelApp = Nothing
            End If
        Catch
        End Try

TYSM尋求幫助

將單元格的Locked屬性設置為false,其中J + 1是所需的列號。

例如解鎖第5列:

For J = 1 To DataGridView1.Columns.Count - 1
If J=5 then
 .cells(rowIndex, J + 1).Locked=False
End if
 If IsNumeric(DataGridView1.Rows(i).Cells(J).Value) Then
..........

在代碼中,在表中完成數據填充后,保護表

Next
.Protect ("fakepwd")
End With

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM