Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem to scan all sheets

Posted on 2016-07-28
3
Medium Priority
?
178 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 11

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 1500 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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.

636 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