0

我想要一个包含电子表格中所有单元格的所有值的数组或列表。有一定数量的列,但有任意数量的行。这是我从 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;

}

我真的很感激任何帮助。

4

1 回答 1

1

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

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

如果你删除Where(...),那应该给你所有的细胞......

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

现在你有了它,你可以在块中获取代码if (theCell != null) { ... }块,让它接受 a 的方法Cell(我ProcessCell在这里调用它,它需要是一个static返回 a 的函数),然后在循环中string调用它,比如foreach这个:

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

然后返回你的结果!

return results;

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

于 2013-10-24T22:32:40.067 回答