如何更改此方法以返回字符串数组

本文关键字:字符串 数组 返回 何更改 此方法 | 更新日期: 2023-09-27 18:36:56

我想要一个包含电子表格中所有单元格所有值的数组或列表。有固定数量的列,但行数不限。这是我从 msdn.com 那里得到的方法,我想知道我是否可以修改它以返回电子表格中的所有值,或者是否有办法获取所有具有值的单元格,然后创建一个字符串生成器来迭代每个地址。真的,我正在尝试了解这个SDK,希望你们能给我一些见解。这是方法:

public static string GetCellValue(string fileName, 
string sheetName, 
string addressName)

{ 字符串值 = 空;

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

}

我真的很感激任何帮助。

如何更改此方法以返回字符串数组

以下代码块抓取所有单元格并将其过滤为我们试图查找的单元格......

Cell theCell = wsPart.Worksheet.Descendants<Cell>().
  Where(c => c.CellReference == addressName).FirstOrDefault();

如果你删除Where(...),那应该给你所有的单元格...

var cells = wsPart.Worksheet.Descendants<Cell>();

现在你有了它,你可以把代码块放在if (theCell != null) { ... }块中,让它成为一个Cell的方法(我在这里称之为ProcessCell,它需要是一个返回stringstatic函数),并在foreach循环中调用它,如下所示:

var results = new List<String>();
foreach cell in cells
{
    results.Add(ProcessCell(cell));
}

然后返回您的结果!

return results;

从技术上讲,我没有回答您的问题,因为返回类型是List<String>而不是数组,但我认为如果真的有必要,您可以弄清楚该部分;)