2013-10-24 42 views
0

我想要一个数组或列表,其中包含电子表格中所有单元格的所有值。有一定数量的列,但有任意数量的行。这是我从msdn.com得到的方法,我想知道是否可以修改它以返回电子表格中的所有值,或者如果有办法让所有包含值的单元格,然后使字符串生成器迭代通过每个地址。真的,我试图让我的头在这个SDK,希望你们可以给我一些见解。下面是该方法:如何更改此方法以返回字符串数组

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

{ 字符串值= 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

下面的代码块抓住所有的细胞和过滤下来,只是我们试图找到一个...

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

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

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

现在,你有,你可以采取的代码块的if (theCell != null) { ... }块内,使之法,需要一个Cell(我把它叫做ProcessCell在这里,它会需要一个static功能返回一个string),并把它称为foreach循环中,像这样:

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

,然后就返回你的结果!

return results; 

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

相关问题