如何在Excel工作表中隐藏列 [英] How to hide column in excel sheet

查看:123
本文介绍了如何在Excel工作表中隐藏列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在excel表中隐藏列...我尝试了以下代码..请帮助我



我有什么尝试过:



I am not able to hide column in an excel sheet...i tried the below code..please help me out

What I have tried:

Public Function setHeaderColumns(ByVal pck As ExcelPackage, ByVal columns As IEnumerable(Of ExcelEntity), ByVal WorksheetName As String) As ExcelPackage
            'create worksheet object
            Dim ws As ExcelWorksheet
            If (pck.Workbook.Worksheets.Count > 0) Then
                ws = pck.Workbook.Worksheets(WorksheetName)
            Else
                ws = pck.Workbook.Worksheets.Add(WorksheetName)
            End If
            Dim ColumnIndex As Integer = 1
            'set the name, color or hide the columns in excel
            For Each col As ExcelEntity In columns
                Dim excelColumn As ExcelColumn = ws.Column(ColumnIndex)
                ws.Column(ColumnIndex).Hidden = Not col.isActive

                'excelColumn.Hidden = Not col.isActive
                ws.Cells(1, ColumnIndex).Value = col.fieldText
                ws.Cells(1, ColumnIndex).Style.Font.Bold = True
                ws.Cells(1, ColumnIndex).Style.Font.Color.SetColor(Color.Black)
                If col.isRequired Then
                    ws.Cells(1, ColumnIndex).Style.Font.Color.SetColor(Color.Red)
                End If
                ws.Column(ColumnIndex).Width = 35
                ColumnIndex += 1
            Next
            Return pck
        End Function

推荐答案

尝试:



Try:

ws.Column(ColumnIndex).EntireColumn.Hidden = Not col.isActive





如果这不能直接起作用,你可能必须先创建一个选择并将.EntireColumn.Hidden应用于选择,这是VBA奇怪的行为之一。



在Excel中查找任务代码的最简单方法是记录执行任务的宏,然后查看生成的代码并将其应用到代码中。



If this does not work directly you might have to create a selection first and apply the .EntireColumn.Hidden to the selection, one of the odd behaviours of VBA.

Easiest way to find code for tasks in Excel is to record a macro executing the task then look at the code generated and apply it in your code.


这篇关于如何在Excel工作表中隐藏列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆