Solved

Problem to scan all sheets

Posted on 2016-07-28
3
82 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
  • 2
3 Comments
 
LVL 24

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 24

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now