简体   繁体   中英

How to set Excel Workbook Visibility in C#

I am trying to use OpenXML to programmatically set an Excel workbook to visible, but am fairly new to C# and am not sure how to do that.

We are using a 3rd party application that stores Office documents and I'm tasked with reporting. One of the many strange things it does is compresses documents before saving them and sets Excel Workbook visibility to false in the database record.

According to the documentation there is the function that should do that:

public DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.Spreadsheet.VisibilityValues> Visibility { get; set; }

How do I translate that into a working C# example? I don't have much so far:

using (MemoryStream mem = new MemoryStream())
{
    mem.Write(byteArray, 0, (int)byteArray.Length);

    using (SpreadsheetDocument rptTemplate = SpreadsheetDocument.Open(mem, true))
    {   // the next line doesn't work
        rptTemplate.WorkbookPart.Workbook.WorkbookProperties.SetAttribute(VisibilityValues = visible)
        rptTemplate.WorkbookPart.Workbook.Save();
        rptTemplate.SaveAs(filePathName);
    }
}

first of all you need to get WorkbookView of your workbook, so

  var workBookView = workbookpart.Workbook.Descendants<WorkbookView>().FirstOrDefault();

and then you can set desired visibility :

   workBookView.Visibility = VisibilityValues.Hidden;

if you are creating spreadsheet document you should create workBookView before you can use it,like below:

   spreadsheetDocument.WorkbookPart.Workbook.Append(new BookViews(new WorkbookView()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM