2017-12-13 8 views
3

最近、私はC#にVBAからプロジェクトを変換されていますが、私はVBAで.Hidden()、.Locked()、および.PROTECT()C#が非表示とロックの行をエクセル

で問題全体を実行しています私は非表示にすることができます(行を強調表示する) - >ロック - >保護する場合は、

VBAバージョンと同じ結果(行を表示することはできません)を生成するためにC#バージョンを書き込む必要がありますか?

私は結果を再現するこれらの短いスニペットにコードを単純化しました。 両方のバージョンは、新しいブックを作成し、セルを変更し、行を非表示にロックし、ブックを保存/閉じるします。

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のバージョン:あなたのProtect方法で

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 

答えて

0

、フォーマット行引数はfalseなくtrueに設定する必要があります。

+0

私は勘違いしていないと思いますが、強調表示などは(たとえあなたがそうしていても)フォーマットの行がハイライトなどのためにあると思っていました。ありがとうございました – Algorythm44

+0

@ Algorythm44行全体または列全体)、おそらくformatセルの引数にあります。 – Magnetron

0

Excelシートを保護して非表示にするコードは次のとおりです。 は

using System; 
using System.Data; 
using Microsoft.CSharp; 
using System.Collections; 
using Excel=Microsoft.Office.Interop.Excel; 

toHideこのコードで、エクセルアプリケーション、ファイルパスは、以下のコードを使用してシートを非表示に続いてパスワード

string FilePath = @"C:\Filename.xlsx"; 
string Password = "12345"; 
Excel.Application ExcelApp = new Excel.Application(); // Initialize Excel Application 
ExcelApp.DisplayAlerts = false;    
Excel.Workbook WB = ExcelApp.Workbooks.Open(FilePath); // Initialize Excel Workbook 

を含む文字列変数である初期化し、以下のように必要な名前空間を使用しますArraylistには、非表示にする必要があるシートのリストが含まれています。

foreach (Excel.Worksheet Worksheet in WB.Worksheets) 
     { 
     if (toHide.Contains(Worksheet.Name)) 
      { 
       ((Excel.Worksheet)WB.Worksheets[Worksheet.Name]).Visible = Excel.XlSheetVisibility.xlSheetHidden; 
      } 
     } 

シートを保護するには、ここにコードがあります。 toProtectここに保護する必要があるシート名を含むArraylistがいます。

ExcelApp.Visible = true; 
     foreach (Excel.Worksheet Worksheet in WB.Worksheets) 
     { 
      if (toProtect.Contains(Worksheet.Name)) 
      { 
       ((Excel.Worksheet)WB.Worksheets[Worksheet.Name]).Protect(Password); 
      } 
     } 
     //WB.Save(); 
     ExcelApp.Visible = false; 

これが役立つかどうかお知らせください。

+0

あなたはその質問を読んだことがありますか?私はすでに、私の期待通りに動作していなかった私のサンプルコードに.Protect()メソッドを既に持っています。そして、引数の一つが混ざっていることが既に指摘されています。 (あなたの例では、私が必要とする間違った値をデフォルトとする引数もありません) – Algorythm44

関連する問題