C# excel 隐藏和锁定行

标签 c# vba excel

最近我一直在将一个项目从 VBA 转换为 C#,但我遇到了 .Hidden()、.Locked() 和 .Protect() 的问题

在 VBA 实现中,如果我 hide(rows)->lock->protect 然后我不能取消隐藏行(按预期),但在 C# 实现中,如果我 hide(rows)->lock->protect 行可以取消隐藏(突出显示行,右键单击,取消隐藏)

我是否遗漏了什么,或者是否需要编写 C# 版本以产生与 VBA 版本相同的结果(行不能取消隐藏)的不同方式?

我已将代码简化为这些重现结果的简短片段。 两个版本都创建新工作簿、修改单元格、隐藏锁定保护行以及保存/关闭工作簿。

C#版本:

using Excel = Microsoft.Office.Interop.Excel;
...

private void button1_Click(object sender, EventArgs e)
{

    Excel.Application ex = new Excel.Application();
    Excel.Workbooks Books = ex.Workbooks;


    //create and save the output workbook (so only .save() needs to be called later)
    Excel.Workbook OutputBook = Books.Add();
    OutputBook.SaveAs("C:\\TestingFolder\\Outputbook.xlsm", Excel.XlFileFormat.xlOpenXMLWorkbookMacroEnabled);

    //write secret stuff
    OutputBook.Sheets[1].Cells[15,15] = "Stuff";

    //hide and lock rows around secret stuff
    OutputBook.Sheets[1].Range["10:20"].EntireRow.Hidden = true;
    OutputBook.Sheets[1].Range["10:20"].EntireRow.Locked = true;


    //protect the sheet with a bad password
    OutputBook.Sheets[1].Protect(
                                    "SomePassword123",//password
                                    false,  //drawing objects
                                    true,   //Contents
                                    false,  //scenarios
                                    false,  //user interface
                                    true,   //format cells
                                    true,   //format columns
                                    true,   //format rows
                                    false,  //insert columns
                                    false,  //insert rows
                                    true,   //insert hyperlinks
                                    false,  //delete columns
                                    false,  //delete rows
                                    true,   //allow sorting
                                    true,   //allow filtering
                                    true    //allow pivot tables
                                );


    //save and close output workbook
    OutputBook.Save();
    OutputBook.Close(false);


    //-----general cleanup start-----
    Books.Close();
    ex.Quit();

    System.Runtime.InteropServices.Marshal.ReleaseComObject(OutputBook);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(Books);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(ex);

    OutputBook = null;
    Books = null;
    ex = null;

    GC.Collect();
    //-----general cleanup end-----


    //show message that the task completed
    MessageBox.Show("done");

}

和 VBA 版本:

Private Sub CommandButton1_Click()

    'create and save the output workbook (so only .save() needs to be called later)
    Dim OutputBook As Workbook
    Set OutputBook = Workbooks.Add
    Call OutputBook.SaveAs("C:\TestingFolder\Outputbook.xlsm", ThisWorkbook.FileFormat)

    'write secret stuff
    OutputBook.Sheets(1).Cells(15, 15) = "Stuff"

    'hide and lock rows around secret stuff
    OutputBook.Sheets(1).Range("10:20").EntireRow.Hidden = True
    OutputBook.Sheets(1).Range("10:20").EntireRow.Locked = True

    'protect the sheet with a bad password
    OutputBook.Sheets(1).Protect Password:="SomePassword123", _
                                DrawingObjects:=False, _
                                Contents:=True, _
                                Scenarios:=False, _
                                AllowFormattingCells:=True, _
                                AllowInsertingHyperlinks:=True, _
                                AllowSorting:=True, _
                                AllowFiltering:=True, _
                                AllowUsingPivotTables:=True

    'save and close output workbook
    Call OutputBook.Save
    Call OutputBook.Close

    'show message that the task completed
    MsgBox "done"

End Sub

最佳答案

在您的 Protect 方法中,格式行参数必须设置为 false 而不是 true

关于C# excel 隐藏和锁定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47798913/

相关文章:

c# - 如何重新启动 WPF 应用程序?

c# - 在图片框内动态创建点/正方形

excel - 如何使用 On Error 打破 VBA 中的循环?

c# - 存储过程需要一个我已经传入的参数

c# - 分页上下限

vba - 如何在 VBA 中动态调整数组的大小?我不断收到下标和索引问题

excel - 将存储在变量中的参数从Excel宏传递到bat文件

vba - 表保护: UserInterFaceOnly gone

javascript - 在office.js中设置excel的条件格式

python - 将文本从一个单元格复制到另一个单元格而不删除原始内容python