How to create slicers using open xml in C#

Caroll Susairaj 1 Reputation point
2021-06-15T12:55:48.86+00:00

I could able to create a pivot table using openxml C# but unable to find a way on linking slicers using C#. Can some one help me on this.

Microsoft 365 and Office | Development | Other
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Nancy Wang 116 Reputation points Microsoft Employee
    2021-06-15T17:15:52.093+00:00

    Hi @Caroll Susairaj ! Your question doesn't appear to be related to Office Scripts - could you remove the 'office-scripts-excel-dev' tag? Thank you so much!

    Warmly,
    Nancy

    0 comments No comments

  2. Timon Yang-MSFT 9,606 Reputation points
    2021-06-16T06:27:20.933+00:00

    Could you please show some existing code? Let us move on based on your code.

    Besides, can we use other packages?

    I often use Microsoft.Office.Interop.Excel, here are some codes written in this package:

                Application application = new Application();  
                Workbook oWorkbook = application.Workbooks.Open(@"C:\...\1.xlsx");  
                try  
                {  
                    Worksheet oWorksheet = (Worksheet)oWorkbook.ActiveSheet;  
      
                    SlicerCaches oSlicerCaches = oWorkbook.SlicerCaches;  
      
                    SlicerCache slicerCache = oSlicerCaches.Add2(oWorksheet.ListObjects["Table1"], "Column1", "mSlicer");  
                slicerCache.Slicers.Add(oWorksheet, System.Reflection.Missing.Value, "Column1", "testSlicer", 100,  
                    400, 150, 200);  
                }  
                catch (Exception e)  
                {  
                    Console.WriteLine(e);  
                }  
                finally   
                {  
                    oWorkbook.Save();  
                    oWorkbook.Close();  
                    application.Quit();  
                }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.