老师 当前运行错误如下:
---------------------------
版本:2022.1.30.2
---------------------------
代码执行出错,错误信息:
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 Foxtable.XLS.Style.set_Locked(Boolean value)
在 UserCode.Test()
---------------------------
确定
---------------------------
当前代码如下:有没有办法避免红色部分代码出错并提高效率呢?
Dim App As New MSExcel.Application
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open("c:\data\文书编号.xls")
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
Dim Rg As MSExcel.Range = Ws.UsedRange
Dim ary = rg.value
Dim Book As New XLS.Book("c:\data\文书编号.xls") '定义一个Excel工作簿
Dim Sheet As XLS.Sheet = Book.Sheets(0) '引用工作簿的第一个工作表
Dim Style As XLS.Style = Book.NewStyle() '定义新样式
Dim bb2 As Integer=Sheet.Rows.Count
Dim cc2 As Integer=Sheet.cols.Count
output.show(bb2 & "||||" & cc2)
For n As Integer = 2 To bb2
'禁止显示
Sheet.Cols(0).Visible= False
Sheet.Cols(1).Visible= False
Sheet.Cols(2).Visible= False
Sheet.Cols(3).Visible= False
Sheet.Cols(4).Visible= False
Sheet.Cols(5).Visible= False
Sheet.Cols(8).Visible= False
Sheet.Cols(9).Visible= False
Sheet.Cols(10).Visible= False
Sheet.Cols(16).Visible= False
'单元格禁止编辑
Sheet(n,6).Style.Locked=True
Sheet(n,7).Style.Locked=True
Sheet(n,11).Style.Locked=True
Sheet(n,12).Style.Locked=True
Sheet(n,13).Style.Locked=True
For i As Integer = 1 To cc2
Sheet(n,i).Style.WordWrap=True '单元格自动换行'
If ary(n,8) = "1" Then
Sheet.Rows(n).Visible= True '当等于1时候 显示当前行'
Else
Sheet.Rows(n).Visible= False '当不等于1的时候 隐藏当前行'
End If
Next
'设定列宽'
Sheet.Cols(6).Width = 150
Sheet.Cols(7).Width = 150
Sheet.Cols(11).Width = 100
Sheet.Cols(12).Width = 100
Sheet.Cols(13).Width = 150
Sheet.Cols(14).Width = 300
Sheet.Cols(15).Width = 150
book.Save("c:\data\文书编号1.xls")
Next
app.quit
[此贴子已经被作者于2022/9/29 15:45:35编辑过]