2013-10-24 58 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>而不是數組,但我認爲你可以計算的那部分,如果它真的有必要;)

相關問題