Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

Retrieve Excel cell values from every row and cell in a WorkSheet

$
0
0

Good Afternoon,

I am utilizing the Open XML 2.0 to retrieve cell values with in a workbook. In my code, I am attempting to retrieve the cell values from every row and every column within the worksheet that contains a value. The data should be displayed on the console.:


Here is the code:

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

namespace ReadingExcel
{
    class RetrieveListOfExcelValues
    {
        static void Main(string[] args)
        {
            String fileName = @"C:\Users\Documents\TestReadingExcel.xlsx";
             Comment one of the following lines to test the method separately.
            ReadExcelFileDOM(fileName);     DOM


        }end Main

        static void ReadExcelFileDOM(string fileName)
        {   
             Open the spreadsheet document for read-only access.
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
            {
               
               
                 Retrieve a reference to the workbook part.
                WorkbookPart wbPart = document.WorkbookPart;
                
                Worksheet name that is the the workbook
                string sheetName = "Sheet1";

                Find the sheet with the supplied name, and then use that 
                Sheet object to retrieve a reference to the first worksheet.
                Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().
                  Where(s => s.Name == sheetName).FirstOrDefault();
                 Throw an exception if there is no sheet.
                if (theSheet == null)
                {
                    throw new ArgumentException("sheetName");
                }

                 Retrieve a reference to the worksheet part.
                WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));

                
                string text;
                
                
                foreach (Row r in theSheet.Elements<Row>())
                {
                    foreach (Cell c in r.Elements<Cell>())
                    {
                        Cell theCell = wsPart.Worksheet.Descendants<Cell>().FirstOrDefault();

                        
                        text = GetCellValue(theCell,wbPart);
                    

                        Console.Write(text + " ");
                    }
                }
                Console.WriteLine();
                Console.ReadKey();
            }
        }end ReadExcelFileDOMMethod

        public static string GetCellValue(Cell cell, WorkbookPart wbPart)
        {
            string value = null;

                if (cell != null)
                {
                    value = cell.InnerText;

                     If the cell represents an integer number, you are done. 
                     For dates, this code returns the serialized value that 
                     represents the date. The code handles strings and 
                     Booleans individually. For shared strings, the code 
                     looks up the corresponding value in the shared string 
                     table. For Booleans, the code converts the value into 
                     the words TRUE or FALSE.
                    if (cell.DataType != null)
                    {
                        switch (cell.DataType.Value)
                        {
                            case CellValues.SharedString:

                                 For shared strings, look up the value in the
                                 shared strings table.
                                var stringTable =
                                    wbPart.GetPartsOfType<SharedStringTablePart>()
                                    .FirstOrDefault();

                                 If the shared string table is missing, something 
                                 is wrong. Return the index that is in
                                 the cell. Otherwise, look up the correct text in 
                                 the table.
                                if (stringTable != null)
                                {
                                    value =
                                        stringTable.SharedStringTable
                                        .ElementAt(int.Parse(value)).InnerText;
                                }
                                break;

                            case CellValues.Boolean:
                                switch (value)
                                {
                                    case "0":
                                        value = "FALSE";
                                        break;
                                    default:
                                        value = "TRUE";
                                        break;
                                }
                                break;
                        }
                    }
            }
            return value;
    }


}
}

Most of this code was derived from the following tutorial:

http://msdn.microsoft.com/en-us/libr...ffice.15).aspx

The code in the tutorial explicitly inputs the cell address to retrieve the cell value. 

I need my program to retrieve the cell values without having to input the cell address into the method and to loop through all of the rows and columns. How do I go about updating this code to retrieve all of the values that are found within the sheet of the workbook? Every corresponding row and column?



The file I am working with has values in Column A through Column AJ and about 10,000 rows in total. I would need to retrieve all of those cell values and import them into a different application. I have been going at this for a few days now and would appreciate any help.



Thank you in advance for you all of your help.


Viewing all articles
Browse latest Browse all 1288

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>