Wiki

Clone wiki

Aspose for OpenXML / Search Data in Spreadsheet

Search Data in Spreadsheet #

using OpenXML SDK

#!c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(@"C:\Users\user\Desktop\Book1.xlsx", true))
            {
                Sheet sheet = document.WorkbookPart.Workbook.Descendants<Sheet>().First<Sheet>();
                Worksheet worksheet = ((WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id)).Worksheet;
                IEnumerable<Row> allRows = worksheet.GetFirstChild<SheetData>().Descendants<Row>();
                foreach (Row currentRow in allRows)
                {
                    IEnumerable<Cell> allCells = currentRow.Descendants<Cell>();
                    foreach (Cell currentCell in allCells)
                    {
                        CellValue currentCellValue = currentCell.GetFirstChild<CellValue>();
                        string data = null;
                        if (currentCell.DataType != null)
                        {
                            if (currentCell.DataType == CellValues.SharedString) // cell has a cell value that is a string, thus, stored else where
                            {
                                data = document.WorkbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault().SharedStringTable.ElementAt(int.Parse(currentCellValue.Text)).InnerText;
                            }
                        }
                        else
                        {
                            data = currentCellValue.Text;
                        }
                        Console.WriteLine(data);

                        /* 
                            your code here

                                if(data.contains("myText"))
                                    doSomething();

                        */

                    }
                }
            }           
        }
    }
}

using Aspose SDK

#!c#
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Cells;

namespace SearchData
{
    class Program
    {
        static void Main(string[] args)
        {
            string projectFiles = Path.GetFullPath("../../Files/");

            Workbook workbook = new Workbook(projectFiles + "TestBook.xlsx");
            //Accessing the first worksheet in the Excel file
            Worksheet worksheet = workbook.Worksheets[0];

            //Finding the cell containing the specified formula
            Cell cell = worksheet.Cells.FindFormula("=SUM(A2:A5)", null);

            //Printing the name of the cell found after searching worksheet
            System.Console.WriteLine("Name of the cell containing formula: " + cell.Name);

            System.Console.ReadKey();
        }
    }
}
Source: https://bitbucket.org/asposemarketplace/aspose-for-openxml/src/ffec3cde9d87/Aspose.Cells/SearchData/?at=master

Updated