Solved

Problem to scan all sheets

Posted on 2016-07-28
3
132 Views
Last Modified: 2016-07-31
Hi,
Does 2nd line below scan through all Worksheet within Excel file? If not, how to adjust it to scan through all Worksheets, except one specific sheet with a given name?
                    Excel.Worksheet Worksheet0;
                        ...
                        foreach (Excel.Worksheet sheet in Book0.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == Worksheet0))

Open in new window

0
Comment
Question by:HuaMinChen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 41734061
First of all, you need to compare name with name:
                    Excel.Worksheet Worksheet0;
                        ...
                        foreach (Excel.Worksheet sheet in Book0.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == Worksheet0.Name))

Open in new window

The code above will select one worksheet with the name equal to Worksheet0.Name.
If you want to select worksheets with the name not equal to Worksheet0.Name then use != operator:
                    Excel.Worksheet Worksheet0;
                        ...
                        foreach (Excel.Worksheet sheet in Book0.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name != Worksheet0.Name))

Open in new window

0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 41734066
Many thanks. How to adjust last line below, as I want to paste to the sheet with name - "Sheet Overall"?
                        foreach (Excel.Worksheet sheet in Book0.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name != "Sheet Overall"))
                        {
                            DocumentFormat.OpenXml.Spreadsheet.Sheets sheetData = Book0.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
                            Excel.Range usedRange = sheet.UsedRange;

                            string cellReference = "^^^^" + sheet.Name + "^^^^";

                            foreach (Excel.Range row in usedRange.Rows)
                            {
                                RowID++;
                                row.Copy();
                                Worksheet0.Cells[RowID, 1].PasteSpecial(-4104);
                        

Open in new window

0
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 41734069
Use == operator:
                        foreach (Excel.Worksheet sheet in Book0.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Sheet Overall"))

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question