Analizando el valor de la celda de la hoja de cálculo de Excel

 C Programming >> Programación C >  >> Tags >> Excel
Analizando el valor de la celda de la hoja de cálculo de Excel

La solución para analizar el valor de la celda de la hoja de cálculo de Excel
se proporciona a continuación:

Analizo la celda ubicada en A2 Dirección. Esto devuelve 3 valor en lugar del esperado Categoría 1 .

prueba.xlsx

using System;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using X = DocumentFormat.OpenXml.Spreadsheet;

namespace DotNetSandbox.SO
{
    public class IncorrectCellValue
    {
        public static void ParseCellValue()
        {
            using SpreadsheetDocument doc = SpreadsheetDocument.Open(@"c:temptest.xlsx", false);
            X.Sheet sheet = doc.WorkbookPart.Workbook.Descendants<X.Sheet>().First();
            WorksheetPart wsPart = (WorksheetPart)doc.WorkbookPart.GetPartById(sheet.Id);
            X.Cell cell = wsPart.Worksheet.Descendants<X.Cell>().First(c => c.CellReference == "A2");

            string cellValue = cell.CellValue.Text;

            Console.WriteLine(cellValue);
            Console.ReadKey();
        }
    }
}

SALIDA:

  • Objetivo:.NET 5
  • DocumentFormat.OpenXml versión:2.13.0

¿Hago algo mal o tal vez es un error de la biblioteca?

Usa este método

 public static string GetCellValue(string fileName,
        string addressName, string sheetName = "")
    {
        string value = null;

        // 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;

            // Find the sheet with the supplied name, and then use that 
            // Sheet object to retrieve a reference to the first worksheet.
            var theSheets = wbPart.Workbook.Descendants<Sheet>();
            Sheet theSheet = string.IsNullOrEmpty(sheetName) ? theSheets.FirstOrDefault() : theSheets.FirstOrDefault(x => x.Name == sheetName);

            // 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));

            // Use its Worksheet property to get a reference to the cell 
            // whose address matches the address you supplied.
            Cell theCell = wsPart.Worksheet.Descendants<Cell>().
              Where(c => c.CellReference == addressName).FirstOrDefault();

            // If the cell does not exist, return an empty string.
            if (theCell.InnerText.Length > 0)
            {
                value = theCell.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 (theCell.DataType != null)
                {
                    switch (theCell.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;
    }

Te quedaste aquí:

Pude obtener la Categoría 1 ejecutando este código:

 var cellValue = GetCellValue(@"c:test.xlsx", "A2");

documento de Microsoft

Tenga en cuenta que cambié el método original para obtener la primera hoja si no pasa el nombre de la hoja al método.

Qué es una cadena compartida:
Para optimizar el uso de cadenas en una hoja de cálculo, SpreadsheetML almacena una única instancia de la cadena en una tabla denominada tabla de cadenas compartidas. Luego, las celdas hacen referencia a la cadena por índice en lugar de almacenar el valor en línea en el valor de la celda. Excel siempre crea una tabla de cadenas compartida cuando guarda un archivo.