2012-08-12 16 views
5

Ho un modello di Excel con vari fogli a cui sto scaricando i dati recuperati da SQL Server usando OpenXML, C#. Dopo aver finito con il dumping dei dati, ho bisogno di nascondere alcuni dei fogli in base alle condizioni. Non ho trovato alcun pezzo di codice per nascondere un particolare foglio usando C# OpenXML.Come nascondere un foglio in Excel usando OpenXML C#?

Ho provato quanto segue ma i fogli non sono stati nascosti.

byte[] byteArray = File.ReadAllBytes("D:\\rptTemplate.xlsx"); 
using (MemoryStream mem = new MemoryStream()) 
{ 
mem.Write(byteArray, 0, (int)byteArray.Length); 
using (SpreadsheetDocument rptTemplate = SpreadsheetDocument.Open(mem, true)) 
{ 
    foreach (OpenXmlElement oxe in (rptTemplate.WorkbookPart.Workbook.Sheets).ChildElements) 
    { 
    if(((DocumentFormat.OpenXml.Spreadsheet.Sheet)(oxe)).Name == "ABC") 
     ((DocumentFormat.OpenXml.Spreadsheet.Sheet)(oxe)).State = SheetStateValues.Hidden; 
    } 
    rptTemplate.WorkbookPart.Workbook.Save(); 
} 
} 

Richiedi assistenza su questo.

Grazie.

risposta

8

è necessario impostare la proprietà ActiveTab del WorkbookView classe ad un indice che è diverso dall'indice del foglio di lavoro che si desidera nascondere.

Quindi, ad esempio, se si desidera nascondere il primo foglio di lavoro (foglio di lavoro con indice 0) nel file excel, impostare la proprietà ActiveTab all'indice del foglio di lavoro visibile successivo.

Ecco un piccolo esempio di codice (basato sul codice che hai fornito):

static void Main(string[] args) 
{ 
    byte[] byteArray = File.ReadAllBytes("D:\\rptTemplate.xlsx"); 

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

    using (SpreadsheetDocument rptTemplate = SpreadsheetDocument.Open(mem, true)) 
    { 
     foreach (OpenXmlElement oxe in (rptTemplate.WorkbookPart.Workbook.Sheets).ChildElements) 
     { 
     if(((DocumentFormat.OpenXml.Spreadsheet.Sheet)(oxe)).Name == "ABC") 
     { 
      ((DocumentFormat.OpenXml.Spreadsheet.Sheet)(oxe)).State = SheetStateValues.Hidden; 

      WorkbookView wv = rptTemplate.WorkbookPart.Workbook.BookViews.ChildElements.First<WorkbookView>(); 

      if (wv != null) 
      { 
      wv.ActiveTab = GetIndexOfFirstVisibleSheet(rptTemplate.WorkbookPart.Workbook.Sheets); 
      }      
     } 
     } 
     rptTemplate.WorkbookPart.Workbook.Save(); 
    } 
    } 
} 

private static uint GetIndexOfFirstVisibleSheet(Sheets sheets) 
{ 
    uint index = 0; 
    foreach (Sheet currentSheet in sheets.Descendants<Sheet>()) 
    { 
    if (currentSheet.State == null || currentSheet.State.Value == SheetStateValues.Visible) 
    { 
     return index; 
    } 
    index++; 
    } 
    throw new Exception("No visible sheet found."); 
} 
+0

Grazie mille. Questo ha funzionato. :-) – Raghu

+1

Ho provato lo stesso codice, passato attraverso il debug di ogni riga, tutto funziona bene. Ma dopo l'esecuzione del codice. Quando apro il foglio di file Excel non ho nascosto. Mi potete aiutare? –

+0

@NarendraKumar: È molto difficile aiutare senza conoscere l'esatta struttura del documento Excel e i nomi dei fogli Excel. Quanti fogli hai? – Hans

Problemi correlati