Есть ли способ получить значение последней заполненной ячейки строки из столбца, используя OpenXml - PullRequest
1 голос
/ 04 апреля 2019

Я работаю над консольным приложением, которое просто извлекает значение из указанной ячейки и отображает это значение на консоли. Я хотел бы изменить код и получить значение последней ячейки в столбце. Я могу получить значение ячеек, которое я указываю, но я хочу получить только последнюю заполненную ячейку (потому что addressName может измениться, когда лист обновляется с большим количеством строк). В настоящее время я использую код ниже, чтобы получить значения по addressName. Может кто-нибудь указать мне правильное направление или показать пример. пожалуйста и спасибо.

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

namespace ConsoleApp5
{
 class Program
  {
    static void Main(string[] args)
    {
        string fileName = @"C:\Temp\myTempDoc\bigexcel.xlsx";
        string sheetName = "sheet1";
        string addressName = "B25";
        var cellVall =GetCellValue(fileName, sheetName, addressName);
        Console.WriteLine(cellVall);
        Console.ReadKey();
    }
    public static string GetCellValue(string fileName,string sheetName,string addressName)
    {
        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.
            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));
            // 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 != null)
            {
                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;
    }
  }
 }

1 Ответ

0 голосов
/ 05 апреля 2019

Мне не совсем ясно, идет ли вы после последнего Cell в Row по вашему выбору или последнего Cell из последнего Row. Оба подхода очень похожи, поэтому я покажу, как сделать оба.

Основной принцип состоит в том, чтобы сначала найти Row, к которому вы стремитесь, а затем схватить ребенка Cell с этого Row.

Если вы хотите самый последний Cell листа, тогда мы просто хотим последний Row:

//grab the last row
Row row = wsPart.Worksheet.Descendants<Row>().LastOrDefault();

Однако, если вы хотите иметь возможность передать номер строки и получить последний Cell из этого Row, то что-то вроде этого поможет (здесь переменная rowIndex обозначает индекс Row для которого вы хотите последний Cell:

//find the row that matches the rowNumber we're after
Row row = wsPart.Worksheet.Descendants<Row>()
                          .Where(r => r.RowIndex == rowIndex).FirstOrDefault();

Если у вас есть Row, это всего лишь случай получения последнего Cell этого Row с использованием кода, аналогичного приведенному выше:

Cell theCell = null;

//find the row that matches the rowNumber we're after
Row row = wsPart.Worksheet.Descendants<Row>().Where(r => r.RowIndex == rowIndex).FirstOrDefault();

if (row != null)
{
    //now grab the last cell of that row
    theCell = row.Descendants<Cell>().LastOrDefault();
}

// If the cell does not exist, return an empty string.
if (theCell != null)
...
...